7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 9Length: 53 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Validating User Input Type

Along with making sure all the required fields are filled in, you may also want to ensure that the user is providing the proper type of data in your form. In this lesson, we will implement some basic checks to be sure that the data the user is submitting to your app has the type that you expect.

2.4 Validating User Input Type

The next thing that I wanna show you has to do with data type validation, so what does that mean? Well, really all I'm talking about now is, let's say I'm creating a very simple form. And in this case I'm really only talking about strings for the most part here. Your name is a string, your email is technically a string and the comments are gonna be a string. But what about if I'm starting to introduce other text boxes or I'm trying to make this very generic? And I wanna make sure that the user has entered an integer or a decimal value or something else, right? And so I wanna be able to make sure that when they pass that data to me at the server, that it is in the proper data type. So that I can continue processing their request, because nothing is worse than getting some sort of invalid data or invalid data types. And then trying to do some sort of processing, and then your application blows up. And then you start sending back strange errors to the end user, nobody likes that. So this is simply another way to do some data validation to make sure that you are getting the type of data that you want. So what we're gonna do is we are simply going to do a little bit of work here. We're gonna continue to use this validation function. You can obviously break these things out into other validation functions or however you would like to do that. But I'm gonna just start to kind of keep them all together. Another thing that I wanna do here is, you can kind of bundle all of these validation areas together. And show them all at the same time to the end user. A lot of times I like to keep things kind of simple and I just wanna do one thing at a time. So right now, the first thing we are doing here is we are checking for required fields. And if they're not there and we want them to be required, then there's gonna be an error. So if that's the case, I wanna just return that particular error. So I'm gonna start by saying if not empty. And if the error is not empty, I just wanna return it and move on and let them know. Now, if it's empty, which means we've already fulfilled the required portion of the check, now I wanna add in some additional checks. And like I said, at this point, I wanna work on adding in some validations for data types. So maybe I'm expecting the input to be an integer or some sort of numeric value, or some other type, I wanna start doing those checks. Now the tough thing about this is going to be that when you receive inputs from a form, when you have them set as text, like I have them here. And then I go into the underscore post super global and pull things out, everything is basically gonna be a string. So you have to use your imagination a little bit and use some of the other tools in the PHP language to be able to validate data types in this way. So one thing that I like to do is I like to be able to pass in what I'm expecting this type to be. So I'm gonna say that I'm gonna pass in another variable. And this is going to be expected type so I can pass things in like integer or a string, or things like that. So the next thing that I wanna do is I wanna come in here and I'm going to create a simple switch statement. And in here, I want to take a look at the expectedType. And so what I'm gonna do here is I'm gonna have a case in here for each of the types that I wanna be able to handle. So in this case we're gonna use maybe two of them as an example, so let's say, in this case, I am expecting this one to be integer. And in this case, I want to do another check. In this case, I'm gonna say error is gonna be equal to, and I can use another helper function called is_numeric. Now is_numeric is a function that's going to take a look at a string to check to see if the value of that string is actually a numeric value. Meaning, is it a 1, 2, or is it maybe 2.5 or whatever, those are all numeric values. Now there is an is_integer that you can use as well, but this one is gonna see if the actual type of the value that you passed in is an integer, which means if I pass in a string, that is the string number 1, this is gonna return a false. And that's not really what I'm looking for, what I'm looking for is more along the lines of an is_numeric. So what I wanna do is I wanna say if it is not numeric, so if is not numeric, and we're passing in this inputValue. If it's not, then I want to, once again, give an error, so I'll say inputName, and we'll concatenate should be a numeric value. Something like that, and then we can go ahead and say if it is not. Or if it is properly formatted as a numeric value, then we'll just leave that as the empty string. And then we'll go ahead and break on that case, and then we'll do one more, we'll do a string as well. Because it's always good to know maybe if they're passing in a string, I don't want them passing in some other goofy thing. So I can say, error is equal to and there's another function for this one, you can say is_string. We'll go ahead and do an input value and we can say $inputName, once again, should be a string. And then if it is, we'll just leave it empty and we'll do a break. And then we'll also put in a default because it's always good practice to have a default. And in this case if there is something else that's passed in that we don't understand, we'll just leave it as empty and then do a break. And then finally, once we've gone through the switch statement we can go ahead and do a return error and save that. So now, as you can see, this could get kind of large depending on what types you wanna support. And that you want add in validations for, so I might consider maybe taking this check and splitting it off into another function. That can do the data type validation and have this validate required input call that one or call it separately. Something like that, but just to illustrate how this can work, let's go ahead and leave this there. So now we need to be able to pass in the expected type of the values that we're taking a look at. So in this case, I'm expecting both of them to be strings. So what I can do here is I'll just go ahead and add in an expected type as a string, just like that and we'll go ahead and save it. Now because both of these things are being passed in as strings, then this should always work. So if I were to pass in Derek and hit Submit, then, as you can see, it works. If I pass in 8, you’re gonna see that it works. Because like I said, I’m always saying I want this to be a string, so that it’s always checking to be sure that it’s a string. And because of the way that this super global works, it’s giving the value back as a string. But let's just say, for instance, that Name, I wanted it to be an integer, even though that's not the case, but let's just dream. And let me go back to my form here, we'll go ahead and refresh this, so I can go ahead and hit Submit. You'll see Name is required, and now because I'm expecting it to be an integer, if I were to type in Derek and hit Submit. You're gonna see Name should be a numeric value. So now you can see how we can start to integrate some other types of checks. Some other types of data validation into our form to start to look for specific types of data that's coming in. And make sure that the end user is providing us exactly what they should.

Back to the top