Playing
17. Fluent Email Validation Rule

Transcript

00:00
We now have a new fluent rule in Laravel. If you don't know what a fluent rule is, let's very quickly cover that first and then I'll show you the new one which is to do with email validation. Okay so what we see here is just the registration process for the Laravel Breeze Starter Kit, so pretty much what you would normally do anyway, and you'll notice here
00:20
that the password rule has this password default method call. What's actually happening here is behind the scenes this is going ahead and plucking out some sensible defaults like a minimum character length for your password, but what you can actually do with this rather than doing something like min 3 and then max 20 or whatever password rules you wanted to enforce, you can
00:45
fluently chain onto this password object what you want to see. So for example you could say min of you could say that you wanted this to be mixed case, you may want to check for compromised passwords in which case you can use uncompromised. There's a bunch of things that you can do with this, this isn't what has recently been added but we have a very similar thing now for email.
01:08
So if you weren't aware of that already, there we go, we've learned something else as well. I'm going to return that back to defaults and now we're going to look at the email fluent rule that's been added. So what we can do here is we can say rule and email or we can just say rules and email and from here we can just continue to chain on any of the usual things that we would
01:31
use to modify this email rule. Now actually just before we do that let's go ahead and look at what we would normally see. So for example what we could do is use DNS to do a DNS lookup on the domain. We can also use things like strict to enforce the validation of the actual email itself more strictly against an RFC. All of these are kind of optional but if you are using these you
01:56
can now do this fluently rather than using the colon and then having a comma separated list and this tends to read a little bit better as well and it kind of makes a little bit more sense. So let's go ahead and say rule and email and I'll show you, let's just do the MX record first of all. So validate MX record is the DNS flag that we just looked at. Basically what
02:18
this will do is it will just look up and make sure the domain is actually available for email just to give you that extra level validation. So this is going to work in exactly the same way. Let's go ahead and try and register an account here. I'm going to do this at a silly domain which we know doesn't probably exist and you can see here the email field must be a valid email
02:38
address. This will actually send a request now to go ahead and look this up. Now that functionality isn't new but we now have a fluent way to do this so it's a little bit clearer. We can also go ahead and use strict as well which will use that strict value that we provided before as a string to validate this a little bit more strictly according to a certain pattern. Of course you can play around
03:01
with these. This is really only useful if you're already using these because you can now do this fluently, read a little bit better and it's a little bit easier to add to if you need to. So there we go. I'm sure some more of these will pop up in future for other validation sets but for now that is the new fluid way to define email rules in Laravel.

Episode summary

In this episode, we're checking out the new fluent rule for email validation in Laravel. First, we quickly go over what "fluent rules" are—basically, it's a way of chaining methods together to define validation rules in a more readable and flexible way. If you've seen how password validation works, for example, with things like min, mixedCase, and checking for compromised passwords, it's very much the same idea!

Now, Laravel has added these fluent rules for email validation, too. Instead of having to use the old-school syntax like 'email:dns,strict', you can chain methods such as dns() and strict() directly onto your email rule. This makes your validation more expressive and easier to read. We go through some practical examples, like validating that the domain of an email actually exists (using DNS/MX checks), or enforcing strict RFC validation.

We also do a quick demo where we try registering with an invalid domain and get to see the validation error in action. If you're already using these options, switching to the fluent syntax will make your code a bit cleaner and simpler to extend in the future. That's the scoop on the new fluent-style email validation in Laravel!

Episode discussion

No comments, yet. Be the first!