What is the correct way to automatically pass in a form value (on the backend)?

I'm using this, which doesn't feel secure:
Forms\Components\Hidden::make('company_id')
->required()
->default(auth()->user()->company_id),
Forms\Components\Hidden::make('company_id')
->required()
->default(auth()->user()->company_id),
Should I be setting something at the model level? I did search the docs, but wasn't quite sure where to look to find this.
3 Replies
Patrick Boivin
I guess it depends what you're doing with the value... what is it used for? If it's for saving the ID on the current record, a model observer is definitely better
benshawuk
benshawukOP2y ago
@pboivin Thanks! I'll look into model observers. @Dennis Koch - Thanks a lot - that worked perfectly! Final code (much securer, and does away with the hidden field!):
protected function getActions(): array
{
return [
Actions\CreateAction::make()
->disableCreateAnother(true)
->mutateFormDataUsing(function (array $data): array {
$data['company_id'] = auth()->user()->company_id;
return $data;
}),
];
}
protected function getActions(): array
{
return [
Actions\CreateAction::make()
->disableCreateAnother(true)
->mutateFormDataUsing(function (array $data): array {
$data['company_id'] = auth()->user()->company_id;
return $data;
}),
];
}
That's a super useful function actually.
Want results from more Discord servers?
Add your server