问题
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