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 1 month 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

August 21st 2024

Find Outdated Composer Dependencies Using "composer outdated"

Introduction When building your PHP web applications, it's important to keep your dependencies up-to...

Read article
August 20th 2024

PHP 8.4 Property Hooks

Introduction PHP 8.4 will be released in November 2024 and will be bringing a cool new feature: prop...

Read article
August 19th 2024

New Array Functions in PHP 8.4

Introduction PHP 8.4 is set to be released in November 2024 and will introduce some handy new array...

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.