2

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

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()

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

4

1 回答 1

2

只是一个想法:重命名您的 php 脚本(驼峰式)第一个字母是小写的“是...” - 它可能期望文件与包含的类命名相同。

于 2013-03-06T12:32:13.223 回答