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.

如果找不到您的类,您可能忘记通过别名(在您的验证器类中)重命名validateBy() 方法返回的字符串:

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