【问题标题】:Symfony2 custom validatorSymfony2 自定义验证器
【发布时间】:2011-06-01 22:27:15
【问题描述】:

我正在尝试使用 symfony2 构建自定义验证器,但发生了一些奇怪的事情:

我已经按照symfony2 cookbook 中的步骤创建了密码和密码验证,但是第一次加载页面时出现此错误:

AnnotationException: [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Password" in property NMSP\MyBundle\Entity\User::$password does not exist, or could not be auto-loaded. 

重新加载后错误消失,验证仍然没有触发,它返回代码是有效的。

这里是相关代码:

//annotation declaration:
/**
 * @ORM\Column(type="string", length="32", unique="true")
 * 
 * @Assert\MinLength(3)
 * @Assert\Password2()
 */
protected $password;

//load files with the following in the code
services:
  validator.password:
    class: NMSP\MyBundle\Validator\PasswordValidator
    tags:
        - { name: validator.constraint_validator, alias: password }

想不出来这个:(

【问题讨论】:

    标签: validation symfony


    【解决方案1】:

    假设您的自定义验证器约束不在 Symfony\Component\Validator\Constraints 命名空间中,而是您自己的命名空间:NMSP\MyBundle\Validator。

    您应该添加以下使用语句:

    use NMSP\MyBundle\Validator as NMSPAssert;
    

    然后在$username 属性上使用以下注解:

    @NMSPAssert\Password()
    

    应该可以的。

    【讨论】:

    • 仍然需要更多帮助。我无法弄清楚如何在自定义验证器中访问数据库以及如何获取其他实体属性的值,例如用于验证(密码 = passconf)......我重新阅读了文档,但他们没有谈论这个:(
    • 您可能已经弄清楚了,但这里是上面评论中您问题的答案:stackoverflow.com/questions/8934607/…
    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多