The default value is not set in the select field with any name
The default value is not set in the select field with any name when editing a record.
I need to add a select field to a form. This field is not part of the model. I need it to substitute options in another field that belongs to the model.
Select::make('my_custom_name_filed')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->default('reviewing')
->live();
Select::make('course_id')
->options(fn(Get $get): Collection => Course::query()->where('my_custom_name_filed', $get('my_custom_name_filed'))->orderBy('number')->pluck('number', 'id'));
6 Replies
I don't understand why the default value is not set on the edit page. Does anyone have any thoughts?
Solution to the problem in Pages\Edit*.php:
protected function mutateFormDataBeforeFill(array $data): array
{
$data['my_custom_name_filed'] = 'reviewing';
return $data;
}
If you know a better and more correct solution, please write about it.The data is filled from the record model that is why, so if it doesn't exist it won't fill
Otherwise, we would need to get the record, first build a default array, then merge and fill which is overly complicated.
This is why we have a mutator π
Why does ->default(...) only work on the record creation page, but not on the edit page?
I've just said above....
When you create it doesn't fill with data
when you fill with data the custom field doesn't exist so no value is set
Oh, you meant default. Thank you. I understand what you mean.
Great