0:00
Hi I'm ChristopherCodeviewVidio.com and in this video we're continuing on with our contact form implementation
0:11
Towards the end of the previous video we were able to send a message and when we sent it it would arrive inside our griller mail or any other mail that you've got set up
0:20
We could see information being dumped out onto the web debug toolbar
0:24
We could see mails travelling through our system. We'd touched on some twig template in and we'd covered our first controller actually
0:29
our first controller action. So to begin with, in this video, we're gonna start off by extracting out
0:34
the creation of this form, moving the various fields into their own form type
0:39
And a form type is a PHP class that describe all the things that make up our form
0:44
such as the form's name, any fields that are on there, and any options or requirements for those fields
0:49
And why we do this is to enable us to reuse forms throughout our project
0:54
which as we'll see, almost immediately, is something that we would need to do in this instance
0:58
So I'm going to start off from the app bundle. I'm going to create a new file under form type
1:04
So those are the two directories. It's going to be the form directory and then a sub-directory of type
1:09
And I'm going to call this the contact form type. Now I'm not suggesting that you need to memorize this
1:15
I'm going to type it out, but you can simply copy and paste this from the documentation
1:19
And in truth I do that a lot too We need to extend abstract type which in PHP storm will bring in the use statement for us and then we going to have one public function called build form
1:31
which has two arguments, the form builder interface, which we'll call builder, and then an array of options
1:38
Inside here, we'll have builder, and then simply we'll just take these out and paste them in
1:45
making sure to close off with the closing semicolon and then we just need to make sure
1:51
that we've got the appropriate use statements for each of these as well. Which again, PHP Storm is pretty good at sorting out for us
1:58
If we've removed them from there though, we can also remove the use statements
2:02
And now, rather than using the create form builder, for our form, we are simply going to create form
2:08
and passing the name of the form that we wish to create, which in this case is the contact form type
2:13
That should be good enough. What I'm going to do also is I'm going to jump into config dev
2:17
because we're going to work in the development environment more than in the production environment
2:22
And under Swift Mailer, I'm just going to un-comment Swift Mailer, and I'm going to disable delivery
2:27
So what that means is when we send an email, it's not actually going to get sent when we're in the development environment or app dev
2:32
So to get to the app dev or the development environment, we just need to go to app underscore dev
2:38
And you may find when you do that that you get an error that says your IP address is not
2:42
valid or whatever. It entirely depends on how you work. with Symphony If you do see that error go on the web app dev and add in your IP address after this one is the IP6 version of the local host Again it entirely depends on how you working You may not need to do that I don because I running the web server locally
3:01
And I'm just going to go ahead and close all of those down for the moment. It's getting a little bit messy
3:06
So I'll go ahead and open up the support controller again. And whilst we haven't changed anything in terms of functionality, we have changed some code
3:13
So let's try it and send through a message now. Now we can send in anything at this point
3:17
because we're not actually going to be sending this mail for real. So it's whacking any old nonsense
3:22
Send that through and we can see one mail was sent. We didn't get any errors. So it all looks good
3:27
Now the next thing that I want to do is get rid of all of this out of the index action
3:32
As really this action is doing a bit too much now. I want to move the logic of handling a forum submission out into its own controller action
3:40
So that's as simple as creating a new controller action. And any controller action just needs to follow the same pattern where we give it any
3:47
name starting with a lowercase and ending in action so in my case I'm just going to call this
3:51
handle form submission action which will need the request and I'll go ahead and set this up properly
3:59
as well and say it's going to be the root of slash form submission you can call it literally
4:05
anything you want no one's going to directly access this anyway and we're going to restrict them
4:09
from being able to do that that we don want our users to actually be able to access this route directly so I say handle form submission is the name of our route And then we will use the method annotation to say that we only accept post requests to this
4:24
route. So what I'm going to do is I'm going to take a full copy of all of that. In fact, I'm just
4:29
going to cut it out. I'm going to paste that in. Rather than cut that out there, I'm going to copy
4:35
that. The one downside is that we need to declare the form twice in this instance. But that's
4:39
not that big of a deal because we've just extracted the form out into its own contact form type
4:44
So no major problems really there. Technically at this point we could get rid of the request as we're not using it
4:51
So let's do so. And then one thing that I like to do is get rid of any nested ifs like this
4:58
So whilst we've got here that it says if form submitted and form is valid, then do this thing inside the if block
5:04
what I like to do is instead say if the form is not valid or the form is not suburb
5:09
or vice versa, then we will return this redirect to route and we're going to redirect to the
5:15
homepage. So I can close off that conditional now and then I can unendent all of that and that just
5:22
frees my sort of mental cycles up in a way from having to worry about this if statement
5:28
Because I know now if we get past there then our form has been submitted and it is also valid
5:34
You may disagree with that and that's absolutely fine but that's just the way I like to work