Mastering Laravel 12 Conditional Validation
July 7, 2025Mastering Laravel 12 Conditional Validation
Laravel 12's validation system is super powerful, and conditional validation makes your forms smarter. With it, you can enforce rules based on other field values. For example, requiring a “company_name” field only if “employment_status” is “employed”, or a “credit_card_number” only when payment method is “credit_card”. This adds flexibility and improves user experience.
🔍 Why use conditional validation?
- Fields might not always be required.
- Makes forms dynamic and avoids unnecessary input.
- Perfect for multi-step forms or building smart APIs.
🛠 Laravel 12 tools for conditional validation
required_if
required_with
Rule::when()
1. Practical example with required_if
// In your Controller:
$request->validate([
'employment_status' => 'required|in:employed,unemployed',
'company_name' => 'required_if:employment_status,employed',
]);
If “employment_status” is “employed”, then “company_name” becomes mandatory.
2. Example using required_with
// In your Controller:
$request->validate([
'shipping_address' => 'required_with:delivery_method',
'delivery_method' => 'nullable|in:pickup,delivery',
]);
If the user picks a delivery method, the shipping address must be provided.
3. Example using Rule::when()
use Illuminate\Validation\Rule;
$request->validate([
'payment_method' => 'required|in:credit_card,paypal',
'credit_card_number' => [
Rule::when($request->input('payment_method') === 'credit_card', ['required', 'numeric', 'digits:16']),
],
]);
When payment method is “credit_card”, the number field must be a 16-digit number.
📌 Pro tips
- Move validation logic to a
FormRequest
class for cleaner controllers. - Use helper methods like
sometimes()
orbail()
for better rule control. - Write tests to confirm your validation works under various scenarios.
📚 Quick wrap‑up
Conditional validation makes forms intelligent and context-aware. Laravel 12 gives you multiple approaches, and with practical examples, you can pick the one that fits your needs most.
Blog
Laravel 12.16.0 - New Features for Developers
Jun 03, 2025
Laravel 12.16.0 - New Features for Developers 1. New Validation Rule: in_array_keys You can now validate that an array contains at least one of the...
React History – A Professional Developer’s Perspective
Jul 26, 2025
1. Origins: Born Inside Facebook In 2011, Facebook engineers faced the increasing complexity of building interactive UIs at scale. They developed...
Is Laravel Slow? Optimize Queries & Indexes for Maximum Performance
Jul 20, 2025
A detailed, example-rich guide to avoid slowdowns in Laravel apps by optimizing data retrieval and employing indexing smartly. 1. 🧠 Fetch Only...
Laravel 12: What’s New From 12.0 to 12.19 – A Complete Guide
Jul 20, 2025
🔧 1. Laravel 12.0 – Starter Kits & Core Changes Version 12.0 introduced modern starter kits for React, Vue, Livewire, plus integratio...
React Hooks Complete Guide
Jul 01, 2025
🎣 Complete React Hooks Guide with Practical Examples 🧠 useState What it does: Adds local state to a function component. Code Example: impo...
ECMAScript 2025 Detailed Update Guide for Frontend Developers
Jul 06, 2025
🔍 ECMAScript 2025 – Detailed Feature Guide All new ECMAScript 2025 features with code examples and explanation of their importance for front...
