自定义约束验证程序不存在,或无法自动加载

时间:2013-03-06 12:23:38

标签: php symfony

我正在尝试创建自定义约束验证器,但我无法使其正常工作,这是我遇到的错误:

AnnotationException: [Semantical Error] The annotation  
"@Cmpny\MyBundle\Validator\IsGifImage" in property           
Cmpny\MyBundle\Entity\Post::$src does not exist, or could not be auto-loaded.  

这是我项目的树:

src/Cmpny/MyBundle/Validator/
└── Constraint
    ├── isGifImage.php
    └── isGifImageValidator.php  

isGifImage.php

<?php
namespace Appinest\WhenayBundle\Validator\Constraint;  
use Symfony\Component\Validator\Constraint;

/**  
* @Annotation  
*/  
class IsGifImage extends Constraint¬
{
    public $message = 'This is not a gif image!';
}
?>

isGifImageValidator

  3 namespace Appinest\WhenayBundle\Validator\Constraint;¬
  2 ¬
  1 use Symfony\Component\Validator\Constraint;¬
  0 use Symfony\Component\Validator\ConstraintValidator;¬
  1 ¬
  2 class IsGifImageValidator extends ConstraintValidator¬
  3 {¬
  4     public function isValid($value, Constraint $constraint)¬
  5     {¬
  6         //My function
 22     }¬
    }

然后在我的实体类中导入:

use Cmpny\WhenayBundle\Validator\Constraint as CmpnyAssert;

然后使用我想要验证的断言:

@CmpnyAssert\IsGifImage()

我搜索了解决方案,却一无所获,有什么帮助吗? :/

1 个答案:

答案 0 :(得分:1)

只是一个想法:重命名你的php脚本(camel-case),第一个字母是小写的'是......' - 它可能希望文件的名称与包含的类相同。