Validating equal fields in Symfony 2

I am trying to implement password change functionality in a Symfony 2 project. I have an entity Userwith validation rules in a file validation.yml. In the object User, I have a field " password" with its validation restrictions in validation.yml.
I created a form with two fields " password" and " confirmPasswod". I want to use authentication restrictions for the password field and check the equality between the fields " passwod" and " confirmPassword". I write in my contronller

$form = $this->createForm(new SymfonyForm\ChangePasswordType(), new Entity\User());
if ($form->isValid())
    {..............}

In the "User" object, I do not have the "confirmPasswod" field. So I get the error message:

Neither property "confirmPassword" nor method "getConfirmPassword()" nor method "isConfirmPassword()" exists in class

Is there a way to use suffix form validation for some form fields, rather than entity based validation for others? Thanks in advance.

+5
source share
1 answer

In SymfonyForm\ChangePasswordTypeyou can use something like this:

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

With Symfony 2.1, you can tweak the parameters to avoid a broken element name (as indicated in the comment)

$builder->add('password', 'repeated', array(
    // … the same as before 
    'first_name' => 'passwd',
    'second_name' => 'passwd_confirm',
    // new since 2.1
    'first_options'  => array('label' => 'Password'),
    'second_options' => array('label' => 'Password confirmation'),    
));
+16
source

All Articles