How to validate doctrine type "json" with symfony json constraint in a form correctly?

kurito

So, I'd like to enter some json into a form for it to be validated by symfonys json constraint:

/**
 * @Assert\Json(
 *     message = "variantJson field: invalid Json."
 * )
 * @ORM\Column(type="json", nullable=true)
 */
private $variantJson = [];

The form looks kinda like this:

$builder
        ...
        ->add('variantJson', null, ['attr' => $style])
        ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            ...
            }
        })
    ;

    $builder->get('variantJson')
        ->addModelTransformer(new CallbackTransformer(
            function ($jsonToString) {
                // transform the array to a string
                return json_encode($jsonToString);
            },
            function ($stringToJson) {
                // transform the string back to an array
                dump(json_decode($stringToJson, true));
                dump(json_last_error());
                  //1
                  return $stringToJson;
                  //2
                  return json_decode($stringToJson, true);
            }
        ))
    ;

The main problem is, that when I try to only return the json string in the ModelTransformer, I get this exception:

Expected argument of type "array or null", "string" given at property path "variantJson".

At the "PropertyAccessor"

And when I want to return as an array, I do the json_decode, and get a different error:

Expected argument of type "string", "array" given

At the "JsonValidator".

My suspection is, both PropertyAccessor and JsonValidator are in serial, and both need different types. I must be missing something. Any ideas? Thanks in advance!

Jeroen

The Doctrine type JSON does indeed expect an array: https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/types.html#json

This is useful if you have the need for an Array in your code and want to save that data in the database. The data gets converted to a JSON string before saving and back to an Array when retrieved.

If you don't need the array in your PHP code and your end-user is submitting JSON somehow that needs to be validated. You should probably just save it as a (LONG)TEXT.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to properly validate JSON request in symfony 3.4

How do I add compound index in Symfony 3 using Doctrine on column of type "json_array"?

How do I validate JSON data in a form

Symfony2 - How to validate autocomplete entity form type?

How to correctly validate a modal form

jsonschema does not validate json correctly

How to validate some custom constraint in symfony 5

Symfony/Doctrine Postgresql JSON to TEXT

Serializing form to json correctly

Symfony2 use doctrine in form type

Use a custom constraint/validator in symfony form type

API Json in symfony form

How to validate login form correctly with MVVM?

Symfony Doctrine - how to generate optgroup select form

Symfony 4: How to remove doctrine integrity constraint violation?

How to Validate JSON with Jackson JSON

Validate JSON Implicit as List or Type

Symfony - Doctrine createQueryBuilder from json_array

How to validate JSON object?

How to validate a JSON with jq?

How to format and validate JSON in anonymous type using C# properly?

SYMFONY custom CONSTRAINT -> Pass variable to a custom CONSTRAINT / How CONSTRAINT binded to a form field can OVERRIDE CONSTRAINT in ANNOTATION

Symfony form and loop with doctrine

Symfony: Form issue using Return type hinting in Doctrine Entity methods

Symfony Form Type constraint of string validates Integer value

Serialize Symfony form data to JSON

Send Nested Json to a Symfony Form

Symfony 4: JSON to Form Collection

How to correctly validate and submit if form is valid with ant design form with typescript