Radio default not working

I have a Radio input in my form and using default isn't working for some reason:
Radio::make('access_option')
->default('specific')
->options([
'all' => 'All Locations',
'specific' => 'Specific locations',
'copy' => 'Copy location access from user',
'team' => 'Add all from specific team'
])
->required()
->live()
->afterStateUpdated(fn (Radio $component) => $component
->getContainer()
->getComponent('dynamicTypeFields')
->getChildComponentContainer()
->fill()),
Grid::make(2)
->schema(fn (Get $get): array => match ($get('access_option')) {
...
})->key('dynamicTypeFields')
Radio::make('access_option')
->default('specific')
->options([
'all' => 'All Locations',
'specific' => 'Specific locations',
'copy' => 'Copy location access from user',
'team' => 'Add all from specific team'
])
->required()
->live()
->afterStateUpdated(fn (Radio $component) => $component
->getContainer()
->getComponent('dynamicTypeFields')
->getChildComponentContainer()
->fill()),
Grid::make(2)
->schema(fn (Get $get): array => match ($get('access_option')) {
...
})->key('dynamicTypeFields')
Once I select an option everything works as expected, but on page load the default isn't there.
Solution:
Your comment got me on the right track. Found a github answer that indicated I need to set the value of the access_option array key before the form is filled, so I added this to my EditUser resource and now its working as expected: ``` protected function mutateFormDataBeforeFill(array $data): array {...
Jump to solution
6 Replies
toeknee
toeknee9mo ago
Does it work on creation form but not the edit form
Jon Mason
Jon Mason9mo ago
@toeknee It's just on the UserResource class but I guess it's when I'm editing a user. when creating a new user, it has a default
Solution
Jon Mason
Jon Mason9mo ago
Your comment got me on the right track. Found a github answer that indicated I need to set the value of the access_option array key before the form is filled, so I added this to my EditUser resource and now its working as expected:
protected function mutateFormDataBeforeFill(array $data): array
{
$data['access_option'] = 'specific';

return $data;
}
protected function mutateFormDataBeforeFill(array $data): array
{
$data['access_option'] = 'specific';

return $data;
}
Jon Mason
Jon Mason9mo ago
thank you!
awcodes
awcodes9mo ago
FYI. ->default() only works on create pages. On the edit page the form data is assumed to have already been saved with the default. That’s why you have to manually fill it.
Jon Mason
Jon Mason9mo ago
in hindsight, that makes perfect sense, thank you!