F
Filament4mo ago
Wim

Conditionally showing TextInput or changing default

I want to show a different default value in the TextInput depending on the organization_type. I have therefore created two TextInput fields. I hide the TextInput depending on the type of organization. As such this works, but I also need the default value to be different depending on the organization type. While the TextInput is correctly chosen (e.g I can see that on the label), the default value of the first TextInput is always shown. So concrete: I have two types of organizations (e.g. an individual and a company). When organization type is individual, I want to show a textInput with the name of the individual. When organization type is company, I want to show a textInput with default value of the user name, with "'s Company" added to the default value Select::make('organization_type') ->live() ->options(OrganizationType::class) ->required() ->afterStateUpdated(function ($state, Set $set) { $set('organization_type', $state); }), TextInput::make('name') ->label('Organization Name') ->default( Filament::getUsername(auth()->user()) . " 's Company") ->visible(fn (Get $get): bool => $get('organization_type') === 'company' ), TextInput::make('name') ->label('Individual Name') ->default( Filament::getUsername(auth()->user()) ) ->visible(fn (Get $get): bool => $get('organization_type') === 'individual' ),
2 Replies
Wim
WimOP4mo ago
Anyone?
toeknee
toeknee4mo ago
Ok don't do thwat you ahve done.... handle it in the afterStateUpdated
Select::make('organization_type')
->live()
->options(OrganizationType::class)
->required()
->afterStateUpdated(function ($state, Set $set, $context) {
$company = Filament::getUsername(auth()->user()) . " 's Company";
$individual = Filament::getUsername(auth()->user());
$name = $state === 'company' ? $company : $name;
$set('name', $name);
}),
TextInput::make('name')
->live()
->lazy()
->label(fn($get) => $get('organization_type') === 'company' ? 'Organization Name' : 'Individual Name'),
Select::make('organization_type')
->live()
->options(OrganizationType::class)
->required()
->afterStateUpdated(function ($state, Set $set, $context) {
$company = Filament::getUsername(auth()->user()) . " 's Company";
$individual = Filament::getUsername(auth()->user());
$name = $state === 'company' ? $company : $name;
$set('name', $name);
}),
TextInput::make('name')
->live()
->lazy()
->label(fn($get) => $get('organization_type') === 'company' ? 'Organization Name' : 'Individual Name'),
Along those lines anyway
Want results from more Discord servers?
Add your server