craft-jason
                                
                                 craft-jason copied to clipboard
                                
                                    craft-jason copied to clipboard
                            
                            
                            
                        Field not normalized on action request
Hi!
May I ask why the field content is not normalized when the request is an action request (https://github.com/chasegiunta/craft-jason/blob/master/src/fields/JasonField.php#L126)?
We are displaying Jason contents on a page which also includes a MailChimp subscribe form. After posting the subscribe form and returning to the page, the Jason content is not decoded and is causing errors now.
https://github.com/chasegiunta/craft-jason/issues/73 ..?
#73 seems to be the cause of this bug indeed. Turns out !Craft::$app->request->getIsActionRequest() is not the best way to go, as it breaks other functionality. Could you fix this with an alternative?
I'd have to look into figuring out test cases for both instances.
Open to PR's if you have the environments set up (only asking since you were on both issues).
I agree, looks like #73 was the wrong bandaid.
Thank you. I'm not really familiar with Crafts normalizeValue() and need to look into it as well, but I'm now temporarily overcoming this in the template by checking if the value is_array() or not and then performing a json_decode() if needed.
Any updates on this matter? This bug seem to be coming back every once in a while in one of our projects.
This keeps biting us too, any progress?
@philipzaengle I really need to know exactly what cases folks are running into this error so I can tailor the response for each. @jerome2710 was your workaround working (at least in your one project) for all known cases?
@chasegiunta @philipzaengle I have not narrowed it down to specific cases / errors, but instead just wrote a helper-function to retrieve the JSON-data:
public static function getData(Element $element): array
{
    $data = $element->json ?? [];
    if (!empty($data) && is_string($data)) {
        $data = json_decode($data, true) ?? [];
    }
    return $data;
}
So looking up the $element->json does not consistently consists of decoded JSON. If it doesn't and consists of a string, we manually decode it.
So .. I've been running into this issue and another issue that I think is related to #73 .. and I think the fix is relatively simple.
On this line, changing:
return json_decode($value, true);
to:
return is_array( $value ) ? $value : json_decode($value, true);
Should help with not trying to normalize input that's already formatted properly. Which should make it easier to save data to as Jason field programmatically without experiencing errors.
Has the author abandoned this plugin? Would be nice to have an update on this or some communication at a minimum.
I just unfortunately don't use Craft for my day job, and my one volunteer project with Craft does not use this plugin. I just haven't been able to dedicate the time to test all the use-cases, because apparently there are several scenarios where one change could break another, so I'm a bit fearful to make any changes.
I might hand this plugin over to someone else, but in the mean-time, forking and making the changes necessary for your use-case might be the way to go.
Thanks, @chasegiunta -- appreciate your message!