Laravel Magazine
Rule::when(): Conditional Validation Without the If-Statement Soup

Rule::when(): Conditional Validation Without the If-Statement Soup

Eric Van Johnson ·

Conditional validation usually starts small and gets ugly fast:

$rules = [
    'shipping_method' => ['required', 'in:standard,express'],
];

if ($request->input('shipping_method') === 'express') {
    $rules['express_reason'] = ['required', 'string', 'max:255'];
}

if ($request->user()->isBusinessAccount()) {
    $rules['tax_id'] = ['required', 'string'];
}

$validated = $request->validate($rules);

It works, but the actual rule set for a field is now split between the $rules array and a handful of if blocks above it. Rule::when() puts the condition directly next to the rule it controls.

The basic shape

use Illuminate\Validation\Rule;

$request->validate([
    'shipping_method' => ['required', 'in:standard,express'],
    'express_reason' => Rule::when(
        $request->input('shipping_method') === 'express',
        ['required', 'string', 'max:255'],
    ),
    'tax_id' => Rule::when(
        $request->user()->isBusinessAccount(),
        ['required', 'string'],
        ['nullable'],
    ),
]);

The third argument is the rule set applied when the condition is false — useful for explicitly marking a field nullable rather than leaving it unvalidated.

Using a closure for lazily-evaluated conditions

If checking the condition is expensive (a query, an API call), pass a closure instead of a pre-computed boolean so it's only evaluated when needed:

'tax_id' => Rule::when(
    fn () => $request->user()->isBusinessAccount(),
    ['required', 'string'],
),

Inside a Form Request

This is where Rule::when() earns its keep, since Form Request rules() methods are exactly where this kind of conditional logic tends to accumulate:

class UpdateShipmentRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'shipping_method' => ['required', 'in:standard,express'],
            'express_reason' => Rule::when(
                $this->input('shipping_method') === 'express',
                ['required', 'string', 'max:255'],
            ),
            'signature_required' => Rule::when(
                $this->input('shipping_method') === 'express',
                ['required', 'boolean'],
                ['prohibited'],
            ),
        ];
    }
}

Notice signature_required uses prohibited in the false branch — actively rejecting the field rather than silently ignoring it, which catches a client sending data your form doesn't expect for that shipping method.

When to reach for sometimes() instead

Laravel's older $validator->sometimes() API solves a similar problem and is still around, but it operates on a Validator instance after the fact, which means splitting your rule definition across two method calls. Rule::when() keeps the condition and the rule inline in the array, which reads better in a Form Request and is easier to unit test in isolation — you can call the condition's closure directly without instantiating a validator at all. For new code, Rule::when() is the one to reach for.

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.