---
title: "Conditional fields — Forms for Laravel | Roundly"
description: "Show or require a field only when another answer matches — hidden fields are skipped by validation automatically."
url: https://roundly-consulting.com/open-source/docs/forms-for-laravel/conditional-fields
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Forms for Laravel](https://roundly-consulting.com/open-source/docs/forms-for-laravel.md)

# 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:

```php
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

| Operator | Matches when the referenced answer… |
| --- | --- |
| `=` | equals the value (the default). |
| `!=` | differs from the value. |
| `in` | is one of the values in the array. |
| `not_in` | is none of the values in the array. |
| `filled` | is filled (the value is ignored). |
| `empty` | is 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:

```php
// 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](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/forms-for-laravel.md)
