【问题标题】:Form not validating in Symfony 2.8Symfony 2.8 中的表单无法验证
【发布时间】:2016-10-03 13:39:57
【问题描述】:

我有一个基于 Symfony 2.8 的项目,并且我有一个使用 CraueFormFlowBundle 创建的表单,它允许创建多步骤表单。我为每个步骤创建了我的流程类和中间表单类型,一切都在这个级别上工作。唯一不起作用的是没有触发单个验证规则,无论是在每个步骤之间还是在表单流结束时。我在我的实体中使用注释来验证数据。我在config.yml 中检查了validationform 组件都是enabledframework.validation.enable_annotations 设置为true

这是我的控制器:

public function newEvaluationAction(Classroom $classroom, Subject $subject, Period $period)
{
    $evaluation = $this->getSchoolManager()->createEvaluation($classroom, $subject, $period);

    $flow = $this->get('app.form.flow.evaluation_create');
    $flow->bind($evaluation);

    $form = $flow->createForm();

    if ($flow->isValid($form)) {
        $flow->saveCurrentStepData($form);

        if ($flow->nextStep()) {
            $form = $flow->createForm();
        } else {
            $this->getSchoolManager()->persistEvaluation($evaluation);
            $this->redirectToRoute('ec_classroom_show_subject', [
                'subject' => $subject->getId()
            ]);
        }
    }

    return $this->render('classrooms/subjects/evaluations/new.html.twig', [
        'form' => $form->createView(),
        'flow' => $flow,
        'classroom' => $classroom,
        'subject' => $subject,
        'period' => $period,
        'evaluation' => $evaluation
    ]);
}

还有我的Evaluation 实体

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(
 *     repositoryClass="AppBundle\Repository\EvaluationRepository"
 * )
 */
class Evaluation
{
    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     * @Assert\Length(max=255)
     */
    private $label;

    /**
     * @var \DateTime
     * @ORM\Column(type="date")
     * @Assert\NotNull()
     * @Assert\Date()
     */
    private $date;

    /**
     * @var Collection
     * @ORM\ManyToMany(targetEntity="Knowledge")
     * @Assert\Count(min=1, minMessage="evaluation.knowledges.at_least_one")
     */
    private $knowledges;

    /**
     * @var Collection|Scale[]
     * @ORM\OneToMany(targetEntity="Scale", mappedBy="evaluation", cascade={"ALL"})
     * @Assert\Count(min=1, minMessage="evaluation.scales.at_least_one")
     * @Assert\Valid(traverse=true)
     */
    private $scales;

    /**
     * @var Subject
     * @ORM\ManyToOne(targetEntity="Subject")
     * @ORM\JoinColumn(nullable=false)
     */
    private $subject;

    /**
     * @var Period
     * @ORM\ManyToOne(targetEntity="Period")
     * @ORM\JoinColumn(nullable=false)
     */
    private $period;

    /**
     * @var Classroom
     * @ORM\ManyToOne(targetEntity="Classroom")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    private $classroom;

    /**
     * @var Composition[]|Collection
     * @ORM\OneToMany(targetEntity="Composition", mappedBy="evaluation", cascade={"all"})
     * @Assert\Valid(traverse=true)
     */
    private $compositions;

它们是要验证的更多实体(请参阅Evaluation 实体中的关系),但即使没有关联的 Evaluation 本身也不会被验证。我该怎么做才能检查每个验证规则?

【问题讨论】:

    标签: php forms validation symfony symfony-2.8


    【解决方案1】:

    找到解决方案:CraueFormFlowBundle 为流程的每个步骤创建一个验证组名称。在我的例子中,我的createEvaluation 流(流的getName 方法给出的名称)第一步的验证组被命名为flow_evaluation_create_step1。我必须在 Assert 注释中设置 groups 属性以验证权限字段(即在当前步骤中编辑的字段)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多