در حال بارگذاری ...

Extracting Validator Class0:00

All right, welcome back, everybody. Let's jump right in. So, yeah, in the last episode, we toyed around with writing our first set of validation rules. But then we realized, well, it probably would make sense to extract this logic into its own file or its own class. And that way, I only have to write the logic one time, and then throughout the entire application, I can reference an existing function or method. Okay, so let's do that now. I'm going to call this class, well, Validator. Okay, class Validator.

I'm going to call this class, well, Validator. Okay, class Validator. And yeah, at least initially, I'm going to keep this class very simple and also very pure. And I'll show you an example. We'll start with something simple, like validating that you've given us a string of a certain size. Okay, so we need the value. And yeah, if I switch back to the controller, we could pluck this logic that we wrote in the last episode and paste it in here and return the result. Okay, so now I can swap this out with the value you give us when you call the function. Okay, let's give that a shot.

Okay, so now I can swap this out with the value you give us when you call the function. Okay, let's give that a shot. I'm going to switch back and require the controller. And actually, real quick, if we have not discussed this, I can require a file like this, or I can use it almost like a function. Both will work just the same. And you can see sometimes I will absentmindedly use both, coming from the JavaScript world where there's also a require function. But yeah, generally, just stick with one or the other. Okay, so with that in mind, I will use this approach.

But yeah, generally, just stick with one or the other. Okay, so with that in mind, I will use this approach. Okay, cool. So now, yeah, well, at least initially, I would have to instantiate a new Validator. And then right here, I could use validator, string, and then pass through the post body. And yeah, so I've just taken the logic that we had in this file and extracted it to a dedicated class. Okay, so let's give that a shot. I switch to the browser, refresh. I try to submit the page.

Trimming Before Validation2:18

I give it a refresh. And now, once again, we have a new row with a body that's not empty, but is composed of empty spaces. Okay, so let's delete that and make sure that we trim it before we perform the validation. All right, so back to PHPStorm, into our validator. And now I could say, well, why don't we trim the value before we check it? All right, so let's give it another shot. Once again, I will hold down the space bar. But yeah, this time, if I submit the form, the validation is working the way you'd expect. So we can see that the trim function that PHP provides literally trims off blank spaces.

But yeah, this time, if I submit the form, the validation is working the way you'd expect. So we can see that the trim function that php provides literally trims off blank spaces before or after your string. And it turns out that's what we want in this case. Okay, great. But now, what about... Well, actually, let's do this. Let's go back to the controller. And you can see we have another check where we make sure, for the example, that your notes can't be more than 1,000 characters.

Adding Min-Max String Rules3:08

And you can see we have another check where we make sure, for the example, that your notes can't be more than 1,000 characters. Okay, so what would we do? Would we create a new function or would we simply expand our existing function to accept a minimum and maximum number of characters? And I think that second approach is probably the one we should use. Okay, so let's accept a minimum number of characters. And we'll set the minimum to 1. You have to give us at least one character by default. And then the maximum should be basically infinite.

You have to give us at least one character by default. And then the maximum should be basically infinite. So unless you explicitly set or pass this argument, the maximum number of characters we allow is infinity, right? So in those cases, we can use inf for infinity. Okay, great. Okay, so now think about it. Let's start by saying, well, let's update the value variable by trimming it so that I don't have to do it more than one time. And then we will say, well, make sure that the value or the length, the number of characters

I don't have to do it more than one time. And then we will say, well, make sure that the value or the length, the number of characters of that value is greater than or equal to the minimum. So in this case, greater than or equal to 1 by default. But also the length of the value is less than or equal to the maximum. Okay, and let's see if that works. So I will come back to my controller. And yeah, now I can merge these into one. So I will get rid of this and update this to validateString. I want the minimum to remain 1, but the maximum is 1,000 characters, which again, just to

So I will get rid of this and update this to validate string. I want the minimum to remain 1, but the maximum is 1,000 characters, which again, just to show you, if I bring this back, that's what we had at the end of the last episode. So yeah, now I'm just merging them into a single function call. And then I could update this string to be a little more generic. How about the body, or I'm sorry, a body of no more than 1,000 characters is required. Okay, and then finally, we should handle the case where it is not valid. So I'm going to negate it. If it is not valid, then update this errors array. Okay, let's have a look in the browser.

Pure Functions and Static5:38

And once again, the validation fails. All right, and I think that's a pretty good refactor. Okay, so now let's talk about this. This method is what we would call a pure function. And I'll tell you what that means. A pure function is a function that is not contingent or dependent upon state or values from the outside world. And by outside world, I literally mean the world that exists outside of this simple function. And again, what that translates to is within the body of this method, I'm not deferring to any internal state.

