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()]
        ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!