‫All right guys.
‫So now we want to get ready to submit.
‫A user to the database when they fill out this register form.
‫So I'm probably going to split this into two videos where with this one we'll handle the validation
‫with the form.
‫So basically we're going to get the data.
‫We're going to create the route to submit to get the data, validate it, load the view if there's errors
‫and show the errors in the form.
‫I also want to create an errors partial because we're using the same code in multiple places.
‫So one thing that I screwed up, you guys might not have when I was showing you in the validation class.
‫So framework validation and I was showing you the different filters that are available, I left in validate
‫URL for this email method, which obviously is not correct.
‫So if you did that as well change that to email.
‫All right.
‫Now, when we submit this form, let's go to the users views users Create view.
‫And we can get rid of this comment here.
‫So this form doesn't have any attributes.
‫We want to make sure that it's submitted as a post.
‫So we want to say method equals post.
‫And then the action or the route that we want to submit to is going to be slash auth slash register.
‫Okay.
‫So a Post request to this URL.
‫So let's add that to our route.
‫So we'll go to our routes dot PHP and down at the bottom here let's say router.
‫And if it's a Post request to slash auth slash register.
‫Then we're going to load a controller method.
‫So user controller and it's going to be the store.
‫Okay.
‫Just like we did with listings.
‫So create right.
‫The create get request is meant to load the form show the form and then the post request to store is
‫meant to submit the form whether it's users listings or whatever.
‫So now let's go to our user controller and let's create that method okay.
‫Make sure you're within the class.
‫And let's say store user in database.
‫And return will be void.
‫Public function.
‫Store.
‫And just for now, we'll do an inspect and die and just say store, just to make sure that the form
‫is actually hooked up to this method.
‫So I'll reload the form register and we see store.
‫Good.
‫Now we're going to be.
‫Doing some validation.
‫And obviously we need the fields, the data that's submitted in the form.
‫So let's do that.
‫Next I'm going to go into the store method and let's say name.
‫And I'm just going to set all the post variables into a regular variable.
‫So we got name.
‫And then we got.
‫Email.
‫So we'll call this email.
‫Then we got what?
‫So name.
‫Email.
‫City.
‫State.
‫Password.
‫Confirmation.
‫So.
‫City.
‫City and state.
‫And password.
‫And we have one more.
‫So this is password underscore confirmation.
‫Is the is the name attribute in the form.
‫So we'll call this password.
‫Confirmation.
‫All right.
‫And then I'm going to create the errors array.
‫So we're going to do this just like we did with the listing.
‫Now, as far as validations, there's a couple of things we want to validate.
‫First, we'll do the email.
‫Let's just put a comment here.
‫So this is going to be all the validation.
‫So if and we're going to say if not validation.
‫So that's our class.
‫And then we're going to use the email method and pass in the email.
‫From the form.
‫And say, if that's not true, right?
‫If that's not validated or if that's not a valid email, then we're going to take the errors away and
‫add email as the key.
‫And then for the value, whatever message you want to display, we'll say please enter a valid email
‫address.
‫All right.
‫Now, before we add any other validations, let's let's make it so that if there's errors the view reloads
‫and shows those errors.
‫So down here we'll say if.
‫Say, if not empty.
‫Errors.
‫Then we're going to load the view of users slash create which is our register form.
‫And then we're going to pass in.
‫Errors.
‫Okay, so we can show the errors in the view.
‫We also want the user data.
‫And there's a few ways you could do this.
‫I'm going to just send in user.
‫So it will be a variable called user that will be an associative array with the name.
‫And also the email.
‫Also the city.
‫And also the state.
‫I'm not going to do the password.
‫Okay.
‫And semicolon there.
‫Good.
‫And we'll do an exit as well.
‫So right now, what will happen if if the email address isn't valid is it'll just load the view.
‫See, we don't see any errors because we haven't handled that yet.
‫If I do enter a valid email.
‫And if if I showed it to you, leaving it blank.
‫But that's not valid either, so that will just reload.
‫So let's say test at Test.com.
‫And now that submits okay.
‫And it just shows store.
‫Actually let's put an else here.
‫And we'll put this in here.
‫All right.
‫Now to show the errors.
‫Let's go to the view.
‫So users create view.
‫And we're going to be using the same code that we used in the listings create which is this.
‫We also use it in the edit form.
‫So I think that this is a good case for for a partial.
‫So let's actually cut this from the listings create.
‫And what we want to be able to do is then say load partial.
‫Like that and we'll say we'll call it errors.
‫The partial we haven't created it yet, but we will.
‫Now we have to send in the errors here and the load partial.
‫Actually let me just show you first what we'll be doing.
‫So we want to send in an array with errors set to errors.
‫And we only want to do this if the errors exist.
‫So we'll just add the null coalescing with an empty array.
‫So we want to do that.
‫However load partial.
‫You can see in right here it doesn't take in data like the load view does.
‫But we can change that very easily by going to our helpers PHP.
‫And just like here where load view takes in data and then extracts it so you have access to those variables,
‫we'll do the same thing here.
‫So let's say data empty array by default down here.
‫Let's say right before it's required we're going to extract.
‫That data array.
‫Okay, so now we can pass data in.
‫So let's create the partial.
‫I'll go into partials create an errors.
‫Uh, errors dot PHP.
‫And I'm going to paste that in and save it.
‫So errors should now be we can pass it in now like this.
‫So now let's go to our listings form.
‫So let's see.
‫Post a job and I'm not going to put anything in.
‫And you'll see that works okay.
‫And then we can take this.
‫And let's also put it in the listings edit.
‫So right here.
‫Replace this with that.
‫And also in our register now right under the H two.
‫We'll pass that in and let's try that out.
‫Register I'm going to click Register with no email.
‫And please enter a valid email address.
‫So now we have that hooked up.
‫So back in oops.
‫Back in the controller let's add a couple more validations.
‫So I'll just take that.
‫And I'm going to use string here because I want to make sure that the name is is filled in.
‫So we'll say name.
‫We'll say the the minimum amount of characters is two and the the max is 50.
‫And then we'll change this.
‫Let's see errors email to errors name.
‫And then let's say please.
‫Or let's say name must be between.
‫Two and 50 characters.
‫Yeah, I guess that's that's fine.
‫So let's test that out.
‫Click register.
‫Um.
‫Undefined string.
‫Oh, I spelled that wrong string.
‫Let's refresh.
‫Resubmit the form and we get our name error.
‫Okay.
‫If I put something in the name, then we don't get it.
‫All right?
‫Now we want to kind of do the same thing with the password.
‫So I'll copy that and let's say.
‫Password, and that's going to be at least six characters.
‫And then let's change the key here to password.
‫And I'm going to say password must be.
‫At least six characters.
‫I'm not going to say the max, because, I mean, who's going to put in a 50 character password?
‫So let's see if that works.
‫Does.
‫All right.
‫And then the last validation is going to be the match for the password and confirm password field.
‫So let's change the method to match.
‫And it's going to take in.
‫The password and it's going to take in the password confirmation.
‫And we'll call this.
‫Password.
‫Underscore confirmation.
‫And then we'll say passwords.
‫Passwords do not match.
‫All right, so let's enter.
‫Let's reload this or just go to it.
‫And let's put something in here and here that doesn't match.
‫And there we go.
‫Passwords don't match.
‫So if I put everything in.
‫We can leave city and state blank and let's say 123456123456 Register and there we go.
‫So it's passing all the tests.
‫To reload that.
‫All right.
‫Cool.
‫Now, in the next video we are going to submit to the database.
‫But we also want to make sure that there's no other user in the database with that same email.
‫So we're going to do those two things in the next video.