chasegiunta / craft-jason

A field type for displaying & editing JSON data.
Other
15 stars 11 forks source link

Error with setFieldValue when Trying to Save Element #150

Open zadro opened 8 months ago

zadro commented 8 months ago

Trying to set a field in a plugin, using the following:

$entry->setFieldValue('json', Json::encode($variable));

JSON value is a string and in this format:

{
    "Some Title": {
        "question": "What is Foo?",
        "answer": "hello\r\n\r\nthis \r\n\r\nis a multi-line!"
    },
    "Another Title": {
        "question": "What is bar?",
        "answer": "This is another answer to the question."
    }
}

Error is below when trying to store an already formatted JSON string using:

"name": "Exception",
    "message": "json_decode(): Argument #1 ($json) must be of type string, array given",
    "code": 0,
    "error": "json_decode(): Argument #1 ($json) must be of type string, array given",
    "exception": "TypeError",
    "file": "/home/zadro/www/clientname/vendor/chasegiunta/craft-jason/src/fields/JasonField.php",
    "line": 127

https://github.com/chasegiunta/craft-jason/issues/108

I can confirm the fix @stevecomrie proposes #108 is a working solution.

However, the fix is required on both lines 127 and 425 in JasonField.php