This episode is for members only

Sign up to access "Enums in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
04. Validating with enums in Laravel

Transcript

00:00
Now to my favorite part, which is validating enums. Now, obviously, we've already seen an example of the statuses where we can just enter anything into our database column, which
00:11
doesn't make any sense. So we've got this enum, but we don't have the benefit of validating it against the values listed within our enum.
00:20
Now, let's go ahead and just add some really simple validation here. So let's go ahead and validate this just directly within the controller when we store this.
00:27
And let's, of course, say that the status is required. And then we'll take a look at how we can check that the value that we're getting through from the form exists within that enum.
00:37
Now, Laravel has a specific validation rule to check that a value exists within an enum. But before we do that, just as a little exercise, we're going to see how we would do this previously,
00:50
so before Laravel implemented this functionality. Now, one of the rules that Laravel has and has had for a while is the in validation rule, which allows you to give a list of comma-separated values
01:04
to check whether the value you're getting in exists within here. So almost like doing an in array. Is this thing in this array?
01:12
So what we could actually do prior to this is if we wanted to hard code this in, we could add placed, shipped, and canceled inside of here. So placed, shipped.
01:23
And let's just leave off canceled so we can see the validation rule working. So if we head over to create, let's just add a little error message down here for the status
01:33
and just end that error. And we'll just put a div in here with the message. We won't bother styling it up. There we go.
01:41
OK, let's head over. Let's go ahead and click Place. That works nicely. If we choose canceled, though, we get
01:46
the selected status is invalid. So validation is working nicely, but we don't, of course, want to have to manually put these in here. That's the whole point of using an enum.
01:56
We want this to be in one place. So if we wanted to, we could go ahead and concatenate on an imploded list of statuses from our order status enum. Now, just to play around with this, how would we do this?
02:12
Well, for the order status, if we just pull that in, we know that we have got a list of cases. That gives us an array, and each of these things are enums themselves.
02:23
So let's just go and get rid of that, and let's have a look. So when I click on this, you can see that for each item in this array, this is an enum itself, of course, with the name and the value that we've created.
02:36
So what we can actually do from here, because each of these are enums, we can use Array Column here. And what that's going to do is allow us to grab the specific value out of each of these
02:47
without having to map through them. So this is the value that we want to check. So if we just give this a refresh, we've now got an array with just them values in.
02:56
That's perfect, because what we can then do is go ahead and implode this by a comma, because that's how Laravel's validation rule works here. And we should now have a comma-separated list of values.
03:09
So what we can do is just grab this and go ahead and, of course, concatenate it onto this rule, and that should work. It's not ideal, and it's not what
03:18
we're going to end up doing, but that should work nicely. So now for each of these, we can create them. But as soon as we deviate from the allowed values, if we just choose Placed as ABC, it doesn't let us do it.
03:31
So of course, we've looked at how we would previously do it, but with Laravel, we have a specific enum validation rule, which makes this incredibly easy. So let's go ahead and pull in this enum rule, new it up,
03:45
and then we're going to pass in the fully qualified class namespace to our order status enum. That is all we have to do. Laravel is now going to do all of the hard work
03:56
behind the scenes for us. So if we come over and have a look, you can see that works. And let's go over here and just change this over again. Hit Create, and there we go.
04:05
So that works in exactly the same way as we've just done, but of course, it's a lot cleaner. If you prefer, you can also pull this from the rule class. So you can say rule under validation and enum.
04:18
It really just depends on how you want to do this. And as you can see, it works in exactly the same way. If we try and modify this over, it's not going to work. So it's entirely up to you.
04:29
I prefer just newing this up, but it's up to you. So there we go. There is our validation, basically just making sure this value exists within order status.
04:38
And we now know that if we wanted to add another thing, there's no more work to do. We're listing through these values, we're validating them, and that's one of the greatest benefits of using enum here.

Episode summary

In this episode, we dive into validating enums in Laravel, and why it matters for keeping your data clean. We start off by looking at how, without validation, any value can sneak into our database—even ones that aren't part of our enum list. Not ideal!

First up, we look at the old way: using Laravel's in validation rule, where you manually list all valid statuses as comma-separated values. This works, but of course, it kind of defeats the point of having enums in the first place—you'd have to update the list everywhere if you change your enum.

To make this DRYer, we see how to build this list dynamically from the enum. We grab all the cases from the enum class, extract their values, and implode them into the format the in rule expects. Now, the validation will always keep up with your enum automatically!

But, Laravel's made things even easier for us. We check out the built-in Enum validation rule. With this, you just point the rule at your enum class, and Laravel takes care of the rest—you don't have to worry about keeping value lists in sync anymore. We check out a couple of ways to use it, both with the direct Enum rule and via Laravel's Rule class.

By the end of the video, you'll see how easy and clean it is to validate that incoming values actually match your enum, making your forms and data a lot more robust. Plus, adding new enum values in the future is basically zero-work. Handy!

Episode discussion

No comments, yet. Be the first!