Closed wq9578 closed 5 months ago
It still creates problems, even after the change (displayed correctly, but stored as a plain string in the JSON column).
I now use the following workaround:
FilamentJsonColumn::make('json')->viewerOnly()->columnSpanFull()
->dehydrated(false), // prevent JSON corruption
See: https://filamentphp.com/docs/3.x/forms/advanced#preventing-a-field-from-being-dehydrated
Thanks for opening this issue. Encoding the state to JSON before dehydrating it looks like it's solving the problem:
$this->beforeStateDehydrated(function(FilamentJsonColumn $component, $state) {
$component->state(json_decode($state));
});
What do you think? If it doesn't cause any further bugs, I'll push the fix now.
EDIT: Had to solve additional issues due to the sequence of JSON parsing throughout the plugin, from PHP to Alpine.
Solved by fe0065d.
When saving a record, the JSON data is stored incorrectly. Instead of the array data an array containing the serialized JSON string (with key "0") is stored.
This is due to a bug in this line: https://github.com/valentin-morice/filament-json-column/blob/b25b146c090d73adeac78a583f47dc8eabcf2894/src/FilamentJsonColumn.php#L27
The context is:
Instead of
it must be
(removing the conversion via
(array)
).Since $state is a string (a result of json_encode()), a conversion with
(array)
produces an array with the string as the single element (with key "0").