在我的控制器上应用验证方法时出现此错误。
捕获致命错误:传递给att \ Bundle \ Validator \ Constraint \ CertWfCheckValidator :: validate()的参数2必须是Symfony \ Component \ Validator \ Constraint的实例,没有给出,在/ var / www / html /中调用第451行上的att / src / att / Bundle / Controller / CertificateController.php并定义了
这是我的服务定义:
att.validator.certificate.wkcheck:
class: att\Bundle\Validator\Constraint\CertWfCheckValidator
arguments: ["@doctrine.orm.entity_manager", "@service_container"]
tags:
- { name: validator.constraint_validator, alias: validator.certwkcheck }
约束类:
namespace att\Bundle\Validator\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class CertWfCheck extends Constraint {
public $message = "El certificado tiene un Tramite asociado, no se puede eliminar";
public function validatedBy() {
return 'validator.certwkcheck';
}
public function getTargets() {
return [self::CLASS_CONSTRAINT];
}
}
验证员类:
namespace att\Bundle\Validator\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CertWfCheckValidator extends ConstraintValidator {
protected $em;
protected $container;
public function __construct(\Doctrine\ORM\EntityManager $em, \Symfony\Component\DependencyInjection\ContainerInterface $container) {
$this->em = $em;
$this->container = $container;
}
public function validate($value, Constraint $constraint) {
var_dump($value);
}
}
Controoller
$certificate = $this->get('certificate.manager')
->getCertificateById(
$this->getRequest()->request->get('id')
);
$validator = $this->get('att.validator.certificate.wkcheck');
$errors = $validator->validate($certificate);
$errorsString = (string) $errors;
答案 0 :(得分:0)
return self::CLASS_CONSTRAINT;
2和主要。 查看您的Validator类,您可以定义方法"验证"有两个参数,但是当你调用这个方法时,你只提供一个参数 - $ value。所以你应该初始化你的约束类,并作为第二个参数传递给方法。像这样:
$certificate = $this->get('certificate.manager')
->getCertificateById(
$this->getRequest()->request->get('id')
);
$validator = $this->get('att.validator.certificate.wkcheck');
$constraint = new CertWfCheck();
$errors = $validator->validate($certificate, $constraint);
$errorsString = (string) $errors;
答案 1 :(得分:0)
更新:我找到了解决方案调用验证服务。
$constraint = new \att\Bundle\Validator\Constraint\CertWfCheck;
$errors = $this->container->get('validator')
->validateValue(
$entity,
$constraint);