Laravel 12 Conditional Validation required_if,nullable And More

In this Laravel tutorial titled “laravel 12 conditional validation required_if nullable and more”, you’ll learn step by step how to use built-in conditional validation rules like nullable, required_if, required_unless, required_with, required_without, required_with_all, required_without_all, exclude_if, exclude_unless in laravel 12 application with practical examples.


Conditional validation is one of Laravel’s most powerful features, allowing you to apply validation rules dynamically based on specific conditions. Conditional validation allows you to apply rules only when certain conditions are met. This makes forms more flexible and user-friendly while keeping your business logic clean. In this laravel tutorial, we will focus on required_if and other commonly used conditional rules, with examples of how to implement them in real-world Laravel 12 projects.

What is Conditional Validation?

Conditional validation means applying validation rules based on the value or presence of other fields in your request. Instead of writing complex custom logic, Laravel provides elegant built-in rules that handle most conditional scenarios.

Why Conditional Validation?

Not all form fields are required in every scenario. For example:

  • A company name may be required only if the user selects a business account type.
  • A phone number may be optional but must be validated if provided.
  • An email field may only need to be validated if the user chooses to update it.

Instead of writing custom logic, Laravel’s conditional validation rules solve these cases elegantly.

Benefits of Conditional Validation:

  • Cleaner Code: Avoid writing custom validation logic in controllers
  • Better User Experience: Only validate fields when necessary
  • Enhanced Security: Prevent malicious users from submitting incomplete data
  • Greater Flexibility: Handle complex validation scenarios elegantly

How to Use nullable Validation Rule in Laravel

The nullable rule allows a field to be empty (null), but if a value is provided, it must pass the given validations.

Key Characteristics

  • Field must exist in the request
  • Can be null or empty string (“”)
  • Other validation rules are skipped if value is null or empty
  • Must be used when you want optional fields with conditional validation

Example:

Suppose we have a form with an optional phone_number field. If the user enters it, we need to validate it as a 10-digit number.

$request->validate([
    'phone_number' => 'nullable|digits:10',
]);

  • If phone_number is not provided, no error will occur.
  • If phone_number is provided, it must be exactly 10 digits.

Read Also : Laravel 12 Custom Validation Rule with Parameters

How to Use required_if Validation Rule

The required_if rule makes a field required only when another field has a specific value. This is perfect for dynamic forms where certain fields depend on user choices.

Basic Syntax

'field_name' => 'required_if:other_field,value'

Example:

          $request->validate([
                'country' => 'required | numeric',
                'state'   => 'required_if:country,1| numeric',
                
            ]);  

Explanation:

  • country is required and must be numeric (for example, 1 = India, 2 = USA).
  • state is required only if country = 1.
  • If the user selects country 1, then they must also provide a numeric state.
  • If the user selects country 2, state is optional.

Multiple Values with required_if Validation

Sometimes you want state to be required for multiple countries (e.g., India = 1, USA = 2, Canada = 3).

Example

       $request->validate([
                'country' => 'required | numeric',
                'state'   => 'required_if:country,1,2,3 | numeric',
            ]);       

Explanation:

  • state is required if country is 1, 2, or 3.
  • This way, if the user selects India (1), USA (2), or Canada (3), then they must provide state.
  • If country = 4 (say Australia), then state is optional

Read Also : Laravel 12 Custom Conditional Validation With Other Fields

How to Use required_unless Validation Rule

This rule makes a field required unless another field has a specific value.

Basic Syntax

required_unless:anotherfield,value,...

Example

$request->validate([
    'subscription_type' => 'required|string',
    'payment_method'    => 'required_unless:subscription_type,free',
]);

Explanation:

  • If subscription_type = free, payment_method is not required.
  • For all other subscription types, payment_method must be provided.
  • If subscription_type = free, Laravel ignores payment_method entirely, so no validation errors.

The Common Issue

What if you want to validate payment_method further (e.g., ensure it’s a string with a minimum length)?

$request->validate([ 
    'subscription_type' => 'required|string', 
    'payment_method' => 'required_unless:subscription_type,free|string|min:10',
]);

But this will cause an error: “The payment method field must be a string.” even when subscription_type = free. That’s because string|min:10 is still applied unconditionally.

Laravel 12 Validation Rule required_unless common issue
Laravel 12 Validation Rule required_unless common issue

Correct Way to Combine Rules

$request->validate([ 
            'subscription_type' => 'required|string', 
            'payment_method' => 'required_unless:subscription_type,free|nullable|string|min:10',
         ]);

Explanation:
  • Adding nullable is key here.
  • Without nullable, Laravel tries to apply string|min:10 even when the field is missing.
  • With nullable, those rules only apply if the field is actually present.

How to Use required_with Validation Rule

This rule makes a field required if any of the listed fields are present.

Example

$request->validate([
            'password'              => 'required|string|min:6',
            'password_confirmation' => 'required_with:password|same:password',
        ]);

Explanation:

  • If password is present, password_confirmation becomes required.
  • The same:password rule ensures both fields match.

If you add nullable in place of required for password :

$request->validate([
            'password'              => 'nullable|string|min:6',
            'password_confirmation' => 'required_with:password|same:password',
        ]);
  • If password is missing, the validation rules for it are ignored.
  • If password is provided, then password_confirmation becomes required.

How to Use required_without Validation Rule

This rule makes a field required if any of the listed fields are not present.

Example:

$request->validate([
    'email' => 'required_without:phone',
    'phone' => 'required_without:email',
]);

Laravel 12 Validation Rule required_without preview
Laravel 12 Validation Rule required_without preview

