 Hey everybody, this is Brian and welcome to the 15th yeet tutorial. Wow, we are flying through these One thing I've noticed is these tutorials are actually becoming almost as popular as my cute ones So there must be a big group of you guys out there wanting to learn yeet. So One thing you should know is my website void realms.com I will try to put these tutorials and the source code out there for this because we've been flying through them so fast It's gonna be difficult for me to get the code out there each time You should note that void realms if you go out to Facebook and you type in void realms There is an actual group out there. It's a public group. Anybody's welcome to join and it's just a bunch of us to talk about programming All right, so Today we're going to be talking about model validation So if you've been following along we've been kind of talking about how to load stuff how to save stuff Do really cool stuff Well, what happens when you go to save and it isn't exactly what you wanted it to be what's called model validation And we're gonna do a simple example of that. So we're gonna crack open our database We're going to go to altar table And we're just gonna say that first name Last name cannot be null So we have changed the underlying database The next step is we need to change the model There's an easy way and a hard way of doing this and I'm all about easy as I'm sure you are to the hard way is You actually crack open the model class and you start typing. We're not gonna do that. We're gonna do the easy way. So let's just Go back here load up G maybe if I remember the password there we go Go to model generator and we're going to overwrite the teacher's class because that's what we just generated Notice how it says diff so we can actually click on diff and see the differences and That right here is what I wanted to bring to your attention first name and last name are now required Meaning that's the eversion of not null meaning that field is required if you try to save that record It will fail if you leave it blank and we're gonna demonstrate that in this tutorial So just go ahead and overwrite Once your code successfully been generated You can just go back to the index here. All right now. Let's Comment that out And we're gonna do a little bit of finagling here with our Stuff there we go Let's go back to teachers We're gonna use the built-in method first. Notice how there's suddenly a login prompt. That's what there is Haven't been here in a while Anyways, notice how there's suddenly red star next to each one. That means it's required Whereas if we go to students, which is pretty much the same structure, there's no red star So this feels with the red star required so we can just actually create a blank student if we want to See boom. Well, we don't want that. So let's actually Delete it. Now if we try to do that with teachers and we go create That's validation in action right there So as please fix the following input errors first name cannot be blank last name cannot be blank because those are required Now this is Ajax base. So if we do that and we go here You'll see it in action. Now. Let's Actually should say if we go here and then here Ajax didn't trigger my bed We can find the code to put the Ajax in there in another tutorial, but I wanted to really demonstrate validation So if you try to make something, that's what happens So let's examine the parts of this in teachers create Let's go teachers controller Here's create You can see how what we're doing here is we're creating a new teacher record and Then ignore this part because what we're saying is if the post back is already set meaning we're saving the information So all we're really worried about this point is we're rendering the model. Well, it's a blank model. So there's nothing there That leads us to this point You type something in you hit save or create this point and then this happens So what's really going on is right here? We're saying if is set blah blah blah Model attributes. We're loading it from the post if model save notice how if model save There's no if model not saved and we're gonna explore why Let's go to create You see how we're rendering a partial This is one of those gotchas in me where you may be looking for the code and you find it's actually somewhere else and the reason Why it's in this underscore form is because it's used in several spots. It's used in the create update See underscore form. This is called code reuse. All right, so Here is our you guessed it our form and Well, actually, let's just enable Ajax and see if that does it. I'm not quite sure if it will do it or not, but Let's try this just an experiment bear me a quick tangent here No, it doesn't enable Ajax on the form And I think that's part of because the render partial, but we'll just change this defaults So I will do the Ajax in a different tutorial. All right back to what we're doing. Sorry scatterbrained Right here this Echo form summary That is the actual error message that you're seeing That's the Big red box right here That's the error summary and then each one of these has an error field It's a hidden field until of course there's an error message and That's this last name cannot be blank It gets the name from the display property So we can actually go into the model here and you can say Sir name. I just kind of wanted to demonstrate this real quick Notice how it's surname instead of last name. Let's actually get rid of that. You can see how it's a surname again So that is the interaction with validation you can see how the labels and come into play there So sometimes the database administrator will call it, you know My first name and you want to just call it first name so you would override that in the model now You should be aware that every time you Generate that model you're gonna have to go back into the attribute labels now You can actually pull that out of the description in key. So that's automatic validation right there Let's go here Where am I there I'm It's been one of those mornings ironically, I don't know what the deal is but Close the model close that let's go back to our test here and we're just gonna say We'll grab this example here Now if we run this code example, what do you think is gonna happen? This is in our test action by the way, we're making a new teachers. We're setting the first name We are not setting the last name and then we're calling save So let's give this a unique name like Validate yeah, why not So we're gonna actually go here and we're gonna test and Voila Didn't print anything. We're gonna actually make it print here This is one of those little gotchas in you and I kind of wanted to go out of my way to explain this Because it was one of my first stumbling blocks. You can see how we've got any record and It's you know, it's a sea octave record. It knows it's a database record So we're going to actually look at the database now Close that and Well, where is it? There's no validate. We should have a number 12 with validate. Well, what happened? You guessed it, of course We didn't validate it. So the saves actually returning false here. So what we can do is say if not save echo did Not save this is just kind of a Down and dirty kind of troubleshooting here So if the model does not save it did not save And then we're gonna just refresh this You see up here in the corner did not save So how do we get around that? I mean we can tell that it didn't save but we need to validate the model So we're gonna actually say if not or actually if Model validate then Model save and then you can programmatically say okay Didn't save something that's going on here Essentially does the same thing right? Notice how we can get an array of errors out of this. So let's actually do that. So we'll say for each Model errors as error Echo and we're just gonna echo this out here Gonna make a big old mess followed by did not save Whoopsie Array conversion string. Hmm. Oh Derp had one of those moments. Sorry. I Just ate Arby's and I don't know what it is about Arby's. I love Arby's But whenever I eat it, I'm like brain dead for like an hour. Here we go So you can see through a very dump that we have an array with one item There's our just 25 length string that says last name cannot be blank So you can actually go in here and get the you know do a for each or get the count and actually pull out The actual error message here So that's how you'd programmatically deal with that That the nutshell is how to do validation Seems pretty simple, but I guarantee you as you're working with you you will run into that problem multiple times Which is why I really wanted to cover it because it was a huge stumbling block for me when I first started with you I would go through and I'd you know hit save and nothing would happen And I'm like what the heck where's my information? And then I had to figure out it wasn't validating and then you know when you get into like database transactions validation is huge Simply because you want to validate it every turn that hey, it's working And if not you want to figure out where it's not working So that's all for this tutorial. Hope you found this educational entertaining Shameless plug advertisement here. Be sure to visit my website Void realms comm also go out to Facebook and join the void realms group where there's just a bunch of us hanging out talking about programming