2015-12-27 36 views
2

通常我在所有基于注释的位置使用Constants,例如, annotations, route and assert annotations,但是在Assert \ Expression中抛出Variable "EntityInterface" is not valid around position 26. 这是一个错误还是特殊的罕见情况?Symfony2 Assert Expression注释不支持常量

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

尝试使用

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

仅供参考,请参阅

+0

它不工作,它说'警告:常数():无法找到固定EntityInterface :: COUNTRY_DEFAULT_VALUE'我检查常量在那里。 – FZE

+1

好的我发现这个问题需要名称空间抱歉。 FrontendBundle \\ EntityInterface :: COUNTRY_DEFAULT_VALUE现在可以。 – FZE

+0

确实@FZE它看起来像常量函数()不能读取使用语句,并需要转义: @ @ assert \ Expression(“!(this.foo ==常量\\富::巴)“) 不像其他约束像范围例如: 使用的AppDomain \模型\富\富; @assert \范围(MIN =美孚:: MIN_NUMBER) – webDEVILopers