symfony: dump doesn't reflect last request in controller
Symfony version(s) affected
7.2.2
Description
I have a signUp controller on which i want to implement validation.
I added annotations to my User class, and then started modifying the code of my route handler to inject validation errors into my twig template. However doing so, I realised that my validation errors were not displayed on the client side.
I tried doing several dumps and got to the point I am at now:
// SignUpController.php
final class SignupController extends AbstractController
{
#[Route('/signup', name: 'app_signup')]
public function index(EntityManagerInterface $entityManager, Request $request, ValidatorInterface $validator): Response
{
dump($request);
$form = $this->createForm(SignupType::class);
$form->handleRequest($request);
dump($form->isSubmitted());
if(!$form->isSubmitted())
return $this->render('signup/index.html.twig', [
'controller_name' => 'SignupController',
'signupForm' => $form->createView(),
]);
$user = $form->getData();
$user->setPassword(password_hash($user->getPassword(), PASSWORD_DEFAULT));
$user->setRoles(['ROLE_USER']);
$user->setAdmin(false);
$user->setActive(false);
$errors = $validator->validate($user);
dump($errors);
dump($user);
if(count($errors) > 0)
return $this->render('signup/index.html.twig', ['errors' => $errors, 'signupForm' => $form->createView()]);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success', 'Compte créé.');
return $this->redirectToRoute('app_main');
}
}
{# templates/signup/index.html.twig #}
{% extends 'base.html.twig' %}
{% block title %}Hello SignupController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Sign up!</h1>
{% include "fragments/validationErrors.html.twig" with {'errors':errors|default([])} %}
{{ form(signupForm) }}
</div>
{% endblock %}
When landing on my signup page, i only see the request dump and the form submitted state. The request method is obviously a ‘GET’ and the form submitted state is false, everything works as expected up to this point.
However, when sending an invalid form, i still only see 2 dumps, and they have the same values as before, even though when inspecting my network i see that the method of the last request is a ‘POST’, so my request dump should show #method: "POST", and $form->isSubmitted() should be true.
I suspect this might be a caching issue on symfony’s side, however I am not familiar enough with this framework. I already tried clearing both client and symfony’s cache, with no avail. any idea?
How to reproduce
- create a new symfony project using
composer create-project symfony/skeleton:"7.1.*" my_project_directory - create a User class using
php bin/console make:user - add validation constraints with
#[Assert/...]annotation, in my case i have a#[Assert\PasswordStrength]on my password field - create a SignupController and copy the code from the description in it
- create a
index.html.twigfile intemplates/signupwith the same code as in description - go to
/signuppage and inspect the dumps, send an invalid form relative to the user’s validation constraints and check the dumps again
Possible Solution
No response
Additional Context
I am using wampServer to serve my project locally
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (9 by maintainers)
@loic-eni filter forms will use
GET, notPOST(and so submitting them will change the URL)