Symfony2 Assert \ Expression注释不支持常量

时间:2015-12-27 09:43:18

标签: php symfony annotations metaprogramming

通常我会在所有基于注释的地方使用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;

1 个答案:

答案 0 :(得分:2)

尝试使用

/**
 * @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())。

供参考,见