And again, what that translates to is within the body of this method, I'm not deferring to any internal state. I'm not referencing an outside class or object. It's all contained. So notice, I call the method. I feed it some values. It works with the values. And then it returns a response. But there is no reference to this inside it. And that's what I mean by it's not dependent upon existing internal states.

But there is no reference to this inside it. And that's what I mean by it's not dependent upon existing internal states. It's not deferring to a different class or a different object. It's all very simple and very pure. So in these cases, when you have pure functions, we can make them static. And that then allows us to call the method directly without first creating a instance of that class like this. Let's switch back. And with this approach, I can now simply remove the instantiation and instead just say className::methodName.

And with this approach, I can now simply remove the instantiation and instead just say class name, ::, and then the name of the method. So yeah, these two :: are how we call or trigger a static method on a class. OK. So if I come back and we give this another shot, it still works just the way it did before. But yeah, just to be crystal clear, if we don't explicitly make this a static function, we'll notice we're going to see some kind of fatal error. Because we are trying to interact or call this method as if it were static. But we didn't declare it as static. So make sure you add that static keyword.

But we didn't declare it as static. So make sure you add that static keyword. OK. So yeah, in situations like this, for very simple and very, again, pure functions, this is a good way to go. OK, great. So now, as you can imagine, your Validator class could validate so many things. Like, make sure you gave me a proper URL. Or make sure that you gave me a valid email address. Why don't we cover that example?

Adding Email Validation7:49

Or make sure that you gave me a valid email address. Why don't we cover that example? Even though I don't have an existing User yet. We haven't built a registration system. Just to give you an idea. OK, so if we want that, we'd create a method called email. I would make it static. And yeah, we'd interact with it like this. Validator::email($value); And then you'd give us the value that probably came from the registration form or something.

Validator, email. And then you'd give us the value that probably came from the registration form or something. So in this case, joe@example.com seems to be a valid email address. And actually, on that note, notice it's not going to ping some existing service to make sure that it is a registered email address. It's just ensuring that it takes the form of an email address. So this would return true. But yes, something like this. Well, that doesn't take the shape of an email address. So it would return false.

Well, that doesn't take the shape of an email address. So it would return false. OK, so the implementation. Well, there's a number of ways to do this. You could paste a very dramatic regular expression. But instead, we're going to use a php function called filter_var. And this gives us a way to sanitize or validate a string in a number of ways. And yeah, if you want, you can always just review the PHP documentation. But in this case, you can see we give it a value and then our desired filter. And again, switch to the documentation to see some examples.

But in this case, you can see we give it a value and then our desired filter. And again, switch to the documentation to see some examples. So yeah, things like, let's see if I can see filter_var. Yeah, here we go. Here's an example. So we call filter_var. We give it the $email address. And then we're going to reference our desired filter. And in this case, the one we want is FILTER_VALIDATE_EMAIL. So I will copy that, switch back, paste it in.

And in this case, the one we want is filter_var with FILTER_VALIDATE_EMAIL. So I will copy that, switch back, paste it in. And then I will accept the desired value and return the result. OK, so now, yeah, again, we don't have an existing email address. This is just a quick example. But we could toy around with it right at the top by saying, and we'd say validator email. And let's start with something that is not a valid email address like this. So we come back, give it a refresh. And sure enough, it returns false. But if we instead provide a valid email address, you'll see it's not going to return true.

And sure enough, it returns false. But if we instead provide a valid email address, you'll see it's not going to return true. But it will return the address itself, which is a truthy value. So in other words, I could say, well, if not validator email, then once again, that is not a valid email. OK, but if we use a real one, I'm just being super crystal clear here to make sure we're all on the same page, then we don't trigger this dd function. So yeah, I'm just giving you some ideas here. And trust me, once it's time to build something like a registration form, this is going to be super handy.

And trust me, once it's time to build something like a registration form, this is going to be super handy. Think about it. To register, you probably have to provide a username, an email address, maybe a password. OK, well, this will take you the whole way there, basically. Think about it. Well, let's validate your username using this method. Next, let's validate the email address using this method. And then finally, maybe you've decided passwords on your site need to be at least seven characters long.

Recap and Refactor Benefits10:55

And then finally, maybe you've decided passwords on your site need to be at least seven characters long. All right, we would validate that using this method, but we'd set the minimum to seven. And that would take care of it. It takes you the whole way there. OK, so now to wrap up, I want to note how the behavior that we currently have is not different than what we had at the conclusion of the last episode. The only thing we've done here is tinkered and refactored and rewrote the code to be just a little more flexible and also, more importantly, pleasing to the eye. And trust me, that makes a huge difference.

ValidationPure FunctionsStatic Methods

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