我正在尝试构建一个作为服务运行的自定义验证器(主要用于获取实体管理器)。
我关注了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
services.yml
validator.selectType:
class: D\AjaxBundle\Validator\Constraints\SeletTypeValidator
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: validator.selectType, alias: selectType }
SelectTypeValidator:
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()));
}
}
}
SelectType
namespace D\AjaxBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class SelectType extends Constraint
{
public $message ='jakis text';
public function validateBy()
{
return 'selectType';
}
}
答案 0 :(得分:1)
服务标签名称应为validator.constraint_validator
,而不是validator.selectType
答案 1 :(得分:0)
我不确定这是否会有所作为,但值得一试...我从不用引号括起服务参数:
arguments: [@doctrine.orm.entity_manager]