JayAyAre
Filling a form on editPage
Forms\Components\TextInput::make('discount')
->live()
->numeric()
->minValue(0)
->minValue(0)
->maxValue(100)
->default(0),
Forms\Components\Hidden::make('total_price')
->default(0),
Forms\Components\Checkbox::make('budget_client_id')
->label('Client')
->live()
->default(false),
Forms\Components\Section::make('section')
->label('Client Information')
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\TextInput::make('last_name'),
Forms\Components\TextInput::make('email')
->required(fn(Forms\Get $get) => $get('budget_client_id')),
Forms\Components\TextInput::make('telephone'),
Forms\Components\TextInput::make('comments')
])->visible(fn(Forms\Get $get) => $get('budget_client_id'))
]);
}
and my edit code is:
public function form(Form $form): Form
{
return $form;
}
22 replies
Filling a form on editPage
public static function form(Form $form): Form
{
$modules = Module::all();
return $form
->schema([
Forms\Components\Select::make('modules')
->live()
->default('')
->required()
->multiple()
->options(fn() => $modules->pluck('name'))
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) use ($modules) {
$selectedModule = $modules->where('name', $state)->first();
if ($selectedModule) {
$discountedPrice = $selectedModule->price * (1 - $get('discount') / 100);
$set('total_price', $discountedPrice);
} else {
$set('total_price', 0);
}
}),
22 replies