2012-02-08 286 views
9

我正在嘗試覆蓋FOSUserBundle中密碼的當前驗證。我嘗試了幾個選項,但仍然找不到解決方案。FOSUserBundle密碼驗證

爲了提高密碼的使用MINLENGTH,我創建了一個validation.yml有:

# src/Acme/UserBundle/Resources/config/validation.yml 
Acme\UserBundle\Entity\User: 
    properties: 
     username: 
      - MinLength: { limit: 3, message: "Your username must have at least {{ limit }} characters." } 
      - MaxLength: { limit: 255, message: "The username is too long" } 
      - NotBlank: { message: "Please enter a username"}  

     plainPassword: 
      - NotBlank: { message: "Please enter a password"} 
      - MinLength: { limit: 8, message: "Your password must have at least {{ limit }} characters.", groups [Registration,Profile]} 
       - MaxLength: { limit: 255, message: "The password is too long" } 

Acme\UserBundle\Form\Model\ChangePassword: 
    properties: 
     new: 
      - NotBlank: { message: "Please enter a new password", groups [ChangePassword]} 
      - MinLength: { limit: 8, message: "Your password must have at least {{ limit }} characters.", groups [ChangePassword]} 
      - MaxLength: { limit: 255, message: "The password is too long", groups [ChangePassword]} 

Acme\UserBundle\Form\Model\ResetPassword: 
     new: 
      - NotBlank: { message: "Please enter a new password", groups [ResetPassword]} 
      - MinLength: { limit: 8, message: "Your new password must have at least {{ limit }} characters.", groups [ResetPassword]} 
      - MaxLength: { limit: 255, message: "The new password is too long", groups [ResetPassword]} 

這是好的爲我工作的/register,但/change-password默認的最小長度的驗證從FOSUserBundle正在所有權。

爲了更清楚地陳述我的問題,在FOSUserBundle中爲密碼設置MinLength以確保其在任何地方都得到驗證,正確的方法是什麼?

另外,什麼是正確的方法與FOSUserBundle在ChangePassword內驗證oldpassword != newpassword

回答

4

validation.yml應在覆蓋FOS用戶實體

而是極致,你應該使用FOS同捆,你應該只需要一個驗證集。

# src/Acme/UserBundle/Resources/config/validation.yml 
FOS\UserBundle\Model\User: 
    properties: 
     username: 
     - MinLength: { limit: 3, message: "Your username must have at least {{ limit }} characters." } 
     - MaxLength: { limit: 255, message: "The username is too long" } 
     - NotBlank: { message: "Please enter a username"}  

     plainPassword: 
     - NotBlank: { message: "Please enter a password", groups:[Registration, ResetPassword, ChangePassword] } 
     - MinLength: { limit: 8, message: "Your password must have at least {{ limit }} characters.", groups:[Registration, ResetPassword, ChangePassword] } 
     - MaxLength: { limit: 255, message: "The password is too long", groups:[Registration, ResetPassword, ChangePassword] } 

當遇到困難,轉到源: https://github.com/FriendsOfSymfony/FOSUserBundle/issues/987

+1

這不似乎是基於這個http://symfony.com/doc/current/reference/constraints/Length.html長度驗證的正確語法 – gondo 2014-01-29 20:01:27