NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Forms for Laravel

Conditional fields

Show or require a field only when another field of the same form — in any group — matches a value. The condition is stored on the field and enforced during validation — a hidden field is skipped (and whatever the request sent for it is not stored), and a conditionally-required field is enforced only when its condition is met:

Forms::define('signup', 'Sign up')
    ->group('step-1', 'Step 1', function (GroupBuilder $g): void {
        $g->field('country', 'Country');
        $g->field('age', 'Age')->number();
    })
    ->group('step-2', 'Step 2', function (GroupBuilder $g): void {
        $g->field('state', 'State')->requiredWhen('country', 'US');        // a field of step 1
        $g->field('guardian', 'Guardian')->visibleWhen('age', [16, 17], 'in');
        $g->field('zip', 'ZIP')->visibleWhen('step-1.country', 'US');      // named exactly
    })
    ->create();

requiredWhen() adds the required rule and stores the condition; visibleWhen() stores the condition only. Both take the referenced field’s key, the value, and an optional operator. A bare key is looked up in the dependent field’s own group first, then in the form’s other groups (by group order) — so a later step can depend on an earlier one. Write group_key.field_key to name the field exactly when two groups share the key.

Operators

OperatorMatches when the referenced answer…
=equals the value (the default).
!=differs from the value.
inis one of the values in the array.
not_inis none of the values in the array.
filledis filled (the value is ignored).
emptyis blank (the value is ignored).

How it’s stored

Conditions are plain data on the field, so they survive in config definitions and API output. A field can carry several conditions — it’s visible only when all of them match:

// visibleWhen('age', [16, 17], 'in') stores this on the field's `conditions` column:
[
    ['field' => 'age', 'operator' => 'in', 'value' => [16, 17]],
]

$field->isVisible($request->all()); // evaluates every stored condition

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.