Exercise 6 of 8 in chapter 1
· 150 XP
· 4 objectives checked by tests
Topics covered:POSTRedirectFlash messages
Announcing the table is nice. Booking it is better. Gorm drew up the form (already in the template): it sends a nom (name) and a number of personnes (people) via POST. Your job is to handle it.
Your mission
In ReservationController::index():
Also accept the POST method on the app_reservation route.
On submission, check the data:
the name is required → otherwise the error “Enter your name.”;
from 1 to 12 adventurers → otherwise “The number of adventurers must be between 1 and 12.”.
On error, show the form again (keeping the submitted values) with the status code 422.
If everything is fine, add the success flash message “Table booked for Gimli (3 adventurers)!” (with the submitted name and number), then redirect to the reservation page.
The template expects the variables personnes, nom and erreur (error, or null).
Why redirect?
This is the POST → redirect → GET pattern: without it, an adventurer who refreshes the page would submit the form again… and book twice. The flash message, stored in the session, survives the redirect and then disappears.