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 スクリプトの名前を変更します (キャメルケース)。最初の文字は小文字の「is...」です。ファイルの名前は、含まれているクラスと同じであると想定される場合があります。

于 2013-03-06T12:32:13.223 に答える