Symfony2 Assert \ Expression注释不支持常量 [英] Symfony2 Assert\Expression annotation doesn't support Constants

查看:64
本文介绍了Symfony2 Assert \ Expression注释不支持常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Generally I use Constants in all of annotation based places e.g. annotations, route and assert annotations, but in Assert\Expression it throws Variable "EntityInterface" is not valid around position 26. Is this a bug or is a special rare case ?

<?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;

推荐答案

尝试使用

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

供参考,请参见

  • http://symfony.com/doc/current/components/expression_language/syntax.html#working-with-functions
  • http://php.net/manual/en/function.constant.php

这篇关于Symfony2 Assert \ Expression注释不支持常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