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?
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.
The Constraint Validation API enables checking values that users have entered into form controls, before submitting the values to the server.
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,
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With