Symfony2 - Custom validator and dependancy injection

此生再无相见时 提交于 2019-12-06 09:32:00

问题


I am trying to use dependancy injection for a custom validator, in order to be able to use the entityManager.

I followed the Symfony Example: Dependency Injection, but I am allways getting this error message:

FatalErrorException: Error: Class 'isdoi' not found in /home/milos/workspace/merrin3/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php line 68

Here are my classes:

1. The IsDOI class:

    <?php
    namespace Merrin\MainBundle\Validator\Constraints;
    use Symfony\Component\Validator\Constraint;

    /**
     * @Annotation
    */
    class IsDOI extends Constraint
    {
    public $message_publisher_DOI = 'The Publisher DOI abbreviation does not correspond to the DOI you filled in !';
    public $message_journal_DOI = 'No journal found with the DOI you filled in !';
    public $journal;
    public $doiAbbreviation;

     public function validatedBy() {
            return "isdoi";
        }

        public function getTargets()
        {
            return self::CLASS_CONSTRAINT;
        }
    }

2. The IsDOIValidator class:

<?php

namespace Merrin\MainBundle\Validator\Constraints;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class IsDOIValidator extends ConstraintValidator
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
    $this->entityManager = $entityManager;
    }

    public function validate($value, Constraint $constraint)
    {       
        $em_mdpipub = $this->entityManager('mdpipub');
        //Do some tests here...

    }
}

3. Service:

merrin.main.validator.isdoi:
    class:  Merrin\MainBundle\Validator\Constraints\IsDOIValidator
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"

Where am I wrong? Thank you for your help.


回答1:


You have wrong service file, when You add tags and alias you could use "isdoi" name

merrin.main.validator.isdoi:
    class:  Merrin\MainBundle\Validator\Constraints\IsDOIValidator
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"
    tags:
        - { name: validator.constraint_validator, alias: isdoi }



回答2:


You're telling Symfony2 that the validator class for your constraint is isdoi (validateBy method). However, your validator is IsDOIValidator.

You must use :

public function validateBy()
{
    return "IsDOIValidator";
}

However, if your Constraint class name is IsDOI, Symfony will automatically look for IsDOIValidator as a ConstraintValidator. The default behavior for validateBy is to append "Validator" to the constraint name, and look for the class with this name. So if you do not overload validateBy, Symfony2 will automatically search for IsDOIValidator.



来源:https://stackoverflow.com/questions/17671384/symfony2-custom-validator-and-dependancy-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!