10

我正在尝试覆盖 FOSUserBundle 中当前对密码的验证。我已经尝试了几个选项,但我仍然找不到解决方案。

为了增加密码的最小长度,我创建了一个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-passwordFOSUserBundle 的默认最小长度验证上,正在取得所有权。

为了更清楚地说明我的问题,在 FOSUserBundle 中为密码设置 MinLength 以确保它在任何地方都得到验证的正确方法是什么?

此外,使用 FOSUserBundle 在 ChangePassword 中验证的正确方法是oldpassword != newpassword什么?

4

2 回答 2

4

validation.yml 应该在覆盖 FOS 用户实体的同一个包中

您应该使用 FOS 而不是 Acme,并且您应该只需要一个验证集。

# 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

于 2013-09-18T20:34:53.077 回答
1

您可以使用验证组

http://symfony.com/doc/2.0/book/validation.html#validation-groups

于 2012-02-09T08:26:10.270 回答