简体   繁体   中英

Symfony2 switching validation

I have 2 customized input fields password and autoGeneratePw. The validator should only validate password input when autoGeneratePw is unchecked. How could I achieve this?

 $form->add('autoGeneratePw', 'checkbox', array(
            'label'=>'Auto generate password',
            'data' => true,
            'mapped' => false,
            'required' => false,
            ));
$form->add('password', 'repeated', array(
             'type' => 'password',
             'first_name' => 'Password',
             'second_name'=> 'RetypePassword',
             'invalid_message' => 'Password are not the same',
         ));

Imho you're overthinking this. Just add the generated password in the required fields (and use a dummy field which won't be submitted to show it in cleartext to the user) like if it was entered from the user, this way you don't have to change anything (especially not the Form component). Phpmyadmin does it like this for example:

为新用户生成phpmyadmin密码

If you're afraid that the generated password wouldn't pass the validation, then you're doing it wrong. ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM