<x-widget.show-if>
Show if Blade component for Laravel and Livewire
Shows part of a form only while another field has a given value: a phone number when Phone is chosen, a second address when a box is ticked, a text box for Other. Hidden, its fields don't submit and their required can't block the form. Starts in the right state after a failed submit, and works inside Livewire components.
php artisan bladewell:add show-if
- Works with
- Laravel 12 and 13, Livewire 3 and 4, Tailwind CSS 4.1+, PHP 8.3+
Usage
Validation
Hidden fields don't submit, so validate them only when they apply. exclude_unless also drops them from the validated data.
$request->validate([
'contact' => ['required', 'in:email,phone'],
'phone' => ['exclude_unless:contact,phone', 'required', 'string'],
]);
Livewire
In a Livewire component it follows the field the same way and keeps up with renders. Bind the field, and pass the starting state from the property, since there's no old input. A plain @if with wire:model.live works too, and asks the server each time.
<x-widget.radio name="contact" wire:model="contact" label="How should we contact you?" inline :options="['email' => 'Email', 'phone' => 'Phone']" />
<x-widget.show-if field="contact" value="phone" :shown="$contact === 'phone'">
<x-widget.phone name="phone" wire:model="phone" label="Phone number" />
</x-widget.show-if>
On the server
The show if can't stop a request that skips it. Here's what to check on your server.
Security
-
Hidden fields are disabled, so the browser doesn't send them, but a request can send any field anyway. Enforce the condition on the server:
required_if:for a field the choice makes required, and for a value sent when the condition is false, decide:exclude_unless:drops it from the validated data, as the example does, orprohibited_unless:refuses the form.Laravel: The required_if rule · Laravel: The exclude_unless rule · Laravel: The prohibited_unless rule
Examples
These examples also use Radio, Phone, Select, Text input, Checkbox and Textarea, which Show if doesn't need on its own. To use them as they are:
php artisan bladewell:add show-if radio phone select text-input checkbox textarea
Contact method
Each way of getting in touch shows its own field: an email address for Email, a number for Phone. A hidden one doesn't submit, and its required can't stop the form. Email is chosen to start with, so :shown says so from the server, with no flash before the script runs.
Show code Hide code
<form class="grid max-w-sm gap-6">
<x-widget.radio name="contact" label="How should we contact you?" value="email" inline :options="['email' => 'Email', 'phone' => 'Phone']" />
<x-widget.show-if field="contact" value="email" :shown="old('contact', 'email') === 'email'">
<x-widget.text-input name="email" type="email" label="Email address" required />
</x-widget.show-if>
<x-widget.show-if field="contact" value="phone">
<x-widget.phone name="phone" label="Phone number" required />
</x-widget.show-if>
</form>
Other option
A list of values shows it for any of them; here An event or Other asks for the details.
Show code Hide code
<form class="grid max-w-sm gap-6">
<x-widget.select name="heard_from" label="How did you hear about us?" placeholder="Choose one" :options="['search' => 'Search engine', 'friend' => 'A friend', 'event' => 'An event', 'other' => 'Other']" />
<x-widget.show-if field="heard_from" :value="['event', 'other']">
<x-widget.text-input name="heard_from_details" label="Tell us more" required />
</x-widget.show-if>
</form>
Different address
With no value, any value shows it: here, the box being ticked. Show-ifs nest, and an inner one hides with its outer.
Show code Hide code
<form class="grid max-w-sm gap-6">
<x-widget.checkbox name="ship_elsewhere" label="Ship to a different address" />
<x-widget.show-if field="ship_elsewhere" class="grid gap-6">
<x-widget.text-input name="shipping[street]" label="Street" autocomplete="shipping street-address" required />
<x-widget.checkbox name="shipping[gift]" label="It's a gift" />
<x-widget.show-if field="shipping[gift]">
<x-widget.textarea name="shipping[gift_message]" label="Gift message" />
</x-widget.show-if>
</x-widget.show-if>
</form>
Props
Other attributes, such as autocomplete or data-*, are passed through to the element. class styles the component's outer wrapper.
<x-widget.show-if>
| Prop | Default | Description |
|---|---|---|
| field | Required | The name of the field it follows, as on that field: "contact", "addons[]", "address[country]". Looked for in the same form, or the whole page outside one. |
| value |
null
|
The value, or a list of values, that shows it: "phone", or ['phone', 'sms']. Left out, any value shows it: a ticked checkbox, a chosen option, text typed in. |
| shown |
null
|
Shown to start with. Left out, it follows the field's old input, so after a failed submit what the person chose is still open. Pass it when the field's value comes from elsewhere: :shown="$user->contact === 'phone'". |
Accessibility
All 3 examples above are checked with axe-core against the WCAG 2.2 A and AA rules, in the light theme and the dark one, both as the page draws and with each popover, dialog, toast and tooltip opened, on every change. A change that fails can't be merged. Where axe can't decide, such as contrast on SVG text, the test measures the colours itself instead of letting it pass.
Automated checks can't judge everything: how it sounds in a screen reader, and how it feels to use from the keyboard, still need a person. Check those on your own pages too.
Source
What bladewell:add show-if writes to your app with the default namespaces. Prefer to copy by hand? Take these files, plus the ones from
Field, and the theme and base CSS.
resources/views/components/widget/show-if/index.blade.php Show
resources/js/widget/show-if/index.js Show