Support the ongoing development of Laravel.io →

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

12 Jul, 2024 2 min read

Photo by Getty Images on Unsplash

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 3 weeks ago.

driesvints, re-hopo-za, alexigallegos, sergefenix liked this article

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

Other articles you might like

November 18th 2024

Laravel Custom Query Builders Over Scopes

Hello 👋 Alright, let's talk about Query Scopes. They're awesome, they make queries much easier to r...

Read article
November 19th 2024

Access Laravel before and after running Pest tests

How to access the Laravel ecosystem by simulating the beforeAll and afterAll methods in a Pest test....

Read article
November 11th 2024

🍣 Sushi — Your Eloquent model driver for other data sources

In Laravel projects, we usually store data in databases, create tables, and run migrations. But not...

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.