Validation Form Setup0:00
Alright, let's talk about some changes to how you will construct custom validation rules. So take a look at my example here. On the homepage, I load a form, and don't be scared, it's the most basic form that every single Hairfella user has constructed. So the form will post to the homepage, just for the demo, so we will hit this route. And within it, you type in a title, you click on the submit button, and then if we detect any errors, we loop through them and just display them within a quick unordered list. So if we switch to Chrome, this is what we get. So let's talk about validation. Now we already know that in Laravel 5.5, I could say request, using the attributes you
Using Built-In Validation0:34
So let's talk about validation. Now we already know that in Laravel 5.5, I could say request, using the attributes you have, I want to validate them against these settings, basically. So I could say, well, the title, and of course that corresponds to this right here. The title is required, but also maybe you have to include at least three characters. So pretty basic stuff. If we give it two characters, then sure enough, the validation fails, and we spit that out. Okay, great. But now what about when your domain requires a custom validation rule? And you're going to find lots of situations where this is relevant.
Generating Custom Rules1:04
But now what about when your domain requires a custom validation rule? And you're going to find lots of situations where this is relevant. All right, well, take a look. If I run php artisan, and I scroll up to my make commands, you'll now see a new one called make:rule. php artisan help, let's take a look. And we give it the name of the class, and as always, you want that to correspond to what the validation rule does. So if it makes sure that something is valid, then name it as such. If you're making sure the given text is spam-free, then name it that.
So if it makes sure that something is valid, then name it as such. If you're making sure the given text is spam-free, then name it that. Let's just do a super basic one. Maybe you have to provide exactly five characters to an input, and you know what, there might even be a built-in validation rule for this. I'm not sure, actually. It's not overly common, but let's just say, for fun, we wanted to construct that. All right, we would say make:rule MustBeFiveCharacters. Okay. And now you'll see a new rules directory within your app folder.
Implementing Rule Contract1:55
Okay. And now you'll see a new rules directory within your app folder. And now our basic interface, or our contract here, let's take a look, I'll show you. ValidationRule. Okay. So the basic contract is you need to implement a passes method, and that's just going to return a boolean. Does the given text and the given attribute pass your test? And then finally, what will be the message on the condition that it doesn't pass? And this is what will get injected into your form, right down here, when we spit out the
And then finally, what will be the message on the condition that it doesn't pass? And this is what will get injected into your form, right down here, when we spit out the error. Okay. The error message. So let's come back, and we're just going to say, check. Now the attribute will be the attribute name. So in this case, the attribute is title, and the value will be whatever you typed into the title input. So let's just say return strlen($value) === 5.
Applying Rule to Request2:42
the title input. So let's just say return string length of value equals five. Very very simple. So now, what will be the message that you want to display to the user? You must enter exactly five characters. And that's it. Really really simple, right? So now, if we go back to our validation, I could say title, and then new it up. App rules must be five characters. Now as always, if you do need to pass through any data to help with the validation rule,
App rules must be five characters. Now as always, if you do need to pass through any data to help with the validation rule, like a repository or anything, it doesn't matter, then of course, like any other class, you'd accept it in the constructor and then pass it through. But in our case, we don't have that, so we can ignore it. All right. I think we're all set to go. So we're going to come back to Chrome, give this a refresh, and we're going to send through something that is not valid. So we run it.
Chaining Rules with Arrays3:48
It's as simple as that. So yeah. Now imagine, like I said earlier, you want a real life validation rule where you maybe quickly scan the given attributes for spam. Make rule, spamFree. Then you could come back and say new App\Rules\SpamFree. Now what if you're saying, or what if you're thinking to yourself, well, what about when I want to chain other validation rules on? Well, so far we were just doing the pipe separation, but you may not know you can also just use a typical array.
Well, so far we were just doing the pipe separation, but you may not know you can also just use a typical array. So we could say, yes, the title is required, and also we're going to new up app\Rules\Spam. As simple as that. So now, as always, you're going to accept the attribute and the value, and then you can new up your Spam class, your Manager class that will check for spam, whatever you want to do there. In our case, we're just going to, we're going to dumb it down and say, well, it passes just as long as the value you typed in is not spam.
In our case, we're just going to, we're going to dumb it down and say, well, it passes just as long as the value you typed in is not spam. And of course, this is a placeholder for your spam detection layer. Finally, we could say the message is, get this spam out of here, fool. And that's it. You now have your custom validation rule, so, so easy in Laravel 5.5. So come back to Chrome, we're going to give this another run, type in anything, valid. But if we do it again and we enter literally spam, then of course it's going to pick up on that. And that's all there is to it.
