PHP Tutorial (& MySQL) #20 - Filters & More Validation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
or rather gang so so far we do some very basic validation to check that an input field actually has a value when a user click Submit but we're still not checking if that value is the correct type like this could be a name and I could click Submit and that's valid in the eyes of this farm so far we want to check that that actually is an email this is a title and this is a comma-separated list of ingredients so we need to validate those things on the server inside this script now the way we're going to do that in PHP is by using a combination of things first of all we're going to use some filters or rather a filter to validate the email and filters are built into the PHP language to help us validate things like this now PHP only has filters for certain things emails are one of them but they don't have a filter for a comma separated list or a title so we need to validate this ourselves and for that we're going to use some regular expressions now I'm not going to focus in this video about what regular expressions are that's something separate from PHP we use them in all languages and for me to delve right into that will take about 15 lessons in itself I do have a whole series on regex regular expressions and if you want to learn more about them and how to use them to validate certain things then I'm going to leave the link to that series down below and you can go and check those out okay and that will really help you when you work with forms in any programming languages okay so either way we're going to use a built-in filter to validate the email from PHP and then we're going to use a function to use some regular expressions to validate the title and the ingredients all right so it sounds complex it's not awfully complex but let's try it so first of all the email so then so far we're checking it's not empty and if it's not empty its echoing out the email now I don't want to do that anymore instead what I'm going to do is create a new variable I'm going to call that email and I'll set it equal to dollar sign underscore post and then email so we're grabbing that value from the post array the value that was submitted to us that's if there's something in it okay I'm restoring that now inside email I'm doing this because we might be using this a few times going forward over the next tutorials and now we have it handily stored in this variable okay then so now I'm going to check that this is of the correct structure it is actually an email and that's what I'm going to use a PHP filter for so we'll do an if check and then we're going to use a method called filter underscore VAR soap filter variable so inside this method of this function is going to take two parameters first of all the value that we want to check which is this thing right here so the email right that's what we're going to check so we're going to copy that and paste it in as the first argument the second argument is going to be the type of filter that we want to apply well we want to apply the filter underscore validate underscore email so that like I said that filter is built in to PHP so we're passing these two things into the function so what that function is going to do is take this value that a user enters and pass it through this validate email filter and that is going to make sure that this is a valid email so it has an @ symbol in it then it ends in something like Co UK or com okay it's a valid email that's going to check it for me now if it is a valid email that's going to return true and this code is going to execute right here now what I want to do is echo something out if this is not true okay if there's a problem with it because then I want to echo out some kind of error so if this returns true then this code is going to fire now I could do an else statement to fire something else but then we'd be placing nothing inside this if statement and that's a waste of coding space the thing so instead I'm going to place the negation operator in front of this right here now remember when we looked at comparisons and billions and things like that if we placed the negation operator in front of the equals it says is not equal to it kind of reversed it okay so what this is going to do is reverse whatever comes after it so if this return is true and the email is actually valid then if we put this in front of it it's going to turn that true value into false okay so if it's valid then this if statement will evaluate to false however if it's not valid and this statement returns false then the whole thing with the negation operator in front of it is going to return true and then we can fire what's in here okay so now we can put the arrow inside here and the error is going to fire when the validation does not pass so the error I want to place is going to be email must be a valid email address and looks like I'm shouting at them there so let me do that again must be a valid email address okay so I hope all that makes sense that I just explained to do with this negation operator and what this returns we're going to do a similar thing now with the other fields but before we do that let me just check if this works so what I'm gonna do now is refresh the page first of all in fact I'm going to press ENTER and we get an error that's probably because we've missed off I will semicolon somewhere we have schoolboy error so let me save that again and I'm gonna refresh and now if I type something in like Shawn then submit we can see the email must be a valid email address all right so we know that this is not valid however if I type in Shawn at the net ninja call it UK and click Submit then we shouldn't get that error perfect because that is valid so that works okay then that's the first step done cool now we need to check these two things and that's title not title we need to check these two things first of all the title and secondly at the ingredients now we used a built in filter right here the filter validate email PHP had that included in the language it doesn't have one for a title because there's no set kind of validation for a title is there and it doesn't have one to validate a comma-separated list at least I don't think so so we're gonna have to validate these ourselves using a different kind of function and some regex okay like I said regex is not gonna be covered properly in this course if you want to learn about it I'll leave the link down below but essentially what regex does or regular expressions do is they look at a string of characters and they match it to a pattern described by the regular expression and even that pattern matches what's in the string then it's going to pass the test if it doesn't match then it's not gonna pass the test for example in title we could create a regular expression pattern that must include uppercase letters lowercase letters and maybe spaces only if it's got any kind of special characters like an @ symbol then it's not going to pass so we're going to create a regular expression to test for that pattern now we're going to do a similar thing to up here first of all we're going to delete this and we're going to store the title inside a little variable called title we'll grab it from dollar underscore post and grab the title from that post array okay then we're going to do an if check and in here the function this time is not going to be filter var but it's going to be P reg underscore match okay so preg match if you like like this and this is how we match something to a regular expression it takes two parameters the first parameter this time is the regular expression that we want to match it against and the second parameter is going to be the title itself okay so then in here the regular expression I'm just going to copy this from my github repo remember you can get this as well it's lesson 20 in the branch drop-down and I'm going to paste it in there so we place it in quotes starts and ends with the forward slash and this is the regular expression so we're seeing right here from the start to the end that's what these symbols means we want any lowercase uppercase all spaces right here as many times as the user 1 so it can be any length at least one character long ok that's what that regular expression means so the second argument we need to pass in here is the title itself that's what we're going to match against alright ok there so Eve it passes this regular expression is going to return true again we want to only output an error if it's false so we need to negate it here for the same reason as up here alright then so now we'll echo the error and that error is going to be title must be letters and spaces only okay okay cool so now we have that error let's give this a whirl I'm going to place in a valid email I'll just say a up be calm one of the simplest emails you can get and the pizza title is going to be blah at one two three now we have an @ symbol in here and one two three they're not valid for a title we didn't say that in the regular expression so this should echo out the error I'm going to submit and now we can see the title must be letters and spaces only okay so let's give this a whirl again yep be calm this time for the title I'll say ninja Supreme sounds like a good pizza submit and now we only get this error at least one ingredient is required so this passed this time ok we're going to do a very similar thing using the same function for the comma separated list of ingredients so let me just copy all that stuff there delete this junk and place this in instead now I want to replace this with ingredients and also this with ingredients from the post array and then we want to replace this with ingredients as well and we want to match against a different type of regular expression here I'm going to delete that and I'm going to grab the other one from my repository as well it's a bit longer this one I'm going to copy it and paste it over here again you can see it's a long regular expression starts with a forward slash ends with a forward slash and this is looking for a comma separated list okay again if you want to learn more about regex check out my other playlists on that and you'll know exactly how I've done this okay so anyway that there is going to check that this ingredient string is a comma separated list of words if you like okay which is what ingredients are words now again we're using the negation operator because if it passes it's true we want to negate that so that we only echo out an error if this returns false all right and the error this time is gonna be ingredients must be a comma separated list all right so if I save this now what I'm gonna do is say a be calm we'll call this the triple B really exciting and then it will just do something random in here like tomato at at at semicolon right that's not a valid ingredient so let's submit and it says ingredients now must be a comma separated list all right let's try this again a at B calm peaks at title CCC we're taking it up a notch and then the ingredients is going to be tomato cheese and mushrooms okay submit this is valid right submit and now no error so that there my friends is all our basic validation for this farm pretty much done if this was a real-life application maybe you do a bit more validation as well this will do for this project okay so now we've got the validation done in the next video what I'd like to do is take the areas that are currently being shown here and show them in the form instead and also if we enter in something that's valid like a be calm and the piece of title which could be DDD if we then you know do something this not valid here these two are valid right so if we submit we should get the areas but those two values should remain there the second time around so the user doesn't have to type them out again that's just the pain to do so I'm going to show you how we can persist that data in the form as well and show the errors underneath the relevant form fields
Info
Channel: The Net Ninja
Views: 53,282
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, regex, regexp, regular expressions, php filter, filter, php filters, email filter, match, preg, php preg, php form validation, form validation, php validation
Id: wFiCZHrCFOw
Channel Id: undefined
Length: 13min 5sec (785 seconds)
Published: Tue Feb 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.