Validating a password in Symfony2

后端 未结 4 852
我寻月下人不归
我寻月下人不归 2020-12-31 12:08

I\'m trying to put together a change password feature in Symfony2. I have a \"current password\" field, a \"new password\" field and a \"confirm new password\" field, and th

4条回答
  •  死守一世寂寞
    2020-12-31 12:48

    In Symfony 2.1 you can use the built-in validator: http://symfony.com/doc/master/reference/constraints/UserPassword.html

    So for instance in your form builder:

    // declare
    use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
    
    // mapped=>false (new in 2.1) is to let the builder know this is not an entity field
    ->add('currentpassword', 'password', array('label'=>'Current password', 'mapped' => false, 'constraints' => new UserPassword()))
    

    Apparently there's a bug right now with that validator so might or might now work https://github.com/symfony/symfony/issues/5460

提交回复
热议问题