V
V
Vadim2018-01-16 06:58:57
symfony
Vadim, 2018-01-16 06:58:57

How to make symfony form json type?

The Doctrine entity has a features property of type json, which should store features in key-value format. The admin panel uses CollectionType for the custom type KeyValueType (two fields: key and value). Everything works, but it turns out redundant:

[
{"key": "Материал", "value": "металлический каркас"}, {"key": "Цвет подставки", "value": "черный"}
]

I would like this:
[
{"Материал": "металлический каркас"}, {"Цвет подставки": "черный"}
]

Or in general, only an array, without objects inside, but as I understand it, json_array in Doctrine is now deprecated.
What can you think of so that the editing forms are displayed in two fields: the name of the characteristic and its value. data transformer? It doesn't seem like it.
I tried using FormEvents::PRE_SUBMIT, it also did not work out the keys to change the collection. Perhaps I did it wrong.
As a last resort it is possible through Event[Subscriber|Listener] Doctrines, but it seems that it is crooked crutches.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-01-16
@Minifets

Here is the documentation on how to create a custom type.
For me, this is the most correct approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question