我的验证器服务未提供给验证器。我收到一个错误:“警告:My\Bundle\Validator\Constraints\MyCustomValidator::__construct() 缺少参数 1...”
这是我的 services.yml
// My\Bundle\Resources\config\services.yml
services:
my.validator.service:
class: My\Bundle\Validator\Constraints\MyCustomValidator
arguments: [ @doctrine.orm.entity_manager ]
这是我的验证器类:
// My\Bundle\Validator\Constraints\MyCustomValidator
namespace My\Bundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManager;
class MyCustomValidator extends ConstraintValidator
{
private $em;
public function __construct($em)
{
$this->em = $em;
}
public function validate($value, Constraint $constraint)
{
// Do something
}
}
这是我的验证.yml
// My\Bundle\Resources\config\validation.yml
My\Bundle\Entity\Page:
properties:
name:
- NotBlank: ~
- My\Bundle\Validator\Constraints\MyCustom: ~
这是我的约束类
namespace My\Bundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class MyCustom extends Constraint
{
public $message = 'Something is wrong with "%string%".';
public function validatedBy()
{
return get_class($this) . 'Validator';
}
}
如果有人可以帮助我,我将不胜感激。
我也尝试将参数名称更改为“@doctrine.orm.default_entity_manager”,但没有运气。