@@ -171,7 +171,10 @@ class TaskController extends AbstractController
171171 $form = $this->createForm(TaskType::class, $task);
172172 $form->handleRequest($request);
173173
174- if ($form->isSubmitted() && $form->isValid()) {
174+ $submitted = $form->isSubmitted();
175+ $valid = $submitted && $form->isValid();
176+
177+ if ($valid) {
175178 $task = $form->getData();
176179 // ... perform some action, such as saving the task to the database
177180
@@ -183,12 +186,21 @@ class TaskController extends AbstractController
183186
184187 // If the client doesn't support JavaScript, or isn't using Turbo, the form still works as usual.
185188 // Symfony UX Turbo is all about progressively enhancing your apps!
186- return $this->redirectToRoute('task_success');
189+ return $this->redirectToRoute('task_success', [], Response::HTTP_SEE_OTHER );
187190 }
188191
189- return $this->render('task/new.html.twig', [
192+ // Symfony 5.3+
193+ return $this->renderForm('task/new.html.twig', $form);
194+
195+ // Older versions
196+ $response = $this->render('task/new.html.twig', [
190197 'form' => $form->createView(),
191198 ]);
199+ if ($submitted && !$valid) {
200+ $response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
201+ }
202+
203+ return $response;
192204 }
193205}
194206```
0 commit comments