F
Filament4mo ago
Skjena

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
cakan4mo ago
That's because the field is disabled. Try making it readonly or hidden. Browsers don't submit disabled fields.
Skjena
Skjena4mo ago
That's a Select field and Select::readOnly does not exist.
cakan
cakan4mo ago
You should add additional hidden field with employee_id value.
Skjena
Skjena4mo 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
awcodes4mo 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
Skjena4mo 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
awcodes4mo 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
Hussain4real4mo ago
if a field is disabled and you still want the data use ->dehydrated()
Skjena
Skjena4mo 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
Hussain4real4mo ago
disabled before dehydrated
Solution
DariusIII
DariusIII4mo ago
As noted you need to have disabled before dehydrated ie:
diabled()->dehydrated()
diabled()->dehydrated()
I use this combo all the time
Skjena
Skjena4mo ago
Thanks @Hussain4real and @DariusIII . It's working fine.
Want results from more Discord servers?
Add your server
More Posts