1

My question is "How can I do form repeated validation in Entity with annotation?". I have an Account entity with (email, password and confirmPassword) attributes. When a new user registers a new account, he/she has to fill in email, password and confirmPassword fields. Obviously, password and confirmPassword fields must match. I saw an example of this validation with pure php (form builder) in Stachoverflow like below.

$builder->add('password', 'repeated', array(
    'type' => 'password',
    'first_name' => 'Password',
    'second_name' => 'Password confirmation',
    'invalid_message' => 'Passwords are not the same',
));

But, this is not what I want. I want this functionality with annotation in my Account entity. Maybe

* @Assert\Match(
*      matchField = "password",
*      message = "The password confirmation does not match password."
* )
protected $confirmPassword;
1
  • Why would you use $confirmPassword in your entity?
    – gremo
    Mar 20, 2012 at 7:27

2 Answers 2

2

You might use method validation:

/**
 * @Assert\True(message = "Passwords are not the same")
 */
public function isPasswordLegal()
{
   return ($this->password == $this->confirmPassword);
}
1

See http://symfony.com/doc/current/reference/forms/types/repeated.html#validation

One of the key features of the repeated field is internal validation (you don't need to do anything to set this up) that forces the two fields to have a matching value. If the two fields don't match, an error will be shown to the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.