Laravel 12 Conditional Validation required_if, nullable, and More

When I started using Laravel 12, validation was one of the first things I had to master. Especially conditional rules like `required_if` and `nullable` made my forms smarter and more flexible.

In this guide, I'll show you how to use these validation rules easily. We'll go through clear examples so you can use them right away in your Laravel projects.

Laravel 12 Validation: Master Conditional Rules like required_if, nullable, and More

Laravel 12 Validation: Conditional Rules like required_if, nullable

 

Let's dive into Laravel 12 Validation with some simple code!

$request->validate([
    'company_name' => 'required_if:is_employed,yes|nullable',
]);

 

What is Validation in Laravel 12?

Validation in Laravel 12 is the process of making sure the data coming into your application is clean, correct, and secure before using it. For example, when users fill out a form, we need to check if they entered the required information, like a valid email address or a minimum password length.

Laravel 12 makes validation very simple and elegant. It provides powerful methods like $request->validate() that allow us to quickly apply validation rules without writing long, complicated code.

This not only protects our applications from bad data but also improves user experience by giving instant feedback when something is wrong.

Simple Example:

$request->validate([
    'email' => 'required|email',
    'password' => 'required|min:8',
]);

 

What is Conditional Validation?

Conditional validation in Laravel 12 means applying validation rules based on the value of another input field. Sometimes, a field should only be required if a certain condition is true.

For example, if a user says they are employed, then we might require them to fill in their company name. If they are not employed, the company name can stay empty.

Instead of writing custom logic, Laravel lets us handle this easily using rules like required_if, required_unless, and others.

How to Use `required_if` in Laravel 12

Example:

$request->validate([
    'company_name' => 'required_if:is_employed,yes',
]);

 

How to Use nullable in Laravel 12

Example:

$request->validate([
    'middle_name' => 'nullable|string|max:255',
]);

 

Combining required_if and nullable

$request->validate([
    'company_name' => 'required_if:is_employed,yes|nullable|string|max:255',
]);

If condition matches, it's required; otherwise, it can be empty.

 

Other Conditional Rules

  1. required_unless
  2. required_with
  3. required_without
  4. sometimes

Example of required_unless: 

The field must be present unless another field has a certain value

$request->validate([
    'company_name' => 'required_unless:is_employed,no',
]);
  • If is_employed is not "no", then company_name is required.

  • If is_employed is "no", company_name can be empty.

Good for when the exception happens based on a specific value.

 

Example of required_with:

The field is required only if another specified field is present (not empty).

$request->validate([
    'state' => 'required_with:country',
]);
  • If country is filled in, then state must also be filled.

  • If country is not provided, state can be empty.

Useful when fields depend on each other (like country → state)

 

Example of required_without:

The field is required if another specified field is NOT present (empty).

$request->validate([
    'email' => 'required_without:phone',
]);
  • If phone is missing, email becomes required.

  • If phone is filled, email can be empty.

Good when you want to accept either of two fields (email or phone).

 

Example of sometimes:

$request->validate([
    'phone' => 'sometimes|required|numeric',
]);

Validate only if phone field is present

 

Conclusion

Conditional validation in Laravel 12 makes our apps smarter and more user-friendly. By using simple rules like `required_if` and `nullable`, we can easily handle different scenarios without writing complicated logic. Keep practicing these techniques and you'll master Laravel validation in no time!

 

Frequently Asked Questions

  1. How to make a field required based on another field in Laravel 12?

    Use the required_if rule:

    'company_name' => 'required_if:is_employed,yes'
  2. What is the use of nullable in Laravel validation?

    It allows a field to be empty without failing validation.

  3. Can we combine required_if and nullable together?

    Yes! Example:

    'company_name' => 'required_if:is_employed,yes|nullable|string'
  4. What is the difference between nullable and sometimes in Laravel validation?

    nullable: Field can be null.
    sometimes: Validation rules are applied only if the field exists in the input.

 

For more detailed information, refer to the official Laravel documentation on validation.


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS