如何在 sylius 上自定义密码验证器?

Posted

技术标签:

【中文标题】如何在 sylius 上自定义密码验证器?【英文标题】:How can I custom password validator on sylius? 【发布时间】:2017-10-10 14:58:49 【问题描述】:

我想自定义用户密码的验证器。我对此进行了测试:

http://sylius-try.readthedocs.io/en/latest/bundles/general/overriding_validation.html

http://sylius-older.readthedocs.io/en/latest/customization/validation.html

http://docs.sylius.org/en/latest/customization/validation.html

但它对我不起作用。这是我的代码:

#AppBundle/Resources/config/validation.yml

Sylius\Bundle\UserBundle\Form\Model\ChangePassword:
    properties:
        currentPassword:
            - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
                message: sylius.user.plainPassword.wrong_current
                groups: [myGroup]
        newPassword:
            - Regex:
                pattern: ((?=\S*?[A-Z])(?=\S*?[a-z]).8,)
                message: myGroup.user.password.regex
                groups: [myGroup]

配置:

#app/config/config.yml   
 imports:
    -  resource: "@SyliusCoreBundle/Resources/config/app/config.yml" 

Services.yml:

# app/config/services.yml
parameters:
    sylius.form.type.user_change_password: [myGroup]
    sylius.form.type.user_reset_password.validation_groups: [myGroup] # the product class also needs to be aware of the translation'a validation

问题是当我尝试更改密码或注册时,未检测到正则表达式。我确信它可以工作,因为当我更改文件 ChangePassword.yml 时,一切正常。但我无法接触到供应商的文件。

感谢您的宝贵时间

【问题讨论】:

好吧,您的正则表达式可能无法按预期工作。试试 ^(?=.*?[A-Z])(?=.*?[a-z]).8,$ 或者因为它是 php - /^(?=.*?[A-Z])(?=.*?[a-z]).8,$/ 正则表达式是正确的,我在vendor上试过,我在网上试过,不是这个问题。问题是 Sylius 没有找到覆盖文件 【参考方案1】:

我找到了解决方案。我将 [myGroup] 替换为 [sylius]。

在教程中说要自定义名称,但是当我这样做时它不起作用。所以我认为我们必须把 [sylius]

【讨论】:

以上是关于如何在 sylius 上自定义密码验证器?的主要内容,如果未能解决你的问题,请参考以下文章

Angular使用总结 ---以密码确认为例实现模版驱动表单的自定义校验

在 Symfony5.3 上自定义新的身份验证错误消息

剃须刀页面上自定义验证属性的 ASP.NET Core 客户端验证

如何使用spring security使用空密码进行基本身份验证?

SpringSecurity+Oauth2.0登录认证+自定义验证

自定义密码匹配验证器在 angular5 中不起作用