简体   繁体   中英

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. 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 .

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

<?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. 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. 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 . 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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