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 
or inline:
matchmatch is nicer than switchswitch $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,
});