Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate a form field against multiple constraints

I have built a registration form where I want to validate fields in it. In my RegistrationFormType I have following code:

public function getDefaultOptions(array $options)
    {
        $collectionConstraint = new Collection(array(
            'email' => new Collection(array(
                new NotBlank(),
                new Email(array('message' => 'Invalid email addressadsfa')),
                )),
            'username' => new Email(array('message' => 'arg Invalid email addressadsfa')),
            'code' => new MaxLength(array('limit'=>20)),
            'plainPassword' => new MaxLength(array('limit'=>20)),
        ));

        return array(
            'csrf_protection' => false,
            'validation_constraint' => $collectionConstraint,
        );
    }

Problem is: The email validation does not work. What am I doing wrong?

like image 315
stoefln Avatar asked Jan 12 '12 11:01

stoefln


People also ask

How do you validate a constraint?

Constraint validation process Constraint validation is done through the Constraint Validation API either on a single form element or at the form level, on the <form> element itself.

What is constraint validation API?

The Constraint Validation API enables checking values that users have entered into form controls, before submitting the values to the server.


1 Answers

You don't need to make the email entry a Collection, just use a simple array. So:

public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection(array(
        'email' => array(
            new NotBlank(),
            new Email(array('message' => 'Invalid email addressadsfa')),
        ),
        'username' => new Email(array('message' => 'arg Invalid email addressadsfa')),
        'code' => new MaxLength(array('limit'=>20)),
        'plainPassword' => new MaxLength(array('limit'=>20)),
    ));

    return array(
        'csrf_protection' => false,
        'validation_constraint' => $collectionConstraint,
    );
}
like image 145
Alessandro Desantis Avatar answered Sep 29 '22 13:09

Alessandro Desantis