Single FileUpload inside state path saves differently as json

Not sure if this is intended behaviour but the file upload with single upload is saving as json with a random key when it's used inside a statePath. E.g image = "icon1.svg" extra = "{ 'image': {'123':'icon2.svg'} }"
Forms\Components\FileUpload::make('image')->image(),

Forms\Components\Grid::make()
->statePath('extra')
->schema([
Forms\Components\FileUpload::make('image')->image(),
])
Forms\Components\FileUpload::make('image')->image(),

Forms\Components\Grid::make()
->statePath('extra')
->schema([
Forms\Components\FileUpload::make('image')->image(),
])
Tried to fix with afterStateUpdated but it's never called.
->afterStateUpdated(static function (BaseFileUpload $component, $state) {
///....
if ($component->isMultiple()) {
$component->state([(string) Str::uuid() => $state]);
} else {
$component->state($state);
}
})
->afterStateUpdated(static function (BaseFileUpload $component, $state) {
///....
if ($component->isMultiple()) {
$component->state([(string) Str::uuid() => $state]);
} else {
$component->state($state);
}
})
Solution:
since the file upload depends on those uuids to function properly, it would be better to format the form data before create / save instead of at the field level.
Jump to solution
8 Replies
awcodes
awcodes15mo ago
This is intended.
exactwebitesolutions
What's the idea behind it when it's only allowing one file to be uploaded? Can it be overwritten so it only saves the file path?
awcodes
awcodes15mo ago
I think you're maybe missing the idea of how the state and statePaths relate to the actual model and how the data gets saved to the db. in your case, 'extra' should be a field in your DB for that model. and since it's a grid, the data has to be stored as json / array
exactwebitesolutions
Is there a way of overwriting how a state path is saved? I only want the json to be "{ 'image': 'icon2.svg' }" I thought Grid component was purely for display purposes.
awcodes
awcodes15mo ago
Layout components can also be used to group fields for relationships and since you aren't using a relationship it defaults to a json / array.
Solution
awcodes
awcodes15mo ago
since the file upload depends on those uuids to function properly, it would be better to format the form data before create / save instead of at the field level.
exactwebitesolutions
I understand why it does it now thanks for explaining. Will look into doing it that way.
Want results from more Discord servers?
Add your server