Hello and welcome.
In this picture we are going to add some validation to our form and we are going to add that in to our
post model.
So let's expand our Dairy Queen here and go to apps on lalley Apps.
Those models are a cream.
Inside there is a Post dot R B.
So this is what the model looks like under my meds.
Fairly basic.
There isn't much to defile.
Let's just explain a few things here.
Post class.
You can see here in inherits it's from the Application Record the Application Record inherits from the
active record base.
So this is the active record base and that's the application recaught.
So this is a post model and this is application.
Record and the Application Record in here is from this here.
While the post clasp inherits from that.
Let's take a quick look.
The application recaught saw this active record base here.
And what that does it actually supplies a great deal of functionality to your real models.
It does this for free.
It includes things like allows you to do kuroda operation so you can do create.
You can read an obsolete.
You can delete or destroy.
He allows you to perform all those operations you also assist with forn validation as well.
So let's go back into the post model here.
Rails includes methods that helps you validate the data that you send to your models.
We just don't want to send any type of data to the model.
We want to validate what we are able to send to the models.
So inside here we are going to apply or implement some validation here too so that it enforces what
you are not allowed to post.
So to implement the validation or you need to do is typing validate.
So we're just trying to specify what the form should validate it also validates and then do as face
call on we want that title.
Okay.
Comma and then presses means we want the process off a title and we set that value to be true.
So do a space and do True.
Next thing we want to do we want to specify the length of that okay so we're typing Len.
K and we do a call on.
I'm going to specify the minimum length of character that is going to be allowed for the title.
So you can specify any and so you type p.m. on.
Then a call on space and they you determine what the minimum character should be in my is I'm just going
to see for I'm happy for.
But you put whatever value you want to what that means when you enter a title for the post.
It has to be a minimum of four character.
That's basically what that EDITION.
I'm not going to go file an UNDO save all you can validate a variety of things at the moment we've Wallace
set the validation to the title so that he does set a minimum of four character long saw you can also
validate things like the presents or the uniqueness of column the format of the column the existence
of arsaell see it at objects ansorge.
So there's lots of things that you can specify where the validation for.
Now we're just going to keep it simple and just validate the title for now with the validation line
place that we have to go back to our post controller and we have to call the Post dot sleeve so that
when the when you press the same button on the form it all validates the.
Cool back to our Controller we can control click Post.
We want to go in to this say actions fellas quote With this save action here.
Here we are going to add in an IF statement there.
So implement your statement first and then I will explain why.
Same thing if.
The post was posted save what it is say it will return in force and if you redirect it.
You can redirect it to the post.
So what this is saying was just submit the form.
Once the validation is so key if the validation is OK it will redirect to the show view.
But if every time you implement an if statement is always good to add an else for the other condition.
So this is if the validation is OK.
This is what it will do.
However the validation is not to say we want to implement something else.
If the validation is not talk we want to render a new king and then we press end to end that.
So what I want to do is wrap this eve steadman Rapid round it and see
wrap that there and wrap that.
So what should happen when the few press the see Button.
It will check if everything is validated okay.
It will redirect to the post for you.
OK if nothing happens that is a sure view.
And if that's not the case everything here doesn't go OK it will fall onto this L statement and render
the form again so render the form you give you the new form again to try again.
So let's save that and have a look at the form hopefully out for mr who run in.
K. So now let's try and say that if we saved that we should get some validation warning if you notice
this.
No.
No error messages here.
He just returns back to the form.
So we need to add SOM or k..
We've course on error here.
Let's see was same is same with the more.
Okay.
Got an error here so I need to go back to the model and check that out.
So I think the the error here I should have had a comma there.
So let's put a comma there and safe.
Because it doesn't await any US wives complaining.
Let's go back and refresh hatch.
Hopefully she'd be all right now.
Okay.
Yeah.
So now let's try and see.
This portrait kit at the moment.
We try and save it it returns us to it gives us the form the new form is gabry renders the form.
So what I want to do in the next lecturer is try to implement som error or handling.
So we know what's actually happening.
So if you SOB if you save the pause he'll tell you there's any error messages at the moment is just
re rendering the form which is what I've specified here.
So that's what is in here.
If posed.
If you press that he redirect you to depose show or else he renders a new form read.
Does the former.
Which is what he's doing.
So we need to implement error or handling in the next lecture.
So thanks for watching.
Bye for now.