|
| 1 | +# Rendering the Form with Twig Helper Functions |
| 2 | + |
| 3 | +In the last chapter, we dived into the Symfony Form component, built our |
| 4 | +first Form type, and created a Form object from the type in the controller. |
| 5 | + |
| 6 | +Now, let's move on to the fun part — bringing it all to life by |
| 7 | +rendering the form. The Symfony Form component packs a bunch of neat helper |
| 8 | +functions that make form rendering a breeze. Open your template and swap |
| 9 | +out the dump with this line of code: `{{ form(form) }}` |
| 10 | + |
| 11 | +Now, head back to your browser and refresh the page. Voila! We have a form. |
| 12 | +Sure, it might not win any beauty pageants, but as backend developers, |
| 13 | +we're not too worried about appearances just yet. Let's focus on |
| 14 | +functionality first, and we'll take care of the styling later. |
| 15 | + |
| 16 | +## Understanding Form Submission Methods |
| 17 | + |
| 18 | +If you open your inspector, you'll notice something interesting. The form |
| 19 | +is sent via POST method. Symfony defaults to POST for forms, although you can |
| 20 | +override this. We'll explore how to send a form via GET later on. |
| 21 | + |
| 22 | +The action attribute is empty. This means the form submits to the same URL, |
| 23 | +which is incredibly convenient when you need to use the same form across |
| 24 | +different pages that submit to various places. Sure, you could specify an |
| 25 | +action explicitly, but most of the time, it's not necessary. |
| 26 | + |
| 27 | +## Styling with Tailwind CSS |
| 28 | + |
| 29 | +Since our project uses Tailwind CSS, let's activate the Tailwind Forms |
| 30 | +plugin for some reasonable styling defaults. Back in PhpStorm, open |
| 31 | +`assets/styles/app.css`, and in the beginning add |
| 32 | +`@plugin "@tailwindcss/forms";` |
| 33 | + |
| 34 | +Don't forget the semicolon at the end. This plugin provides a neat minimal |
| 35 | +reset for form controls, making them easier to style with Tailwind classes. |
| 36 | +That's designer's favorite feature! |
| 37 | + |
| 38 | +Refresh your browser, and you'll notice a significant improvement. Our |
| 39 | +form has gone from a 90s throwback to a modern developer tool. |
| 40 | + |
| 41 | +Now, let's add a minor detail to make our form fields blend better with the |
| 42 | +background. In the CSS file, add this bit: |
| 43 | + |
| 44 | +```css |
| 45 | +input, textarea, select { |
| 46 | + background-color: inherit; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Adding a Submit Button |
| 51 | + |
| 52 | +Buuuut we've got a tiny issue. Our beautiful reset form is missing the most |
| 53 | +important element - a submit button. How do we add one? The best practice |
| 54 | +is to add your submit button manually in Twig, not inside the form type. |
| 55 | + |
| 56 | +Let's create a simple button. Back in the Twig template, below the form, add: |
| 57 | +`<button type="submit"></button>`. I will add some Tailwind CSS classes |
| 58 | +to make it look prettier like a real stylish button. You can copy/paste |
| 59 | +that long list of CSS classes from the scripts below the video. |
| 60 | + |
| 61 | +Remember, this button must be inside the `form` tag; otherwise, no matter how |
| 62 | +many times you click or how hard you will press your touchpad, it just won't |
| 63 | +do anything. So, we'll switch from rendering the entire form at once |
| 64 | +to rendering it piece by piece. |
| 65 | + |
| 66 | +Replace the form tag with `{{ form_start(form) }}`, add `{{ form_end(form) }}` |
| 67 | +below it, and put a special `{{ form_widget(form) }}` between them to |
| 68 | +render all the form fields. |
| 69 | + |
| 70 | +## A Quick Note About disabled Turbo |
| 71 | + |
| 72 | +By the way, I’ve temporarily disabled Turbo Drive globally in this project. |
| 73 | +You’ll see some commented-out Turbo code in `assets/app.js`. |
| 74 | + |
| 75 | +Make sure you also have Turbo disabled so your behavior matches what you see |
| 76 | +in the videos. Why? Because with Turbo enabled, page navigation happens |
| 77 | +via AJAX requests. And I know, that sounds crazy, but even form submissions |
| 78 | +become AJAX! That can make debugging harder when learning how forms work. |
| 79 | +For now, plain old full-page loads keep things simple and predictable. |
| 80 | + |
| 81 | +## Testing our Form |
| 82 | + |
| 83 | +Finally, it's time to test our form. Fill out the form and hit that |
| 84 | +"Create" button! Did it work?! |
| 85 | + |
| 86 | +As of now, we're not doing anything with the submitted data internally — no |
| 87 | +saving, no validating, no redirecting. You'll see this when you try to |
| 88 | +reload the page, and your browser asks if it should resubmit the form. |
| 89 | + |
| 90 | +But don't worry, we'll cover how to process the form and store the new |
| 91 | +StarshipPart in the database in the next video. Stay tuned! |
0 commit comments