5

I'm trying to implement change password functionality in Symfony 2 project. I have entity User with validation rules in validation.yml file. In User entity I have field "password" with its validation constraints in validation.yml.
I created form with 2 field 'password' and 'confirmPasswod'. I want to use my entity validation constraints for 'password' field and check equality between 'passwod' and 'confirmPassword' fields. In my contronller I write

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

In 'User' entity I don't have 'confirmPasswod' field. So I get error:

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

Is there any way to use entity-based form validation for some form fields and not entity-based validation for other? Thanks in advance.

1 Answer 1

16

In SymfonyForm\ChangePasswordType you 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',
));

Since Symfony 2.1 you can configure options to avoid broken element name (as mentioned in 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'),    
));
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me as well. Thanks. There is one thing I changed, though. I used password and password_confirmation instead of Password and Password confirmation. If you use the latter, you end up with awkward element names like vnn_pressboxbundle_preferencestype_password_Confirm password.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.