Explanation:

  • If email is missing, phone is required.
  • If phone is missing, email is required.

This is often used when at least one field must be filled.

The Common Issue

Now suppose you want to further validate each field: email should be a valid email address and phone should be a 10-digit number.

You might write:

$request->validate([
            'email' => 'required_without:phone|string|email',
            'phone' => 'required_without:email|digits:10',
        ])

But the problem arises when a user provides only one of them:

Laravel 12 Validation Rule required_without common issue
Validation Rule required_without common issue
  • If the user enters only a valid phone number and leaves email empty Laravel may still try to validate the email field with string|email and throw an error : “The email field must be a string”.
  • Similarly, if the user enters only a valid email and leaves phone empty, Laravel may try to validate the phone field with digits:10 and fail.

This happens because Laravel applies all rules to the field, even if the field is empty, unless you explicitly allow it to be nullable.

Correct Way to Combine Rules

$request->validate([
            'email' => 'required_without:phone|nullable|string|email',
            'phone' => 'required_without:email|nullable|digits:10',
        ]);

Adding nullable allows the field to be empty without triggering validation errors.

  • string and email are only checked if the field is present.
  • digits:10 is only checked if the phone field is present.

When using conditional validation rules like required_without, always include nullable for fields that may not be submitted.

This prevents unconditional rules (string, email, digits) from failing when the field is missing.

How to Use required_with_all Validation Rule

The required_with_all rule specifies that a field is required only if all of the listed fields are present in the request.

Example:

$request->validate([
    'address'     => 'string|max:255',
    'city'        => 'string|max:100',
    'postal_code' => 'required_with_all:address,city|string|max:10',
]);

Explanation:

  • If both address and city are provided, then postal_code is required.
  • If only one of them is filled, postal_code is not required.

The Common Issue

When using the required_with_all rule like above, you encounter unexpected validation errors. If the fields (address,city) are left empty, the validation above still trigger errors as shown in image.

laravel 12 conditional validation required_if,nullable and more
Validation Rule required_with_all common issue

Correct Way to Combine Rules

$request->validate([
            'address'     => 'nullable|string|max:255',
            'city'        => 'nullable|string|max:100',
            'postal_code' => 'required_with_all:address,city|nullable|string|max:10',
        ]);

To resolve this issue, all related fields should be defined as nullable. The reason is that if address and city are set as nullable but postal_code is not, Laravel will still attempt to validate postal_code against the string and max rules, leading to undesired errors. By making all fields nullable, Laravel will enforce the postal_code requirement only when both address and city are provided, while still allowing those fields to remain optional.

postal_code is required only if both address and city are submitted.

How to Use required_without_all Validation Rule

This rule makes a field required if none of the listed fields are present.

Example:

$request->validate([
    'email' => 'required_without_all:phone,username|string|email',
    'phone' => 'nullable|string',
    'username' => 'nullable|string',
]);

Laravel 12 Validation Rule required_without_all preview
Validation Rule required_without_all preview

Explanation:

  • If the user does not provide phone and username, then email is required.
  • If the user provides a phone, then email is not required.
  • If the user provides a username, then email is not required.
  • If the user provides both phone and username, email is still not required.

At least one of email, phone, or username must be filled.

The Common Issue

In this case, if the user leaves the email field empty, Laravel will still try to validate it with string|email, this leads to an unwanted validation error as given below:

Laravel 12 Validation Rule required_without_all common issue
Validation Rule required_without_all common issue

Correct Way to Combine Rules

$request->validate([
    'email' => 'required_without_all:phone,username|nullable|string|email',
    'phone' => 'nullable|string',
    'username' => 'nullable|string',
]);

  • Adding nullable tells Laravel: “It’s okay if this field is empty.”
  • Now, string|email rules will only apply if the email field actually contains a value.
  • At the same time, required_without_all:phone,username ensures that if neither phone nor username is present, email must be provided.

How to Use exclude_if Validation Rule

This rule removes a field from validation if another field has a specific value.

Example:

$request->validate([
    'status'     => 'required|string',
    'reason'     => 'exclude_if:status,active|string|max:255',
]);

Explanation:

  • status required and must be a string Example values: active, inactive, pending.
  • reason validated only if status is not active
  • If status = active reason is excluded from validation.
  • If status is inactive or pending: reason must be a string and maximum length: 255 characters.

How to Use exclude_unless Validation Rule

This rule removes a field from validation unless another field has a specific value.

$request->validate([
    'status' => 'required|string',
    'reason' => 'exclude_unless:status,inactive|string|max:255',
]);

Explanation:

  • If status = active, the reason field will be excluded from validation.
  • Otherwise, reason will be validated normally.

Key Difference Between exclude_if and exclude_unless

  • exclude_if excludes a field when another field matches a value.
  • exclude_unless excludes a field unless another field matches a value.

Conclusion

In this Laravel 12 tutorial, we explored conditional validation rules such as nullable, required_if, required_unless, required_with, required_without, required_with_all, required_without_all, exclude_if, and exclude_unless with practical examples.

These rules make your forms smarter and more dynamic by applying validation only when necessary. Instead of writing complex custom logic, you can use Laravel’s built-in rules to handle most real-world scenarios with just a few lines of code.

By using conditional validation effectively, you can achieve:

  • Cleaner and more maintainable code – less custom validation logic in controllers.
  • Better user experience – users only see relevant validation messages.
  • Increased flexibility – easily handle different conditions for optional and required fields.
  • Stronger data validation – ensures that the submitted data always follows your business rules.

Whether you’re building user registration forms, account management systems, or complex business applications, mastering these rules will help you create robust, user-friendly, and secure Laravel applications.