我正在尝试构建一个作为服务运行的自定义验证器(主要用于获取实体管理器)。
我关注了doc和一些博客文章,但无法使其正常运行。我有这个错误
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
SelectTypeValidator.php
namespace D\AjaxBundle\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Bridge\Doctrine\RegistryInterface;
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()));
}
}
}
SelectType.php
namespace D\AjaxBundle\Validator;
use Symfony\Component\Validator;
class SelectType extends Constraint
{
public $message ='message';
public function validateBy()
{
return 'SelectTypeValidator';
}
public function getRequiredOptions()
{
return array();
}
}
services.yml
services:
d_ajax.validator.SelectTypeValidator:
class: D\AjaxBundle\Validator\SelectTypeValidator
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: validator.constraint_validator, alias: SelectTypeValidator }