Why form errors appear before the form has been submitted

27 views Asked by At

I have made my first project with Sylius and I have created a new entity the same way I do in other symfony projects. But this time when I go to contact page, the form errors appear before the form has been submitted

enter image description here

#[ORM\Entity(repositoryClass: ContactRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[ORM\Table(name: 'contact')]
class Contact
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

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

#[Assert\NotBlank]
#[ORM\Column(length: 100)]
private ?string $firstName = null;

#[Assert\NotBlank]
#[ORM\Column(length: 100)]
private ?string $lastName = null;
  
// ...
}

The controller:

    #[Route('/{_locale}/nous-contacter', name: 'front_contact')]
public function contact(Request $request, ManagerRegistry $doctrine, MessageBusInterface $bus, Captcha $captcha): JsonResponse|Response
{
    $contact = new Contact();

    $form = $this->createForm(ContactType::class, $contact);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $doctrine->getManager();

            $entityManager->persist($contact);
            $entityManager->flush();

            $contactId = $contact->getId();
            $sendCopie = $form->get('getCopy')->getData();

            $bus->dispatch(new EmailContact($contactId, $sendCopie), [new DelayStamp(10000)]);

            return $this->redirectToRoute('front_contact');

    }
    
    return $this->render('front/contact.html.twig', array(
        'form' => $form,
        'contact' => $contact
    ));
}
                        <div class="row mb-10">
                            <div class="col-6">
                                {{ form_row(form.firstName) }}
                            </div>
                            <div class="col-6">
                                {{ form_row(form.lastName) }}
                            </div>
                        </div>

                        <div class="row mb-10">
                            <div class="col-12">
                                {{ form_row(form.orderNumber) }}
                            </div>
                        </div>
                    </form>
0

There are 0 answers