Support the ongoing development of Laravel.io →

Laravel Advanced: Top 10 Validation Rules You Didn't Know Existed

12 Jul, 2024 2 min read

Do you know all the validation rules available in Laravel? Think again! Laravel has many ready-to-use validation rules that can make your code life a whole lot easier. Let’s uncover the top 10 validation rules you probably didn’t know existed.

1. Prohibited

Want to make sure a field is not present in the input? Use prohibited.

'username' => 'prohibited',

If username is included in the request, validation will fail. Simple and effective, especially for a honeypot!

2. Prohibits

Need a field to prohibit another field from being present? Check this out.

'password' => 'prohibits:username',

If password is present, username must not be.

3. Required If

This one’s a lifesaver when you need conditional validation.

'email' => 'required_if:contact_method,email',

The email field is required only if contact_method is email.

4. Required Unless

Opposite of required_if. Use it to require a field unless another field has a specific value.

'email' => 'required_unless:contact_method,phone',

Here, email is required unless contact_method is phone.

5. Required Without

This rule is great when you need a field only if another field isn’t present.

'email' => 'required_without:phone',

If phone isn’t provided, email must be.

6. Required Without All

Step up your game by requiring a field if none of the other specified fields are present.

'email' => 'required_without_all:phone,address',

If neither phone nor address is present, email is required.

7. Starts With

Check if a string starts with a given value.

'username' => 'starts_with:admin,user',

The username must start with either admin or user.

8. Ends With

Similarly, check if a string ends with a specific value.

'username' => 'ends_with:_admin,_user',

The username must end with either _admin or _user.

9. In Array

Confirm a field’s value exists in another array field.

'selected_option' => 'in_array:available_options.*',

The selected_option must be one of the values in the available_options array.

10. Different

Make sure two fields have different values.

'new_password' => 'different:current_password',

The new_password must be different from the current_password.

Wrapping Up

So there you have it, folks! Ten super handy Laravel validation rules you might not have known about. Using these can save you time and make your code cleaner and more efficient.

All the above have been previously shared on our Twitter, one by one. Follow us on Twitter; You'll ❤️ it.

You can also check the first article of the series, which is on the Top 5 Scheduler Functions you might not know about. Keep exploring, and keep coding with ease using Laravel. Until next time, happy coding! 🚀

Last updated 2 months ago.

driesvints, re-hopo-za liked this article

2
Like this article? Let the author know and give them a clap!

Other articles you might like

November 16th 2023

Create a Laravel package on your local environment

How to set up a local development environment to test your package classes or utilities within a lo...

Read article
September 22nd 2024

Receive Slack Notifications from your Laravel App with a 10-minute Setup

In the previous article, I introduced a Backpack's new Menu Dropdown Column component which I use fo...

Read article
September 10th 2024

Craft emails with Vue and Tailwind using Inertia Mailable

How to easily build dynamic email templates while keeping your Vue and Tailwind tools using the Iner...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.