‫Okay.
‫So we're going to start to get into adding listings through our application.
‫But with that we have to look at validation of the data that's being entered.
‫We have to look at sanitation to clean up any user input that could be harmful.
‫So that will take up the next few lessons, next few videos.
‫All right.
‫Now I want to start off by creating a validation class that we can use.
‫And this will have static methods.
‫So we won't have to instantiate it.
‫But it'll be helpful to just validate strings and make sure that some something is being added.
‫We can validate email addresses and we'll have like when we when we register users they'll be a password
‫confirmation.
‫So we'll have a match method.
‫So two fields have to match.
‫So things like that.
‫And that's going to go in the framework.
‫So in framework let's create a new file called validation dot PHP.
‫And this is going to be a class as well.
‫So let's create say PHP.
‫Now since this is in our framework folder we're going to give it the framework namespace.
‫So let's say namespace framework.
‫Okay.
‫And then let's create a class called validation.
‫Make sure it's the same name as your as your file.
‫And let's create our first method here.
‫So I'll just put in some comments here.
‫We'll say this will validate a string.
‫And it's going to have a couple params.
‫So let's say param.
‫String and that will be the value.
‫Now I want to I want to be able to set a minimum and a max with this method as well.
‫So let's do a param.
‫For the men that will be an int, so that will be men.
‫And then we'll also do max.
‫And as far as what it will return, it'll be a boolean, so it'll be either true or false.
‫So let's say public.
‫Now this is going to be static.
‫So we don't have to instantiate the class to use it.
‫We use the class itself call it string and pass in our params.
‫So the value.
‫Pass in the min, which we're going to actually give a default to, and we're going to set that to one.
‫Then we have our max.
‫And for the max I'll set the I'll set it to infinity.
‫And we can do that with INF.
‫All right.
‫So we want to first of all just make sure it's a string.
‫So there's a function we can use called is string.
‫So let's say is underscore string and then value okay.
‫Whatever the value is that's passed in then we'll say value.
‫We're going to trim that value.
‫So we're just going to pass it through trim.
‫So any whitespace will get trimmed off.
‫And then let's say length.
‫And we'll get the length of this str Len.
‫And pass in our value.
‫And then as far as returning true, it has to.
‫It has to be at least the min, and it can't be over the max.
‫And it has to be there.
‫It can't be empty and min on its own.
‫The default is one, so it has to have at least one character.
‫So let's say return.
‫Length and we'll say if that is greater than or equal to men and.
‫If length is less than or equal to the max.
‫So that will be either true or false.
‫All right.
‫Now outside of the where we check to see if it's a string, we're just going to return false.
‫Okay.
‫So pass in a value.
‫It has to be.
‫It has to be greater than or equal to men, and the length has to be less than or equal to max in order
‫to return true.
‫So this will return true if both of those are true.
‫So before we create any other methods, let's test this out.
‫So I'm going to go to the listing controller.
‫Which is right here.
‫And we're going to be using this validation class in this controller.
‫So at the top here we need to say use framework.
‫And you'll see we have the framework slash validation option because we gave it the namespace of framework.
‫All right.
‫Now for now we're just going to test it out in index.
‫So you're going to want to go to the listings.
‫So slash listings because that's where this method runs.
‫And let's try out that string method.
‫So I'm going to run it through inspect and die.
‫And let's say validation.
‫So the name of the class because remember it's their static methods.
‫So we don't have to instantiate it.
‫And we can use the scope resolution operator and let's say string.
‫And I'm going to pass in.
‫Let's just pass in an empty string with no other arguments.
‫And let's reload that and you'll see we get false.
‫And the reason for that is because there's no characters.
‫But we have we're returning true only if it's greater than or equal to the min, which the min by default
‫is one.
‫All right.
‫Now, you can change that by passing in.
‫Well, first, let's just put something in here.
‫We'll put one character and reload and we get true.
‫However, if we want to raise that that min, we can add a second argument here.
‫Let's say three.
‫And now it's false.
‫Okay, now this has to be at least three.
‫So let's say a, b, c reload.
‫And now it's true.
‫We can also add in a max.
‫So let's say five is the max.
‫So we'll say a b c d e f g.
‫Reload and we get false because it's over five.
‫So in this particular case it has to be either 3 or 4 to be true.
‫All right.
‫But, I mean, this will be used a lot just to make sure something is in that, in whatever is passed
‫in here, okay?
‫Because if it's empty, then it's going to be false.
‫So that's the string method.
‫Now let's create another method to to check email addresses.
‫So I'm just going to add a comment here.
‫Let's say validate email address.
‫And as far as a program, it's going to take in a string and we'll call that value.
‫And then for return.
‫Now, the way that that this is going to work is it's going to return false.
‫If it's not, if it's not a valid email and it will return the actual email if it is true, I mean,
‫if it is a valid email.
‫So let's say mixed for the return.
‫And then we'll create our function.
‫So public static.
‫Function.
‫Email.
‫And we're going to pass in value.
‫Okay.
‫And we'll set value equal to the trimmed value.
‫So just trim off any whitespace.
‫And what we're going to do is use the filter var function, which will take in the email address.
‫And then it takes in a specific filter.
‫And you can filter for many, many different things an email address, a boolean, a float, an int,
‫a date even like an IP address.
‫So we want to we want to use the email filter.
‫So let's say return filter underscore var.
‫And pass in the value, and then we want to pass in the filter we want to use which is filter, underscore,
‫validate.
‫Uh, validate email.
‫You can see there's a bunch of different options here.
‫So let's go ahead and save that.
‫And let's go over to our listing controller index method and we'll try it out.
‫So I'll just do actually let's put back our inspect and die.
‫And this time.
‫We're going to say email.
‫And let's pass in test.
‫We'll say test number sign test.com.
‫If I reload, we get false.
‫Right.
‫Because that's not valid.
‫If I say test at Test.com reload we get the actual email address.
‫All right.
‫So that's how that works.
‫And just to show you some of these other filters we can use, let's say if we do int.
‫And we reload, then obviously we get false.
‫If I pass in three and reload, then I get three.
‫Okay, we can do.
‫What else?
‫Let's say.
‫Um, what else we we can even do, like IRL.
‫So if I pass in here three and I run it, obviously that's false.
‫And let's pass in Http.
‫Halo.com.
‫Reload and I get the URL.
‫All right.
‫So filter var can really come in handy for for just data.
‫Sanitization.
‫Sanitization and validation.
‫All right.
‫So let's let's create the last one that I want to put here.
‫And that is going to be to match two strings.
‫And this will we're going to use this when we have the password confirm.
‫So under the email method let's create another.
‫Comment and we'll say match.
‫Let's see.
‫Match a value against.
‫Another.
‫And for the pram, let's say.
‫String and call it value.
‫And then return.
‫And it's going to return a boolean.
‫So let's say public.
‫Static function and call it match.
‫And it's going to take in.
‫Actually, it takes in two values.
‫I don't know why I just didn't add that.
‫So let's say value one and value two.
‫Okay.
‫So value one.
‫Value two.
‫And I'm going to trim both of those so I can just.
‫Grab that, paste that, and then change that to value one.
‫Value one.
‫Value to.
‫All right.
‫So we'll trim those.
‫Oops.
‫We'll trim those.
‫And then we just want to return if value one is equal to value two.
‫And then we can test that out.
‫So over here let's say match and we'll pass in a string of test.
‫And let's say test one.
‫So that should give us false if I get rid of the one.
‫And reload.
‫Then we get true.
‫All right.
‫So those are the three that I want to create right now.
‫I mean obviously if you want to add more later on you could.
‫So that's what this file is for is just a little validations.
‫So what I want to do now is create the store method.
‫So basically the create method is it's really only to load the view.
‫Let's get rid of.
‫The inspect and die here.
‫So the create right?
‫When I go to just listing slash create it loads the create method which is right here.
‫And that's just responsible for showing the form.
‫When we submit the form that we want to go to a method called store.
‫And again, this is kind of the convention that a lot of frameworks use.
‫I always go back to Ruby on Rails because that's kind of what I like to follow.
‫But Laravel also I believe has this, this convention.
‫So in the next video we're going to start on the store method and also talk about data sanitization
‫and so on.