Why Nova needs validation0:00
Look, I agree. Nova can feel like black magic. But it's important to remember it's not black magic. It's PHP and Vue. PHP and Vue. That's it. It doesn't know the intentions of your application. So it can be tempting to throw some fields into a resource and think, well, I'm not going to have any bad actors. I don't need to validate any data going in. But the bottom line is, if you put bad data into Nova, you'll get bad data out. It's not going to automatically protect you. It does not know your intentions. Let me try and illustrate this using our book resource. If I head down to the Pages field here, I'm able to update this to a negative number, something like minus 10. Now, obviously, we know full well a book can have a minimum of one page. But Nova doesn't know that. Nova doesn't know that this has to have a minimum or maximum number of pages. We have to tell it that using
How bad input happens0:52
a book can have a minimum of one page. But Nova doesn't know that. Nova doesn't know that this has to have a minimum or maximum number of pages. We have to tell it that using validation rules. But it goes even further. Let's say I'm a bad actor. I'm going to right click on this field and inspect the element. Now, of course, because we've said it's a number field, Nova has used a number HTML input type. But I can change this to text. Nova won't stop me doing that. And then I can change this to something like I'm a hacker. If I do that, and click Update and continue editing, note that I don't get a validation error, I get a SQL error. If you are ever submitting a form and get a SQL error, you should know that something's gone wrong. SQL errors should be a last, last, last, last, last resort. We should have caught that exception much earlier on turned it into a validation.
Adding pages field rules1:38
should know that something's gone wrong. SQL errors should be a last, last, last, last, last resort. We should have caught that exception much earlier on turned it into a validation error and returned a problem to the browser. Now, thankfully, validation is super easy to add to our fields in Laravel Nova. So let's go ahead and update our pages field to support validation. I'm going to chain on another method to our pages field. Hopefully you're getting used to chaining methods onto fields now. And the first thing I'm going to say is that it's a required field. Now you may think, well, Luke, haven't we already declared the required method on this field? We have. However, that required method does one thing and one thing only. And that is add this asterisk to the label. In fact, if we've declared a required validation rule, we don't even need to include the required method. If I refresh
and one thing only. And that is add this * to the label. In fact, if we've declared a required validation rule, we don't even need to include the required method. If I refresh this page, note that our pages field still has the *. And that's because Nova's smart enough to look at our field and understand, okay, it's got a required validation. It must be required. I'll add the *. So only in a few select circumstances, should you actually need to use the required method? Obviously we want to declare that it has to be an integer that will stop that SQL error from happening if we have a bad actor. And we can also declare that it has a minimum of 1, it can't be lower than 1 and a max of 10,000. Let's say I'm sure there are books larger than 10,000 pages, but not in our library at the moment. With that in place, we should actually have a working validation. If I try to update this book again, I will get a
there are books larger than 10,000 pages, but not in our library at the moment. With that in place, we should actually have a working validation. If I try to update this book again, I will get a validation error. As you can see, I scroll down, we have this highlighted in red and the validation error here at the bottom. If I try to change this, say to 10,001 and try and update again, I get the opposite error because of my maximum. And if I try to be a bad actor and change this to a text type, instead of a number type, let's again, enter I'm a hacker and try an update. I'm going to get a validation error saying it must be an integer. So hopefully that shows how important validation is in Nova, but also how easy and consistent validation is with what you're used to inside standard Laravel applications. If you've done validation before, you already know how to do it inside Nova. There is one little nuance with validation in Nova that I'd like to
Unique title rule nuance4:00
to inside standard Laravel applications. If you've done validation before, you already know how to do it inside Nova. There is one little nuance with validation in Nova that I'd like to show you. And we'll use the title field to demonstrate this. Let's say that every title in our library has to be unique. You're probably used to doing something similar with user emails inside Laravel applications. Well, how do you do that? You use the unique rule, don't you? So let's go ahead and do that now. I'll come on up to the title. I'll add a rules method. We'll add the basics. It's required. It has to be a string. We can add a min of one character and a max of 255 characters. Again, because we've declared a required validation rule, we don't need to use the required method. And then maybe we could say, yeah, it has to be unique inside the books table using the title column. If I jump back into my resource and update this, I'm going to get an
Creation vs update rules4:46
the required method. And then maybe we could say, yeah, it has to be unique inside the books table using the title column. If I jump back into my resource and update this, I'm going to get an error. And that's because well, 1984 does already exist in our database. It's this book. Well, how do we handle that inside standard Laravel? In an update form request, you've likely added the except property to a unique validation rule to ignore the current resource. Nova has a way of allowing us to do that as well. But we also need a way to separate rules for creating a book and rules for updating a book. Let's take this validation rule here and remove it. These rules are going to be the same across both creation and updating. So we can leave that in place. I'm going to chain the creationRules method on which will only be executed when we're creating the resource. And I can simply paste in our unique rule there because it makes sense in that context. Then I'll
to chain the creation rules method on which will only be executed when we're creating the resource. And I can simply paste in our unique rule there because it makes sense in that context. Then I'll chain on an update rules method, which as you've probably guessed, will only be executed when updating this resource. And here I can use the unique books title rule again, but I'm going to add an extra bit on the end of this rule, which is this resource ID placeholder that Nova provides. So double curly braces, resource ID, and then two closing curly braces. And Nova is going to replace this with the current resource ID when executing these validation rules. This placeholder will work on any validation rule where you can specify a model ID in order to alter the validation rule logic. If I add that, everything should now work. I can update and continue. Oh, hold on. I just need to alter the number of pages to an actual valid value. Now let's say a hundred pages.
rule logic. If I add that, everything should now work. I can update and continue. Oh, hold on. I just need to alter the number of pages to an actual valid value. Now let's say a hundred pages. Yeah, I can now update and continue editing. I get no error for the title, but if I change this, say to Emma, which is another book inside our library, I get the error. The title has already been taken. So super useful that Nova allows us to share rules across creation and updating. But for those few instances where they diverge, you can make use of the creationRules method and the updateRules method in order to specify separate rules for each of those instances, which makes validating fields in Nova a breeze. The bottom line of this episode is if you know how to do validation in Laravel, if you've done validation in controllers and form requests, you know how to do validation in Nova. You just add those exact same rules that you're familiar
Validation recap takeaway7:22
how to do validation in Laravel, if you've done validation in controllers and form requests, you know how to do validation in Nova. You just add those exact same rules that you're familiar with to the rules method, to the creation rules method, and to the update rules method. And that's it. It's as simple as that. With just a few lines, you can ensure that the data that goes into your administration panel is valid and your application will work as expected.
