stoefln
stoefln

Reputation: 14556

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?

Upvotes: 8

Views: 7240

Answers (1)

Alessandro Desantis
Alessandro Desantis

Reputation: 14343

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,
    );
}

Upvotes: 19

Related Questions