【问题标题】:Symfony2 Assert\Expression annotation doesn't support ConstantsSymfony2 Assert\Expression 注解不支持常量
【发布时间】:2015-12-27 09:43:18
【问题描述】:

通常我在所有基于注释的地方使用Constants,例如annotations, route and assert annotations,但在 Assert\Expression 中会抛出 Variable "EntityInterface" is not valid around position 26. 这是一个错误还是特殊的罕见情况?

<?php

    /**
     * @var string
     *
     * @ORM\Column(name="id_number", type="string", length=11, nullable=true)
     * @Assert\Expression(
     *     "this.getNationality() == EntityInterface::COUNTRY_DEFAULT_VALUE and value != null",
     *     message = "form.user.validation.id_number.blank",
     *     groups = {"personal_info"}
     * )
     * @Assert\Regex(
     *      pattern="/^([\d]{11})$/",
     *      match=true,
     *      message="form.user.validation.id_number.regex",
     *      groups = {"personal_info"}
     * )
     */
    private $idNumber;

【问题讨论】:

    标签: php symfony annotations metaprogramming


    【解决方案1】:

    尝试使用

    /**
     * @ORM\Column(name="id_number", type="string", length=11, nullable=true)
     * @Assert\Expression(
     *     "this.getNationality() == constant('EntityInterface::COUNTRY_DEFAULT_VALUE') and value != null",
     *     message = "form.user.validation.id_number.blank",
     *     groups = {"personal_info"}
     * )
     */
    

    改为(省略部分示例以专注于在此处使用constant())。

    参考,见

    【讨论】:

    • 它不起作用,它说Warning: constant(): Couldn't find constant EntityInterface::COUNTRY_DEFAULT_VALUE我检查了常量是否存在。
    • 好的,我以这种方式找到了问题,它需要命名空间对不起。 FrontendBundle\\EntityInterface::COUNTRY_DEFAULT_VALUE 现在可以了。
    • 确实 @FZE 看起来常量 function() 无法从 use 语句中读取并需要转义:@Assert\Expression("!(this.foo == constant('App\\Domain\ \Bundle\\Foo\\Foo::bar)") 与 Range 等其他约束不同:使用 AppDomain\Model\Foo\Foo; @Assert\Range(min=Foo::MIN_NUMBER)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2012-01-23
    相关资源
    最近更新 更多