Can't get the field when disabled

I have made the employee_id selectable for role-based users. However, when the field is disabled, I can't get the employee_id in $data inside the handleRecordCreation().
Solution:
As noted you need to have disabled before dehydrated ie:
diabled()->dehydrated()
diabled()->dehydrated()
I use this combo all the time...
Jump to solution
12 Replies
cakan
cakan2y ago
That's because the field is disabled. Try making it readonly or hidden. Browsers don't submit disabled fields.
Skjena
SkjenaOP2y ago
That's a Select field and Select::readOnly does not exist.
cakan
cakan2y ago
You should add additional hidden field with employee_id value.
Skjena
SkjenaOP2y ago
Thanks for your suggestion. I have tried this also but it act same like as disabled. If the field was hidden also can't get that inside $data.
awcodes
awcodes2y ago
If it doesn’t change then you don’t need it in $data. It still exists on the $record and can be retrieved from there.
Skjena
SkjenaOP2y ago
Yeah actually i have not change the field and it was required field in my table. At the time of record creation that employee_id key was not there so it's returning error.
awcodes
awcodes2y ago
You can inject $operation into the callback for things like required() and disabled() to make it required on create and not on edit. https://filamentphp.com/docs/3.x/forms/advanced#injecting-the-current-form-operation
Hussain4real
Hussain4real2y ago
if a field is disabled and you still want the data use ->dehydrated()
Skjena
SkjenaOP2y ago
i have tried this also doesn't work. Here is my code snippet looks like
Select::make('employee_id')
->relationship(
'employee',
fn () => "full_name",
function ($query) {
return $query->limit(10);
}
)
->searchable()
->default(function () {
return auth()->user()?->employee?->id;
})
->dehydrated()
->disabled(!auth()->user()->hasRole(['manager', 'super-admin']))
->required()
->reactive()
Select::make('employee_id')
->relationship(
'employee',
fn () => "full_name",
function ($query) {
return $query->limit(10);
}
)
->searchable()
->default(function () {
return auth()->user()?->employee?->id;
})
->dehydrated()
->disabled(!auth()->user()->hasRole(['manager', 'super-admin']))
->required()
->reactive()
Hussain4real
Hussain4real2y ago
disabled before dehydrated
Solution
DariusIII
DariusIII2y ago
As noted you need to have disabled before dehydrated ie:
diabled()->dehydrated()
diabled()->dehydrated()
I use this combo all the time
Skjena
SkjenaOP2y ago
Thanks @Hussain4real and @DariusIII . It's working fine.

Did you find this page helpful?