Symfony 4.4 FormType problem with validate

I have a problem with my validation form when I submit there is no error message just refresh the page and I don’t understand what I have forget? I tried with SubmitType but I have the same problem.

My FormType

class FilmType extends AbstractType
{
   public function buildForm(FormBuilderInterface $builder, array $options)
   {

    $builder
        ->add('titre')
        ->add('resume')
        ->add('dateSortie')
        ->add('personnages', EntityType::class, [
            'class' => Personnage::class,
            'query_builder' => function (PersonnageRepository $pr) {
                return $pr->createQueryBuilder('p')
                    ->orderBy('p.nom', 'ASC');
            },
            'choice_label' => 'nom'
        ])
        ->getForm();
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Film::class,
    ]);
}

}

My Controller, I think is ok

 /**
 *
 * @param Request $request
 * @Route("/film/new", name="film_new")
 * @return Response
 *
 */
public function new(Request $request): Response
{
    $film = new Film();
    $form = $this->createForm(FilmType::class, $film);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($film);
        $entityManager->flush();

        return $this->redirectToRoute('home');
    }

    return $this->render('film/new.html.twig', [
        'film' => $film,
        'form' => $form->createView(),
    ]);
}

MY Twig, I add form_errors but nothing more

{% block body %}
    <form>
        <div class="form-group">
            {{ form_start(form)}}
            {{ form_errors(form) }}
            {{ form_widget(form) }}
            <button> Envoyer </button>
            {{ form_end(form) }}
        </div>
    </form>
{% endblock %}

My Film entity…..

/**
 * @ORMEntity(repositoryClass="AppRepositoryFilmRepository")
 */
class Film
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $titre;

    /**
     * @ORMColumn(type="text")
     */
    private $resume;

    /**
     * @ORMColumn(type="date")
     */
    private $dateSortie;

    /**
     * @ORMManyToMany(targetEntity="AppEntityPersonnage", mappedBy="film")
     */
    private $personnages;

    public function __construct()
    {
        $this->personnages = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitre(): ?string
    {
        return $this->titre;
    }

    public function setTitre(string $titre): self
    {
        $this->titre = $titre;

        return $this;
    }

    public function getResume(): ?string
    {
        return $this->resume;
    }

    public function setResume(string $resume): self
    {
        $this->resume = $resume;

        return $this;
    }

    public function getDateSortie(): ?DateTimeInterface
    {
        return $this->dateSortie;
    }

    public function setDateSortie(DateTimeInterface $dateSortie): self
    {
        $this->dateSortie = $dateSortie;

        return $this;
    }

    /**
     * @return Collection|Personnage[]
     */
    public function getPersonnages(): Collection
    {
        return $this->personnages;
    }

    public function addPersonnage(Personnage $personnage): self
    {
        if (!$this->personnages->contains($personnage)) {
            $this->personnages[] = $personnage;
            $personnage->addFilm($this);
        }

        return $this;
    }

    public function removePersonnage(Personnage $personnage): self
    {
        if ($this->personnages->contains($personnage)) {
            $this->personnages->removeElement($personnage);
            $personnage->removeFilm($this);
        }

        return $this;
    }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *