FilamentF
Filament2y 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(),
Solution
@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);

or inline:
$set('price', match ($state) {
    'standard' => 14.75,
    'advanced' => 26.54,
    'vip' => 36.87,
});
Was this page helpful?