Assign default value on a field during AttachAction
In my RM form if I want to attach an employee, it will ask to select a status,
what I want to do is when I attach an employee
it will not ask to select for 'status' but rather set the 'status' to 'Pending'
by default.
AttachAction::make()
->label('Assign Employee') ->form(fn (AttachAction $action): array => [
Section::make() ->schema([ Select::make('recordId') ->options(Employee::all()->pluck('full_name', 'empl_id')) ->label('Employee') ->searchable() ->preload() ->multiple(), TextInput::make('assigned_by'), Select::make('status') ->options([ 'approve' => 'Approve', 'disapprove' => 'Disapprove', 'pending' => 'Pending', 'on-process' => 'On process', ])->required(),
]),
])
->label('Assign Employee') ->form(fn (AttachAction $action): array => [
Section::make() ->schema([ Select::make('recordId') ->options(Employee::all()->pluck('full_name', 'empl_id')) ->label('Employee') ->searchable() ->preload() ->multiple(), TextInput::make('assigned_by'), Select::make('status') ->options([ 'approve' => 'Approve', 'disapprove' => 'Disapprove', 'pending' => 'Pending', 'on-process' => 'On process', ])->required(),
]),
])
3 Replies
Updated the code but Im not sure if this is the correct way.
// Select::make('status')
// ->options([
// 'approve' => 'Approve',
// 'disapprove' => 'Disapprove',
// 'pending' => 'Pending',
// 'on-process' => 'On process',
// ])->required(),
Hidden::make('status')->default('Pending'),
Try using the option key/value which would be lower case pending in your case on the Select
If
default('pending')
is not working you can add this to the select
->afterStateHydrated(fn ($component) => $component->state('pending'))
What I want is emplement the AttachAction in a RelationManager and save the following
fields on the db
empl_id -- <w/ user interaction Section::make>
assigned_by -- <w/ user interaction TextInput::make>
status -- <w/o user interaction>
just set the 'status' field to 'Pending' every time I click
the attach button on a RM and save it to the DB...
somewhere in Edit menu, that's where a user interaction
for the 'status' field.
so in my case I comment out the Select::make('status') part
and add the line
Hidden::make('status')->default('Pending'),
this one work, but Im not sure if this is the proper way
thank you, I will read more about this on the documentation