craft-jason icon indicating copy to clipboard operation
craft-jason copied to clipboard

Field not normalized on action request

Open jerome2710 opened this issue 4 years ago • 12 comments

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.

jerome2710 avatar Dec 03 '20 11:12 jerome2710

https://github.com/chasegiunta/craft-jason/issues/73 ..?

chasegiunta avatar Dec 03 '20 13:12 chasegiunta

#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?

jerome2710 avatar Dec 03 '20 13:12 jerome2710

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.

chasegiunta avatar Dec 03 '20 14:12 chasegiunta

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.

jerome2710 avatar Dec 03 '20 14:12 jerome2710

Any updates on this matter? This bug seem to be coming back every once in a while in one of our projects.

jerome2710 avatar Feb 22 '21 10:02 jerome2710

This keeps biting us too, any progress?

philipzaengle avatar Sep 02 '21 18:09 philipzaengle

@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 avatar Sep 03 '21 15:09 chasegiunta

@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.

jerome2710 avatar Sep 13 '21 07:09 jerome2710

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.

stevecomrie avatar Jan 30 '23 22:01 stevecomrie

Has the author abandoned this plugin? Would be nice to have an update on this or some communication at a minimum.

zadro avatar Mar 09 '24 17:03 zadro

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.

chasegiunta avatar Mar 09 '24 17:03 chasegiunta

Thanks, @chasegiunta -- appreciate your message!

zadro avatar Mar 09 '24 17:03 zadro