简体   繁体   English

Symfony2中标准验证器的自定义验证消息

[英]Custom validation messages for standard validators in Symfony2

I want to override only one message in symfony2 default validation message which is : 我只想覆盖symfony2默认验证消息中的一条消息:

This value should be the user current password. 此值应该是用户当前的密码。

this message has as a key security.validator.user_password I tried to override this message by creating BundleName/Resources/config/security.yml . 该消息具有密钥security.validator.user_password我尝试通过创建BundleName/Resources/config/security.yml覆盖此消息。

BundleName/Resources/config/security.yml BundleName / Resources / config / security.yml

security:
    validator:
        user_password: custom message 

I tried xml file and cleared the cache but I get nothing. 我尝试了xml文件并清除了缓存,但一无所获。

Any suggestions ? 有什么建议么 ?

I think you can override using the validation.yml file in your bundle config like said on symfony2 doc. 我认为您可以像在symfony2 doc上所说的那样,在包配置中使用validation.yml文件进行覆盖。

### src/UserBundle/Resources/config/validation.yml
Acme\UserBundle\Form\Model\ChangePassword:
properties:
   oldPassword:
       - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
           message: "Wrong value for your current password"

I found a solution to this when using FOSUserBundle as a parent to my Users bundle: 当使用FOSUserBundle作为Users捆绑包的父级时,我找到了解决方案:

# Acme\UsersBundle\config\validation.yml
Acme\UsersBundle\Entity\User:
    properties:
        password:
            - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
                message: Please check your current password and try again

This pattern can be used to override any of the default strings which are shown when validation on a given field in that entity fails. 对该实体中给定字段的验证失败时,可以使用此模式来覆盖显示的任何默认字符串。

So to clarify: 所以要澄清一下:

  • Acme\\UsersBundle\\Entity\\User: the Entity on which validation is carried out Acme \\ UsersBundle \\ Entity \\ User:执行验证的实体
  • password: the name of the entity attribute you're validating 密码:您要验证的实体属性的名称
  • Symfony\\etc: the class which is actually doing the validation Symfony \\ etc:实际在进行验证的类
  • message: What you want to override with 消息:您要覆盖的内容

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM