本文介绍了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()).

instead (omitted parts of your example to focus on using constant() here).

参考,见

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

09-24 15:01