3

我正在尝试构建一个作为服务运行的自定义验证器(主要用于获取实体管理器)。

我关注了文档和一些博客文章,但无法使其正常工作。我有这个错误

    Catchable Fatal Error: Argument 1 passed to 
D\AjaxBundle\Validator\Constraints\SelectTypeValidator::__construct() must implement 
interface Doctrine\Common\Persistence\ObjectManager, none given, called in 
/AJAX/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php on line 67 and defined in 
/AJAX/src/D/AjaxBundle/Validator/Constraints/SelectTypeValidator.php line 14

服务.yml

    validator.selectType:
        class: D\AjaxBundle\Validator\Constraints\SeletTypeValidator
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: validator.selectType, alias: selectType }

选择类型验证器:

namespace D\AjaxBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

use Doctrine\Common\Persistence\ObjectManager;

class SelectTypeValidator extends ConstraintValidator
{
    private $om;

    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    public function validate($value, Constraint $constraint)
    {
        $fieldOne = $this->om->getRepository('DAjaxBundle:City')->findOneBy(array('id' =>
                $value->getId()));

        if ($fieldOne == null) {
            $this->context->addViolation($constraint->message, array('%string%' => $value->
                    getId()));
        }
    }
}

选择类型

namespace D\AjaxBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class SelectType extends Constraint
{
    public $message ='jakis text';

    public function validateBy()
    {
        return 'selectType';
    }
}
4

2 回答 2

1

服务标签名称应validator.constraint_validatorvalidator.selectType

于 2012-12-12T17:54:46.810 回答
0

我不确定这是否会有所作为,但值得一试......我从不使用引号包装服务参数:

arguments: [@doctrine.orm.entity_manager]
于 2012-12-12T17:51:18.080 回答