[英]Form not validating in Symfony 2.8
我有一个基于Symfony 2.8的程序,并且有一个用CraueFormFlowBundle
创建的表单,该表单允许创建多步骤表单。 我创建了流程类,并为每个步骤创建了中间表单类型,所有工作都在此级别上进行。 唯一不起作用的是,无论是在每个步骤之间还是在表单流的结尾,都不会触发单个验证规则。 我在实体中使用注释来验证数据。 我已经在config.yml
中检查了是否enabled
了validation
和form
组件,并将framework.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
本身也不会得到验证。 如何检查所有验证规则?
找到了解决方案: CraueFormFlowBundle
为流程的每个步骤创建一个验证组名称。 在我的情况下,我的createEvaluation
流(该流的getName
方法给定的名称)的第一步的验证组名为flow_evaluation_create_step1
。 我必须在Assert
批注中设置用于验证权限字段的groups
属性(即,在当前步骤中编辑过的属性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.