Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Over the last few movies, we've seen how to validate the form data that comes in to us and how to display useful error messages back to our user. But our validations right now are not really reusable, we've just written if statements. If a certain logic is true or if a certain logic is false, then add an error. It would be much better if we could take that validation and put it into a custom function so that we could reuse it. And that way, every time throughout our entire application that we want to check and see is a form field blank, we can just call our function and ask it. Hey, use your logic that you have built in and tell me, is this form field blank? What's going to be really useful, is if we can develop a file that is going to be a library of validation functions that we can just include and call upon whenever needed.
That way, we can do it throughout our application, and potentially we can even take that file from project to project, and always have these validations ready for us to call. It's very handy. Let's see how to create that kind of file now. So to begin with, let's open up our validations.php that we worked with before, that has all of the logic in it that we want. And let's do a Save As on this, and instead of validations, we're going to call it validationfunction.php. Now its not going to have any HTML in it anymore its just going to be PHP that defines our function. So we want to take the HTML out of the top and bottom, and I'm also going to take out some of these that I'm not going to use.
So I'm going to take out uniqueness, and format, I'll leave inclusion in, I'll take type out, and let's take minimum length out as well. Now, you can do all of these on your own. I'm only going to show you this subset, just these three as an example. So for all of these, I'm just going to take the indenting out. And then let's change each one of these to be a function that's reusable. So instead of having a value set here, what I want to have is a function whose name I'll call has presence, and it's going to accept a value as an argument. So, all this logic is going to come up inside here, but there are a couple of differences, some changes I need to make.
First of all, I don't want it to echo anything back. So I'm going to take that away. What I want it to do is return true or false, so I don't need this if statement at all. And I can actually take the parentheses away. This is now a Boolean, the result of this will be either true or false. We saw that when we worked with Booleans before. But in addition, I need to make sure since it's in our function, that I call return on it, so that it is returned back as a value. Now, if I call has presence and I pass in a value, it'll return back true or false to me. But, the logic that I had worked on before, was actually if it was false, then add the error.
But has presence is the opposite. It's not has no presence, it's has presence. So I need to reverse the logic of this. I think it works better if these work in the affirmative instead of the negative. So if it has the presence, it will return true. So, I have to reverse each one of these, is set, and (INAUDIBLE) value is not exactly equal to an empty string. See how that works? So if it is set and the value is not equal to string, then it has presence. True. If not, it would return false. Alright, so let's look at the next one. Let's say that we have string length, maximum length, instead of having max, instead I'm going to have a function has max length. And it will return true or false, once again, based on whether or not it has this maximum length.
Now, it's a little bit awkward naming, maybe you want to name it something else. But to me it means that it is under this maximum length. So, let's make those same changes we made before. Take that out and that out, return the value and now I just need to reverse the logic of it. So, now it's not less than, but less than or equal to. Be careful when you're working with less than and greater than, that you consider that equal to case as well. So if the string length is less than or equal to the max then true, it has a maximum length of whatever we've asked it for.
Alright, let's try another one. Let's do inclusion in a set, take these away, function has_inclusion_in, and I'll pass it $value and $set. Alright? Now, I know I don't need all of this anymore. I just need this bit of logic in the middle. Return. And I need to reverse the logic so that if it's in the array, it returns true. So it has inclusion is basically the same thing as saying in array. This just makes it a little more feel like a validation, and this just happens to be the way that it's going about it.
Might be also that you beef up this function over time so that it does other things. So I don't mind it. Okay. So now that we have our little mini validation functions file, let's include some of those and start trying them out. So, I'm going to go over to Validation errors, and the very first thing I'm want to do is go to the bottom of it. You'll see that we had form errors, that was a function that we wrote before. Let's reuse that. Let's just grab that. I'm going to cut it, I'm going to add it to my validations function. So now it's here as well. So that'll get defined at the same time. So I don't need it here. I can take out a bit of this PHP.
I don't need any of these validations that I was doing before, because all of these now can be handled with require-once and validation-functions.php. So there we go. I still have my errors the way that's being set, and then I'm displaying any errors that came up. So, if we were working with a real form, we would probably be checking to see if the post variable had been set. We would be then pulling in the value something like this. For now, just for demo purposes, I'm just going to set username right here in the file.
So if username is equal to trim and we'll put in an empty string, then we'll check if not has presence calling our new function on username. And then we can define our custom behavior, whether we want it to do. We might not want it to do the same thing every time we call the function. We'll call it user name equals and let's say, user name can't be blank. Pretty standard stuff. Alright, so then we'll display that. Our user name is blank, so we should be able to see that page if we go to validationerrors.php.
Validationerrors.php, it's where we're working at before. Reload the page, and there we are. User name can't be blank. If we test it, move that out of the way. Where's the other file? There it is. User name is Kevin. Now we try it, and it doesn't give us an error anymore. See how that works? Now we have some reusable functions that we can use each and every time. So now that we know how to create validations and we've made them into reusable functions, let's try putting it all together by applying validations to our single page form submission.
We'll do that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member