1

カスタムバリデーターをサービスとして公開しています (依存関係として他のサービスが必要なため)。私はそれを次のように定義しました:

<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

   <services>
      <service id="validator.unique.available_email_validator" 
               class="Nourdine\BasicBundle\Validator\Constraints\AvailableEmailValidator">
         <argument type="service" id="signup_manager" />
         <tag name="validator.constraint_validator" alias="available_email_validator" />
      </service>   
   </services>

</container>

問題は、すべてが機能しないことです。エラーは次のとおりです。

Class 'available_email_validator' not found in /home/nourdine/development/symf-app/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php on line 67 

実際のところ、次の場合、バリデーターは利用可能なサービスの中に表示されません。

console container:debug

したがって、問題はサービスが正しく公開されていないことにあると思われます。それを解決して、検証プロセスの爆発を解決します。

誰?

4

1 に答える 1

2

ドキュメントで述べたように:

As mentioned above, Symfony2 will automatically look for a class named after the constraint, with Validator appended. If your constraint validator is defined as a service, it's important that you override the validatedBy() method to return the alias used when defining your service, otherwise Symfony2 won't use the constraint validator service, and will instantiate the class instead, without any dependencies injected.

クラスが見つからない場合は、validatedBy() メソッドによって返された文字列の名前をエイリアス (バリデータ クラス内) で変更するのを忘れている可能性があります。

public function validatedBy()
{
    return 'available_email_validator';
}
于 2012-12-08T14:29:25.833 に答える