'Array to string conversion in EasyAdmin 3 (Symfony 5)

I'm trying to display a json array on the EasyAdmin detail page. I read here Is there a way to represent a JSON field in EasyAdmin 3? that you can use ArrayField in EasyAdmin 3 to display a json array, which would make sense to me as that is the only field in EasyAdmin that could display it so I added it like this:

public function configureFields(string $pageName): iterable
{
    return [
        ArrayField::new('status', $this->translator->trans('form.label.status'))->onlyOnDetail(),
    ];
}

But it gives me this error: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). Do I need to add something else to it or does it not work at all?



Solution 1:[1]

change "status" to a multiplicity "statuses" Worked for me with changing "print" to "prints"

Solution 2:[2]

I found a workaround that resolved the issue in my situation where I wanted just to show JSON on details page

So in your entity add a get function as an unmapped field as indicated in the official documentaiton https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields

for example

public function getJsonField() {
  return json_encode($this->yourEntityJsonAttribute);
}

then in configureFields function in your CrudController add your field like this

TextAreaField::new('jsonField')->onlyOnDetail()

You can also read the json attribute and generate an HTML string in the getJsonField function then in configure field just add renderAsHtml in th field like this

TextAreaField::new('jsonField')->onlyOnDetail()->renderAsHtml()

Wish this suits your case too, Good luck

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henry Robben
Solution 2 Ahmed Ghiloubi