Symfony2 Assert\Expression annotation doesn't support Constants

℡╲_俬逩灬. 提交于 2020-01-02 09:57:03

问题


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;

回答1:


Try using

/**
 * @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"}
 * )
 */

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

For reference, see

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


来源:https://stackoverflow.com/questions/34479270/symfony2-assert-expression-annotation-doesnt-support-constants

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!