F
Filament4w ago
Buda

JSON keys get overriden if they are not used in a form.

I am trying to modify some keys in a json array for a Spatie Media item. The json array has values that are not exposed to the front end.
Textarea::make('custom_properties.caption')
->label('Caption')
->columnSpanFull(),

TextInput::make('custom_properties.alt_text')
->label('Alt text')
->columnSpanFull(),
Textarea::make('custom_properties.caption')
->label('Caption')
->columnSpanFull(),

TextInput::make('custom_properties.alt_text')
->label('Alt text')
->columnSpanFull(),
I have a custom_properties.width / custom_properties.height that is being set through a job, so these values don't make sense to be a part of the form builder. When I save my model, the entire json array is overriden instead of just the keys I am accessing. I would love to know if this is a bug or if I missing something to tell filament to only update those keys.
Solution:
I appreciate the reply! I may look into making an issue for it and see if I can make a PR for it. For future people looking at this, this is how I solved it: ```...
Jump to solution
4 Replies
Dennis Koch
Dennis Koch4w ago
I don't think it's a bug. It's probably just filling the whole array. So if you want to keep data that's not in the form you probably need to backfill those via mutateDataBeforeSave
Buda
Buda4w ago
I think this could be labeled as a bug. I can't imagine a usecase where someone would be editing a specific key in an array and would want the entire array to be overriden.
Dennis Koch
Dennis Koch4w ago
I agree that it might not be expected. In the end this is just a simple Model::update(..). If the data is not there it will be overwritten.
Solution
Buda
Buda4w ago
I appreciate the reply! I may look into making an issue for it and see if I can make a PR for it. For future people looking at this, this is how I solved it:
->mutateFormDataUsing(function ($record, array $data): array {
$data['custom_properties'] = array_replace($record->custom_properties, $data['custom_properties']);

return $data;
}),
->mutateFormDataUsing(function ($record, array $data): array {
$data['custom_properties'] = array_replace($record->custom_properties, $data['custom_properties']);

return $data;
}),