Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Visible or Hidden Fieldset Based on Other Field

2024-05-19

If you want to show/hide a group of fields based on some other dropdown/radio value, you don't need to define visible/hidden for each field, you can make it a Fieldset.

If you want to show/hide the Fieldset based on value of another form field, you need to add ->live() to that field and add visible() or hidden() to the Fieldset with the condition.

That condition has $get() parameter that contains all the current values of the form, so you can check them and show/hide the Fieldset.

app/Filament/Resources/PropertyResource.php:

use App\Enum\PropertyType;
 
// ...
 
Forms\Components\Radio::make('property_type')
->options(PropertyType::class)
->default(PropertyType::SALE)
->inline()
->inlineLabel(false)
->live(),
Forms\Components\Fieldset::make('Sale Pricing')
->label('Pricing')
->visible(function (Forms\Get $get) {
return $get('property_type') == PropertyType::SALE;
})
->schema([
Forms\Components\TextInput::make('sale_price'),
Forms\Components\TextInput::make('down_payment'),
]),
Forms\Components\Fieldset::make('Rent Pricing')
->label('Pricing')
->visible(function (Forms\Get $get) {
return $get('property_type') == PropertyType::RENT;
})
->schema([
Forms\Components\TextInput::make('rent_price'),
Forms\Components\TextInput::make('deposit'),
]),

Additionally, this example contains Enum class for PropertyType, so if you're curious about this one:

app/Enum/PropertyType.php:

namespace App\Enum;
 
use Filament\Support\Contracts\HasLabel;
 
enum PropertyType: string implements HasLabel
{
case SALE = 'sale';
case RENT = 'rent';
 
public function getLabel(): string
{
return match ($this) {
self::SALE => 'Sale',
self::RENT => 'Rent',
};
}
}

A few of our Premium Examples: