簡體   English   中英

表格未在Symfony 2.8中驗證

[英]Form not validating in Symfony 2.8

我有一個基於Symfony 2.8的程序,並且有一個用CraueFormFlowBundle創建的表單,該表單允許創建多步驟表單。 我創建了流程類,並為每個步驟創建了中間表單類型,所有工作都在此級別上進行。 唯一不起作用的是,無論是在每個步驟之間還是在表單流的結尾,都不會觸發單個驗證規則。 我在實體中使用注釋來驗證數據。 我已經在config.yml中檢查了是否enabledvalidationform組件,並將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM