Bonjour,

J'ai un nouveau projet symfony 6.4, je viens de démarrer le CRUD sans bundle mais les message d'erreur ne s'affichent pas.. Voici les extraits de code concernés. Savez vous pourquoi je rencontre ce probleme?

Merci d'avance pour votre aide.

L'entité Tag

<?php

namespace App\Entity;

use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

#[ORM\Entity(repositoryClass: TagRepository::class)]
#[UniqueEntity(fields:['label'], message: 'Ce tag existe déjà!')]
class Tag
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255, unique: true)]
    private ?string $label = null;

    #[ORM\ManyToMany(targetEntity: Video::class, mappedBy: 'tag')]
    private Collection $videos;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $slug = null;

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

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

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(string $label): static
    {
        $this->label = $label;

        return $this;
    }

    /**
     * @return Collection<int, Video>
     */
    public function getVideos(): Collection
    {
        return $this->videos;
    }

    public function addVideo(Video $video): static
    {
        if (!$this->videos->contains($video)) {
            $this->videos->add($video);
            $video->addTag($this);
        }

        return $this;
    }

    public function removeVideo(Video $video): static
    {
        if ($this->videos->removeElement($video)) {
            $video->removeTag($this);
        }

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(?string $slug): static
    {
        $this->slug = $slug;

        return $this;
    }
}

_form.html.twig

{% form_theme form 'partials/_customForm.html.twig' %}

{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.label) }}
{{ form_errors(form.label) }}
<button type='submit' class="rounded-md bg-green-600 px-3 mt-10 py-2 text-sm font-semibold text-white shadow-sm hover:bg-green-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-green-600">{{button_label|default('Ajouter')}}</button>
{{ form_end(form) }}

La méthode add du TagController

    #[Route('/ajout', name: 'add')]
    public function add(EntityManagerInterface $em, Request $request, SluggerInterface $slugger)
    {
        $tag = new Tag();
        $form = $this->createForm(TagType::class, $tag);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

            $tag = $form->getData();
            $slug = strtolower($slugger->slug($tag->getLabel()));

            $tag->setLabel($tag->getLabel());
            $tag->setSlug($slug);

            $em->persist($tag);
            $em->flush();

            $this->addFlash('success', 'Le tag a bien été ajouté!');

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

        return $this->render('admin/tag/add.html.twig', [
            'form' => $form->createView(),
        ]);

    }

le theme du formulaire:

{% use 'tailwind_2_layout.html.twig' %}

{%- block form_row -%}
    {%- set row_class = row_class|default('flex flex-col') -%}
    {{- parent() -}}
{%- endblock form_row -%}

{%- block widget_attributes -%}
    {%- set widget_class = widget_class|default('rounded-md bg-gray-100 focus:border-th-noir focus:ring-th-noir') -%}
    {%- set widget_disabled_class = widget_disabled_class|default('my disabled widget classes') -%}
    {%- set widget_errors_class = widget_errors_class|default('') -%}
    {{- parent() -}}
{%- endblock widget_attributes -%}

{%- block form_label -%}
    {%- set label_class = label_class|default('text-th-noir') -%}
    {{- parent() -}}
{%- endblock form_label -%}

{%- block form_help -%}
    {%- set help_class = help_class|default('my label classes') -%}
    {{- parent() -}}
{%- endblock form_help -%}

{%- block form_errors -%}
    {%- set error_item_class = error_item_class|default('my error item classes') -%}
    {{- parent() -}}
{%- endblock form_errors -%}

Ce que je veux

Je veux simplement afficher les message d'erreur

Ce que j'obtiens

Par exemple pour l'entité Tag, quand j'essaie d'ajouter un Tag qui existe déjà en BDD l'ajout est bien empéché mais le message associé n'est pas affiché.

Aucune réponse