Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

El Klo's avatar
Level 11

Set description for a Filament form based on user selection

I have the following form in my Filament ReportResource:

public static function getForm(): array {
		return [
			Section::make('Details')
				->description('MAKE DYNAMIC')
				->aside('right')
				->columns(1)
				->collapsible()
				->icon('heroicon-o-information-circle')
				->schema([
					Fieldset::make('Locatie')
					        ->schema([
						        TextInput::make('name')
						                 ->label('Naam')
						                 ->columnSpanFull(),
						        TextInput::make('town')
						                 ->label('Plaats')
					        ]),

					Select::make('client_id')
					      ->relationship('client', 'name')
					      ->searchable(['name'])
					      ->preload()
					      ->required(),

					Select::make('species')
					      ->label('Soort')
					      ->enum(Species::class)
					      ->options(Species::class)
					      ->searchable(),

				]),
		];
}

I would like to update the description from the details section based on what the user selects in the client_id select. So, if they select a certain option in the client_id select, the description updates automatically with info on the Client model.

How can i achieve this?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To achieve dynamic updating of the section description based on the user's selection in the client_id select field, you can use Livewire's reactive properties and event listeners. Since Filament uses Livewire components under the hood, you can leverage Livewire's features to accomplish this.

Here's how you can modify your ReportResource form to update the section description dynamically:

use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Fieldset;
use Filament\Resources\Form;
use App\Models\Client;

public static function form(Form $form): Form
{
    return $form->schema([
        Section::make('Details')
            ->description(fn (callable $get) => $get('selectedClientDescription') ?? 'Default description')
            ->aside('right')
            ->columns(1)
            ->collapsible()
            ->icon('heroicon-o-information-circle')
            ->schema([
                // ... other fields ...

                Select::make('client_id')
                    ->relationship('client', 'name')
                    ->searchable(['name'])
                    ->preload()
                    ->required()
                    ->reactive()
                    ->afterStateUpdated(function ($set, $state) {
                        $client = Client::find($state);
                        $description = $client ? $client->description : 'Default description';
                        $set('selectedClientDescription', $description);
                    }),

                // ... other fields ...
            ]),
    ]);
}

In this solution, we've done the following:

  1. Added a description method to the Section component that uses a callable to get the dynamic description. The callable uses $get to retrieve a value from the component's state.
  2. Made the client_id select field reactive so that it triggers an update to the Livewire component's state when its value changes.
  3. Added an afterStateUpdated callback to the client_id select field. This callback is executed whenever the select field's value changes. Inside the callback, we find the Client model based on the selected client_id and set the description accordingly. If no client is found, we default to a 'Default description'.
  4. We use $set to update a custom property selectedClientDescription in the component's state, which is then used by the description method of the Section component.

Make sure that the Client model has a description attribute or a method that returns the desired description text.

Please note that the description method of the Section component expects a string, so ensure that the description attribute or method of the Client model returns a string.

This solution assumes that you have a Livewire component for your ReportResource form and that the Client model has a description attribute or method. Adjust the code as necessary to fit your actual model structure and attributes.

1 like

Please or to participate in this conversation.