Skip to content

Commit b244ed0

Browse files
dunglastgalopin
authored andcommitted
Turbo: improve docs for forms
1 parent ea80287 commit b244ed0

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Swup/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The main usage of Symfony UX Swup is to use its Stimulus controller to initializ
3838
<html lang="en">
3939
<head>
4040
<title>Swup</title>
41-
41+
4242
{% block javascripts %}
4343
{{ encore_entry_script_tags('app') }}
4444
{% endblock %}
@@ -66,7 +66,7 @@ additional containers, for instance to have a navigation menu that updates when
6666
<html lang="en">
6767
<head>
6868
<title>Swup</title>
69-
69+
7070
{% block javascripts %}
7171
{{ encore_entry_script_tags('app') }}
7272
{% endblock %}

src/Turbo/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)