Error doing json_decode to a MediaCollection after form validation fail #3654
Unanswered
fabiomlferreira
asked this question in
Ideas
Replies: 1 comment
-
My use case is a bit specific, and most users likely wouldn't encounter this issue. However, there's a potential PHP error in the jsonSerialize method. It's intended to always return an array, but currently, in certain cases, it might return other data types, causing code to break. In my opinion, discussing this issue might not be necessary. Instead, we could simply make the jsonSerialize method protected to ensure it always returns an array. What do you think @freekmurze ? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a form with a field named "cover" and that form is for a Demo model that have a Media Collection we can access like this
$demo->getMedia('cover').
When I open if I do json_encode($demo->getMedia('cover')) it calls jsonSerialize() from Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection
This works but if I have some field missing and the cover input have any value the the json_encode($demo->getMedia('cover')) will crash, because the jsonSerialize() method is like this
In this case this method will return what is inside old('cover') in my case this is a string with the name of the file and a string is not an array and I get an error.
I this this code should be improved to prevent to return something that is not an array where this is my suggestion:
Beta Was this translation helpful? Give feedback.
All reactions