WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.106 --> 00:00:03.726 align:middle
Now that we have our form rendering,
we want to be able to submit it!

00:00:04.366 --> 00:00:07.286 align:middle
Crazy idea, I know but let's see what we can do.

00:00:07.286 --> 00:00:13.056 align:middle
We'll use our standard code for this
$form-&gt;handleRequest($request);.

00:00:13.056 --> 00:00:18.426 align:middle
Now we don't have the $request object
yet, so let's put our type hint in for it.

00:00:19.016 --> 00:00:21.956 align:middle
And like always, make sure you have
the right one and let it autocomplete

00:00:21.956 --> 00:00:23.196 align:middle
to get the use statement for you.

00:00:24.686 --> 00:00:31.596 align:middle
And real quick, plug request in here too: Below,
let's add if ($form-&gt;isValid()) with a TODO

00:00:31.596 --> 00:00:37.126 align:middle
to remind us to actually start saving stuff.

00:00:37.126 --> 00:00:39.776 align:middle
Next, there are two things we typically do here:

00:00:40.006 --> 00:00:44.396 align:middle
first add a flash message,
like "Sam would be proud".

00:00:44.926 --> 00:00:46.266 align:middle
And I truly think he would.

00:00:47.006 --> 00:00:52.166 align:middle
And second, we redirect with
return $this-&gt;redirectToRoute()

00:00:54.046 --> 00:00:56.776 align:middle
and let's send our users
to the movies_list route,

00:00:56.776 --> 00:01:01.596 align:middle
which of course we get a nice
autocomplete on: That was pretty easy,

00:01:01.596 --> 00:01:08.426 align:middle
but we'll be writing those 8 lines of code over
and over and over again inside of our project.

00:01:09.596 --> 00:01:11.446 align:middle
There must be a better way!

00:01:12.106 --> 00:01:14.906 align:middle
To fix that we are going to use
something called a "Live Template".

00:01:16.476 --> 00:01:19.526 align:middle
That's what lets us just
type a in a twig template,

00:01:19.526 --> 00:01:21.906 align:middle
hit tab and get a full a tag printed out.

00:01:23.636 --> 00:01:28.036 align:middle
We want to do the same type of thing and have
it generate all this form handling boiler plate

00:01:28.036 --> 00:01:28.766 align:middle
code for us.

00:01:29.836 --> 00:01:32.486 align:middle
To make this, first select all
the code that you want included.

00:01:33.636 --> 00:01:37.116 align:middle
Then click on the Tools dropdown menu,
and select "Save as Live Template".

00:01:40.656 --> 00:01:43.756 align:middle
Let's give this a name, like
formhandle with a description

00:01:43.756 --> 00:01:46.706 align:middle
of "Adds controller form handling code".

00:01:48.876 --> 00:01:52.136 align:middle
Inside the template text tweak the
indentation but other than that,

00:01:52.136 --> 00:01:53.666 align:middle
let's just leave this alone for now.

00:01:54.686 --> 00:01:59.036 align:middle
Hit 'ok'. Now that we have this,
we can delete our hard work.

00:01:59.506 --> 00:02:02.986 align:middle
Replace it by typing formhandle,
hitting tab, and then...

00:02:03.146 --> 00:02:05.546 align:middle
boom! Enjoy that autocompletion.

00:02:05.966 --> 00:02:08.886 align:middle
Of course now we need to
make parts of this dynamic.

00:02:09.046 --> 00:02:12.246 align:middle
We'll need to edit our live
template...how do we do that?

00:02:12.246 --> 00:02:17.936 align:middle
Well, it's probably in the preferences
menu and if we search for it, it is!

00:02:20.706 --> 00:02:24.856 align:middle
We'll assume that the variable is always
called $form because it usually is,

00:02:25.116 --> 00:02:28.516 align:middle
but we do want to change
our success message here.

00:02:28.516 --> 00:02:33.186 align:middle
Let's swap it out for '$SUCCESSMESSAGE$'
with dollar signs

