Symfony2 Assert\Expression 注释不支持常量

FZE*_*FZE 3 php annotations metaprogramming symfony

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;
Run Code Online (Sandbox Code Playgroud)

loc*_*inz 6

尝试使用

/**
 * @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"}
 * )
 */
Run Code Online (Sandbox Code Playgroud)

相反(省略了示例的部分内容以集中在constant()此处使用)。

作为参考,请参阅

  • 好吧,我发现问题是这样的,它需要命名空间,抱歉。FrontendBundle\\EntityInterface::COUNTRY_DEFAULT_VALUE 现在可以了。 (2认同)