Skip to content

Commit 49b2341

Browse files
feat(guestbook): Add a guestbook ^^ to compare form handling
1 parent 412b345 commit 49b2341

File tree

8 files changed

+151
-1
lines changed

8 files changed

+151
-1
lines changed

assets/styles/app.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ input, textarea {
102102
.btn--secondary {
103103
@apply bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded;
104104
}
105-
105+
textarea, input[type=text], input[type=email], input[type=search] {
106+
@apply border-2 hover:bg-gray-100 text-gray-800 py-2 px-4 rounded;
107+
}
106108
h1 {
107109
@apply text-5xl;
108110
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
use App\Form\GuestbookType;
8+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\Routing\Attribute\Route;
12+
13+
class GuestbookController extends AbstractController
14+
{
15+
#[Route('/guestbook', methods: ['POST', 'GET'])]
16+
public function index(Request $request): Response
17+
{
18+
$symfonyWayForm = $this->container->get('form.factory')->createNamed('sf_guestbook', GuestbookType::class);
19+
$symfonyWayForm->handleRequest($request);
20+
if ($symfonyWayForm->isSubmitted() && $symfonyWayForm->isValid()) {
21+
// Save
22+
return $this->redirectToRoute('app_guestbook_success');
23+
}
24+
25+
$htmxWayForm = $this->container->get('form.factory')->createNamed('htmx_guestbook', GuestbookType::class);
26+
$htmxWayForm->handleRequest($request);
27+
if ($htmxWayForm->isSubmitted() && $htmxWayForm->isValid()) {
28+
// Save
29+
$response = $this->render('guestbook/success.html.twig');
30+
31+
// Not mandatory, only to be compliant with the "old way".
32+
$response->headers->set('HX-Push-Url', $this->generateUrl('app_guestbook_success'));
33+
34+
return $response;
35+
}
36+
37+
return $this->render('guestbook/index.html.twig', [
38+
'symfonyWayForm' => $symfonyWayForm,
39+
'htmxWayForm' => $htmxWayForm,
40+
]);
41+
}
42+
43+
#[Route('/guestbook/success', methods: ['POST', 'GET'])]
44+
public function success(Request $request): Response
45+
{
46+
return $this->render('guestbook/success.html.twig');
47+
}
48+
}

src/Form/GuestbookType.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Form;
4+
5+
use App\Entity\Message;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
8+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
9+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
10+
use Symfony\Component\Form\FormBuilderInterface;
11+
use Symfony\Component\Validator\Constraints\Email;
12+
use Symfony\Component\Validator\Constraints\NotBlank;
13+
14+
class GuestbookType extends AbstractType
15+
{
16+
public function buildForm(FormBuilderInterface $builder, array $options): void
17+
{
18+
$builder
19+
->add('content', TextAreaType::class, [
20+
'label' => 'Your message',
21+
'constraints' => [
22+
new NotBlank(),
23+
]
24+
])
25+
->add('email', EmailType::class, [
26+
'label' => 'Email',
27+
'constraints' => [
28+
new NotBlank(),
29+
new Email()
30+
]
31+
])
32+
->add('submit', SubmitType::class, [
33+
'label' => 'Send',
34+
])
35+
;
36+
}
37+
}

tailwind.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
content: [
44
"./assets/**/*.js",
55
"./templates/**/*.html.twig",
6+
"./vendor/symfony/twig-bridge/Resources/views/Form/tailwind_2_layout.html.twig"
67
],
78
theme: {
89
extend: {},

templates/base.html.twig

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
Emoji Search
4747
</a>
4848
</li>
49+
<li>
50+
<a href="{{ path('app_guestbook_index') }}" class="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent">
51+
Guestbook
52+
</a>
53+
</li>
4954
{% if app.user %}
5055
<li>
5156
<a href="{{ path('app_logout') }}" class="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent">

templates/guestbook/index.html.twig

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block title %}Guestbook{% endblock %}
4+
5+
{% form_theme symfonyWayForm 'tailwind_2_layout.html.twig' %}
6+
{% form_theme htmxWayForm 'tailwind_2_layout.html.twig' %}
7+
8+
{% block javascripts %}
9+
{{ parent() }}
10+
<meta name="htmx-config" content='{
11+
"responseHandling":[
12+
{"code":"422", "swap": true},
13+
{"code":"[45]..", "swap": false, "error":true},
14+
{"code":"...", "swap": true}
15+
]
16+
}' />
17+
{% endblock %}
18+
19+
{% block body %}
20+
<div class="grid grid-cols-2 gap-6">
21+
<div>
22+
<h2>Symfony Way</h2>
23+
24+
<form action="{{ path('app_guestbook_index') }}" method="post" class="mt-5" novalidate>
25+
{{ form_row(symfonyWayForm.content) }}
26+
{{ form_row(symfonyWayForm.email) }}
27+
28+
{{ form_row(symfonyWayForm.submit, {'attr': {'class': 'btn'}}) }}
29+
30+
{{ form_rest(symfonyWayForm) }}
31+
</form>
32+
</div>
33+
<div>
34+
<h2>HTMX</h2>
35+
36+
<form hx-boost="true" action="{{ path('app_guestbook_index') }}" method="post" class="mt-5" novalidate>
37+
{{ form_row(htmxWayForm.content) }}
38+
{{ form_row(htmxWayForm.email) }}
39+
40+
{{ form_row(htmxWayForm.submit, {'attr': {'class': 'btn'}}) }}
41+
42+
{{ form_rest(htmxWayForm) }}
43+
</form>
44+
</div>
45+
</div>
46+
{% endblock %}

templates/guestbook/success.html.twig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block title %}Guestbook success{% endblock %}
4+
5+
{% block body %}
6+
<p class="text-7xl">
7+
Thank you! 🤗 🥰
8+
</p>
9+
{% endblock %}

templates/spa/messages/message.html.twig

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends 'base.html.twig' %}
22

3+
{% form_theme form 'tailwind_2_layout.html.twig' %}
4+
35
{% block body %}
46
{{ form_start(form, {'attr': {
57
'hx-post': path('app_spa_message'),

0 commit comments

Comments
 (0)