00:02:33.186 --> 00:02:35.636 align:middle
on both sides and surrounded in quotes.

00:02:35.736 --> 00:02:41.936 align:middle
Let's do the same thing down here in the
redirect, we'll change that to '$ROUTENAME$'.

00:02:41.936 --> 00:02:48.016 align:middle
In the event that the route has parameters
add in array next to it for convenience.

00:02:48.136 --> 00:02:49.506 align:middle
Save this.

00:02:49.666 --> 00:02:56.066 align:middle
Get rid of the code and again
type in formhandle and tab.

00:02:56.976 --> 00:03:00.836 align:middle
Type in 'Sam would be proud', hit tab,

00:03:01.196 --> 00:03:04.946 align:middle
type movie_list for our redirect,
then tab one more time.

00:03:05.706 --> 00:03:12.936 align:middle
Now that is awesome and so much faster than
typing it all out again and again and again.

00:03:13.816 --> 00:03:17.286 align:middle
Another great spot to use Live Templates
is inside of this form row stuff

00:03:17.286 --> 00:03:19.046 align:middle
since we'll be typing this all the time.

00:03:20.106 --> 00:03:24.876 align:middle
Copy one of our rows, go to Tools
and "Save as Live Template".

00:03:26.336 --> 00:03:31.376 align:middle
We'll call this one formrow with a
description of "Renders form_row in Twig".

00:03:33.536 --> 00:03:36.836 align:middle
Below, there's a note saying
its applicable for HTML,

00:03:36.836 --> 00:03:39.956 align:middle
HTML text and HTML Twig files - so that's right.

00:03:39.956 --> 00:03:43.546 align:middle
Let's update this line of code to be form.

00:03:44.096 --> 00:03:50.246 align:middle
$FIELD$. Click ok and now we can try this out.

00:03:50.686 --> 00:03:55.576 align:middle
Type formrow, hit tab and now we can just
start typing in the field property name

00:03:55.576 --> 00:03:57.306 align:middle
and select it from the list that pops up.

00:04:00.206 --> 00:04:03.476 align:middle
Now, as great as formrow is,
sometimes you can't use it.

00:04:03.796 --> 00:04:04.786 align:middle
womp womp.

00:04:05.696 --> 00:04:08.816 align:middle
That's when you'll end up rendering a field
manually, which will probably be a div

00:04:08.816 --> 00:04:14.686 align:middle
and the three parts: {{
form_label(form.releasedAt) }}.

00:04:15.366 --> 00:04:25.026 align:middle
Copy that and update for form_widget, and
form_errors: Clearly this is also begging

00:04:25.026 --> 00:04:26.446 align:middle
to be turned into a Live Template.

00:04:27.176 --> 00:04:27.826 align:middle
You know the drill!

00:04:28.266 --> 00:04:31.666 align:middle
Select the full div, go to
Tools, Save as Live Template.

00:04:33.646 --> 00:04:39.586 align:middle
Let's name this one formrowfull, and
it "Renders widget/label/errors".

00:04:40.866 --> 00:04:44.696 align:middle
What's cool here is that there's just
one variable that's duplicated 3 times.

00:04:44.696 --> 00:04:50.556 align:middle
So we can just say $FIELD$
and repeat that each time.

00:04:54.876 --> 00:04:58.336 align:middle
Just a quick indentation fix there and click ok.

00:04:59.716 --> 00:05:05.576 align:middle
Awesome! Let's get rid of the div we
had, type formrowfull, hit the tab key,

00:05:05.786 --> 00:05:09.136 align:middle
and as we start typing the property
name we get the autocomplete

00:05:09.136 --> 00:05:11.966 align:middle
and it's filling instantly
on each of the three lines.

00:05:12.356 --> 00:05:16.206 align:middle
I'll fix the spacing here to wrap it up!

00:05:16.786 --> 00:05:19.246 align:middle
So, live templates: big win.

