Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override password validation in FOSUserBundle

I'm using FOSUserBundle in my Symfony2 project. I can't find any sensible explanation how to override form fields validation. I need to set password field validation to at least 7 characters, at least one small letter, at least one capital letter and at least one digit. None of solutions I've found alreade seems to work. Any ideas?

like image 352
pawel.kalisz Avatar asked Feb 21 '13 15:02

pawel.kalisz


2 Answers

Use a standard Regex constraint with the following pattern /(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/

class User extends FOSUser
{

    /**
     * @Assert\Regex(
     *  pattern="/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/",
     *  message="Password must be seven or more characters long and contain at least one digit, one upper- and one lowercase character."
     * )
     */
    protected $plainPassword;

}
like image 78
Tamlyn Avatar answered Nov 08 '22 00:11

Tamlyn


you shoud create a custom constraint class like described in documentation

  1. Add your regex expression inside isValid method
  2. Add your custom error messages
  3. add your constraint class to your entity class with validation.yml (or with annotations if you prefer)

that should fit your needs

like image 41
Julien Rollin Avatar answered Nov 08 '22 02:11

Julien Rollin