【问题标题】:Error when validate entity with custom validation constraint使用自定义验证约束验证实体时出错
【发布时间】:2016-07-09 14:59:08
【问题描述】:

在我的控制器上应用验证方法时出现此错误。

可捕获的致命错误:传递给 att\Bundle\Validator\Constraint\CertWfCheckValidator::validate() 的参数 2 必须是 Symfony\Component\Validator\Constraint 的实例,没有给出,在 /var/www/html/ 中调用att/src/att/Bundle/Controller/CertificateController.php 在第 451 行并定义了

这是我的服务定义:

    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);
    }  
}

控制器

   $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;

【问题讨论】:

    标签: validation symfony dependency-injection constraints


    【解决方案1】:
    1. 不要在约束类的getTargets()方法中返回数组,改为return self::CLASS_CONSTRAINT;

    2 和主要的。 查看您的 Validator 类,您使用两个参数定义方法“validate”,但是当您调用此方法时,您只提供一个参数 - $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;
    

    【讨论】:

    • 谢谢丹尼斯。我可以通过调用验证服务来修复它。我更新了答案。
    【解决方案2】:

    更新:我找到了调用验证服务的解决方案。

    $constraint = new \att\Bundle\Validator\Constraint\CertWfCheck;
    $errors = $this->container->get('validator')
                    ->validateValue(
                            $entity,
                            $constraint);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多