F
Filamentβ€’5mo ago
MZX

Conditional Fields

What is wrong here? This isn't working. When the user selects the tier, I want the price to be set automatically.
Forms\Components\Select::make('tier')
->options(Tier::class)
->required()
->reactive()
->afterStateUpdated(function ($state, callable $set) {
switch ($state) {
case 'standard':
$set('price', 14.75);
break;
case 'advanced':
$set('price', 26.54);
break;
case 'vip':
$set('price', 36.87);
break;
}
}),
Forms\Components\TextInput::make('price')
->label('Price (€)')
->numeric()
->disabled(),
Forms\Components\Select::make('tier')
->options(Tier::class)
->required()
->reactive()
->afterStateUpdated(function ($state, callable $set) {
switch ($state) {
case 'standard':
$set('price', 14.75);
break;
case 'advanced':
$set('price', 26.54);
break;
case 'vip':
$set('price', 36.87);
break;
}
}),
Forms\Components\TextInput::make('price')
->label('Price (€)')
->numeric()
->disabled(),
Solution:
@MZX not related to the question, but match is nicer than switch πŸ˜‰ ```php $price = match ($state) { 'standard' => 14.75, 'advanced' => 26.54,...
Jump to solution
7 Replies
BKF Dev
BKF Devβ€’5mo ago
Is tier an Enum ? if so, you should set cases as this : case Tier::Standard : ...
Adnan Yalahow
Adnan Yalahowβ€’5mo ago
make the price ->reactive too
BKF Dev
BKF Devβ€’5mo ago
No, no need πŸ™‚
MZX
MZXOPβ€’5mo ago
@BKF Dev didn't work
Solution
ericmp
ericmpβ€’5mo ago
@MZX not related to the question, but match is nicer than switch πŸ˜‰
$price = match ($state) {
'standard' => 14.75,
'advanced' => 26.54,
'vip' => 36.87,
};

$set('price', $price);
$price = match ($state) {
'standard' => 14.75,
'advanced' => 26.54,
'vip' => 36.87,
};

$set('price', $price);
or inline:
$set('price', match ($state) {
'standard' => 14.75,
'advanced' => 26.54,
'vip' => 36.87,
});
$set('price', match ($state) {
'standard' => 14.75,
'advanced' => 26.54,
'vip' => 36.87,
});
Trungnhq
Trungnhqβ€’5mo ago
if you use Filament V3, use live() instead of reactive()
MZX
MZXOPβ€’5mo ago
This is what worked! Thanks a lot buddy Both seem to work, but I guess live is the newer one so I'm going with that
Want results from more Discord servers?
Add your server