`json` Doctrine Mapping type support
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.
why not just create your own field and configurator?
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!
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.
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.
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 ?
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)
));
}
Hi @Dominik-Czulak , can you explain a little bit more your solution?
Thank you.
@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)
Closing as fixed in #5759.