Take another look at Bruna's CuisineController: read each field, convert the price, find the category, redisplay the submitted values… Sixty lines for a single form. And more forms are coming soon (editing a dish, leaving a review).
Symfony has a component for that: Form. You describe the form once, in a class, and Symfony takes care of the HTML, reading the request, converting values and CSRF protection.
Your mission
- Complete
App\Form\PlatType, bound to thePlat(dish) entity: fieldsnom(name),description(a text area),prix(price, a number),specialite(specialty, an optional checkbox) andcategorie(category), chosen from the database and displayed by its name. - Rewrite
CuisineController::nouveauPlat()with this form: when it is submitted and valid, give the dish a slug made from its name, save it, then redirect to its page. - Display the form in
cuisine/nouveau.html.twig, with an “Add to the menu” button.
Reminder
$form->handleRequest($request) fills the Plat object with the submitted data. You no longer have to read $request->request yourself. The hidden _token field protects the form against requests forged from another site (CSRF): Symfony checks it on its own.
Data validation (a required name, a positive price…) is the topic of the next exercise.