dpjo15650 2018-09-14 12:10
浏览 60
已采纳

Symfony验证类:未定义的属性$ groups

I have the following validation class OneAnswerValidator.php:

<?php

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\InvalidArgumentException;

class OneQuestionOneAnswerValidator extends ConstraintValidator {
    // is this necessary? In the docs doesn't appear this property
    //public $groups = [];

    public function validate($answers, Constraint $constraint) {
        if (empty($answers)) {
            return;
        }
        $questions = [];
        foreach ($answers as $answer) {
            $questionId = $answer->getQuestion()->getId();
            if (isset($questions[$questionId])) {
                $this->context
                            ->buildViolation($constraint->message)
                            ->setParameter('{{ questionId }}', $value)
                            ->addViolation();
                break;
            }
        }
    }
}

With the associated constraint OneAnswer.php:

<?php

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class OneQuestionOneAnswer extends Constraint
{
    public $message = 'La pregunta {{ questionId }} tiene varias respuestas';
}

But when the form is submitted I get the following error:

[2018-09-14 13:59:38] request.CRITICAL: Uncaught PHP Exception PHPUnit\Framework\Error\Notice: "Undefined property: App\Validator\Constraints\OneQuestionOneAnswerValidator::$groups" at /Applications/MAMP/htdocs/team-analytics/vendor/symfony/form/Extension/Validator/Constraints/FormValidator.php line 84 {"exception":"[object] (PHPUnit\\Framework\\Error\\Notice(code: 8): Undefined property: App\\Validator\\Constraints\\OneQuestionOneAnswerValidator::$groups at /Applications/MAMP/htdocs/team-analytics/vendor/symfony/form/Extension/Validator/Constraints/FormValidator.php:84)"} []

In the documentation there is nothing about a property $groups (but the error get solved when I add that property to OneAnswerValidator class). Any idea why is this happening?

By the way, I am adding the constraint in a Form Type class:

    ->add('answers', EntityType::class, [
        'class' => Answer::class,
        'choice_label' => 'title',
        'label' => 'Respuesta',
        'multiple' => true,
        'constraints' => new OneAnswerValidator(['message' => 'fooo'])
    ]);

Thanks!

  • 写回答

1条回答 默认 最新

  • doujing3896 2018-09-14 16:37
    关注

    In your Form Type you have to give the Constraint (here OneQuestionOneAnswer) and not the ConstraintValidator.

    Try something like this

    ->add('answers', EntityType::class, [
            'class' => Answer::class,
            'choice_label' => 'title',
            'label' => 'Respuesta',
            'multiple' => true,
            'constraints' => [new OneQuestionOneAnswer()]
        ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?