简体   繁体   English

表格未在Symfony 2.8中验证

[英]Form not validating in Symfony 2.8

I have a Symfony 2.8 based proejct, and I have a form, created with CraueFormFlowBundle , which allows creation of multi-step forms. 我有一个基于Symfony 2.8的程序,并且有一个用CraueFormFlowBundle创建的表单,该表单允许创建多步骤表单。 I created my flow class, and intermediary form types for each step, everything works at this level. 我创建了流程类,并为每个步骤创建了中间表单类型,所有工作都在此级别上进行。 The only thing that is not working is that not a single validation rule is triggered, whether between each step or at the end of the form flow. 唯一不起作用的是,无论是在每个步骤之间还是在表单流的结尾,都不会触发单个验证规则。 I'm using annotations in my entities to validate data. 我在实体中使用注释来验证数据。 I've checked in my config.yml that both validation and form components are enabled and framework.validation.enable_annotations is set to true . 我已经在config.yml中检查了是否enabledvalidationform组件,并将framework.validation.enable_annotations设置为true

Here's my controller : 这是我的控制器:

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

and my Evaluation entity 和我的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;

They are more entities to validate (see relationships in the Evaluation entity), but not even the Evaluation itself, without associations, is not validated. 它们是更多要验证的实体(请参阅Evaluation实体中的关系),但是甚至没有关联的Evaluation本身也不会得到验证。 What can I do to have every validation rule to be checked ? 如何检查所有验证规则?

Found the solution : CraueFormFlowBundle creates, for each step of the flow, a validation group name. 找到了解决方案: CraueFormFlowBundle为流程的每个步骤创建一个验证组名称。 In my case, the validation group of the 1st step for my createEvaluation flow (the name given by the getName method of the flow) is named flow_evaluation_create_step1 . 在我的情况下,我的createEvaluation流(该流的getName方法给定的名称)的第一步的验证组名为flow_evaluation_create_step1 I had to set, in the Assert annotation, the groups property for the rights fields to validate (ie. the one who are edited in the current step). 我必须在Assert批注中设置用于验证权限字段的groups属性(即,在当前步骤中编辑过的属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM