EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

`json` Doctrine Mapping type support

Open rernesto opened this issue 5 years ago • 9 comments

Short description of what this feature will allow to do: Edit/Display json doctrine mapped fields.

/**
     * @ORM\Column(type="json")
     */
    protected $jsonData = [];

Example of how to use this feature

Field::new('jsonData')
    ->setFormType(JsonType::class);

or

JsonField::new('jsonData');

Meanwhile I could make a custom field, but I don't wanna reinvent the wheel if EasyAdmin already supports this. I tried already using EasyAdmin Field with My own custom FormType... did not work, whatever I tried i got a pretty

The value of the "jsonData" field of the entity... can't be converted into a string, so it cannot be represented by a TextField.

rernesto avatar May 13 '21 10:05 rernesto

why not just create your own field and configurator?

pkly avatar May 18 '21 09:05 pkly

For now we won't add this field ... but you can try this instead:

CodeEditorField::new('jsonData')->setLanguage('js');

This field has other options to configure the tab size, etc. Hope it helps you!

javiereguiluz avatar May 18 '21 18:05 javiereguiluz

why not just create your own field and configurator?

That's what I am doing but was not working properly. I was pushed to move forward to fix more urgent stuff so I left it for a few days. I will try @javiereguiluz way around and then finish my own custom field until EasyAdmin add support for this. Thanks both for the answer.

rernesto avatar May 25 '21 14:05 rernesto

CodeEditorField::new('jsonData')->setLanguage('js');

This threw An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). I will play a little more with this approach.

rernesto avatar May 25 '21 14:05 rernesto

CodeEditorField::new('jsonData')->setLanguage('js');

This threw An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). I will play a little more with this approach.

Is there any solution ?

tourze avatar Aug 11 '21 02:08 tourze

For any people in the future that stumble upon this problem: In my case it worked with CodeEditorField and then ->setFormType(CustomJsonType) The CustomJsonType has a simple model or view transformer depending on your needs like

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    parent::buildForm($builder, $options);

    $builder->addModelTransformer(new CallbackTransformer(
        fn($object) => json_encode($object),
        fn($json) => json_decode($json)
    ));
}

Dominik-Czulak avatar Mar 16 '22 12:03 Dominik-Czulak

Hi @Dominik-Czulak , can you explain a little bit more your solution?

Thank you.

xergio avatar Mar 31 '22 19:03 xergio

@xergio Create a class like

namespace App\Form;

use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CodeEditorType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;

class JsonCodeEditorType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        parent::buildForm($builder, $options);

        $builder
            ->addModelTransformer(new CallbackTransformer(
                static fn ($object) => json_encode($object, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT),
                static fn ($json) => json_decode($json, true, 512, \JSON_THROW_ON_ERROR)
            ));
    }

    public function getParent(): string
    {
        return CodeEditorType::class;
    }
}

In the configureFields method of your CRUD Controller:

    yield CodeEditorField::new('result')
                ->setFormType(JsonCodeEditorType::class)
               

myfluxi avatar May 19 '22 20:05 myfluxi

Closing as fixed in #5759.

javiereguiluz avatar Jun 23 '23 18:06 javiereguiluz