About Projects Resume Blog Contact

Create Custom Validation Rules In Laravel


This post is over a year old, so some information in the post may be out-dated or not relevant any more.

One thing I really love about Laravel is its built-in validation rules. Laravel makes it really easy to add validation rules and messages to your models. For most scenarios, using the built-in validation rules will be enough, but what if those rules aren't enough? That's where creating your own custom validation rules comes in to play.

Any time I need some kind of custom validation rule on an attribute, I make a custom validation rule because I can then reuse the rule on any other attribute that might need the same kind of validation.

Let's say you have a form where a user can update their password on some kind of profile page, but you don't want them to enter a password that contains their name. This could easily be accomplished with a custom validation rule.

The custom rule would look like this:

You could then use the validation rule in a request like this:

That's really all there is to creating a custom validation rule and using it. The rule shown above is an extremely simple example of what is possible with a custom validation rule.

For a more in-depth explanation on this take a look at a post by Taylor Otwell which goes into more detail on the subject.

Share this Post


Need a new website or application developed?

Contact me now