Skip to content

[Form][Assert\NotBlank] #[Assert\NotBlank] ignored when 'required' => false #61077

Closed as not planned
@tiffanymartin34

Description

@tiffanymartin34

Symfony version(s) affected

7.3.1

Description

When editing an entity that has a #[Assert\NotBlank] constraint on a string property, and the corresponding form field is configured with 'required' => false, submitting the form with an empty value causes an InvalidTypeException instead of triggering the validation error.

This issue only occurs during edit. During creation, the NotBlank constraint behaves as expected and shows the correct message.

How to reproduce

<?php

namespace App\Entity;

use App\Repository\MyEntityRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: MyEntityRepository::class)]
class MyEntity
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): static
    {
        $this->name = $name;

        return $this;
    }
}
<?php

namespace App\Form;

use App\Entity\MyEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MyEntityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', options: [
                'required' => false,
            ])
        ;
    }

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

<?php

namespace App\Controller;

use App\Entity\MyEntity;
use App\Form\MyEntityType;
use App\Repository\MyEntityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/my/entity')]
final class MyEntityController extends AbstractController
{
    #[Route(name: 'app_my_entity_index', methods: ['GET'])]
    public function index(MyEntityRepository $myEntityRepository): Response
    {
        return $this->render('my_entity/index.html.twig', [
            'my_entities' => $myEntityRepository->findAll(),
        ]);
    }

    #[Route('/new', name: 'app_my_entity_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $myEntity = new MyEntity();
        $form = $this->createForm(MyEntityType::class, $myEntity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($myEntity);
            $entityManager->flush();

            return $this->redirectToRoute('app_my_entity_index', [], Response::HTTP_SEE_OTHER);
        }

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

    #[Route('/{id}', name: 'app_my_entity_show', methods: ['GET'])]
    public function show(MyEntity $myEntity): Response
    {
        return $this->render('my_entity/show.html.twig', [
            'my_entity' => $myEntity,
        ]);
    }

    #[Route('/{id}/edit', name: 'app_my_entity_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, MyEntity $myEntity, EntityManagerInterface $entityManager): Response
    {
        $form = $this->createForm(MyEntityType::class, $myEntity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->flush();

            return $this->redirectToRoute('app_my_entity_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('my_entity/edit.html.twig', [
            'my_entity' => $myEntity,
            'form' => $form,
        ]);
    }

    #[Route('/{id}', name: 'app_my_entity_delete', methods: ['POST'])]
    public function delete(Request $request, MyEntity $myEntity, EntityManagerInterface $entityManager): Response
    {
        if ($this->isCsrfTokenValid('delete'.$myEntity->getId(), $request->getPayload()->getString('_token'))) {
            $entityManager->remove($myEntity);
            $entityManager->flush();
        }

        return $this->redirectToRoute('app_my_entity_index', [], Response::HTTP_SEE_OTHER);
    }
}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy