表格未在Symfony 2.8中验证

时间:2016-10-03 13:39:57

标签: php forms validation symfony symfony-2.8

我有一个基于Symfony 2.8的proejct,我有一个用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本身也未经验证。如何检查每个验证规则?

1 个答案:

答案 0 :(得分:0)

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