I’m using Filament PHP to build a form for managing users in my application. The specialization_id field in the form is a Select component, which depends on the user’s role (doctor). If the user’s role is doctor, the field should display a list of specializations and select the current specialization by default.
Here is my form schema for the specialization_id field:
FormsComponentsSelect::make('specialization_id')
->label('Specialization Name')
->relationship('doctor.specialization', 'name')
->required(fn (callable $get) => $get('role') === 'doctor')
->hidden(fn (callable $get) => $get('role') !== 'doctor')
->default(fn ($record) => $record->doctor->specialization_id ?? null),
The default() callback doesn’t seem to work. The dropdown for specialization_id appears when the role is doctor, but the default value is not set. Instead, the field is empty i.e “Select an Option”.
I verified that the relationship works correctly. When I dd($user->doctor->specialization) in the form, it returns the expected specialization.
Expected Behavior
The specialization_id dropdown should:
Default to the currently assigned specialization if the user already has one.