3

Symfony 5.3 + EasyAdmin 3.3.2 + Doctrine

New approach of asking SO questions: short and simple (I got banned from asking questions before and I have no idea what I did wrong)

What I have: I have an entity 'Device' which has a field 'attributeList'.

Example content of attributeList:

[ {"key":"color", "value":"blue", "type":"text"},  {"key":"amount", "value":"4", "type":"number"},  {"key":"consumable", "value":"true", "type":"boolean"} ]

What I want: Each js object in that array should be rendered in one form row (in the edit form in this case). 'color' would be the label text, 'blue' would be the input value and 'text' would be the type of the input HTML element of the form widget.

I need to be able to edit,delete and add new attributes in the (edit) form. I think its easier to maintain the code and make it work to reduce the amount of twig adjustments as much as possible and use the onboard tools/services of symfony/easyadmin instead.

Current approach: Thats why I tried it recently via data mappers. From the description of what it can do it sounds like exactly what I need. Sadly with the setData() function it only changes the input value. The label is for all attributes 'value' and the field-type is always text.

DeviceCrudController.php

$attributeListForm = CollectionField::new('attributeList'," ")->setEntryType(JsonAttributeType::class);

JsonAttributeType.php

class JsonAttributeType extends AbstractType implements DataMapperInterface
{
    public function mapDataToForms($viewData, iterable $forms)
    {
        $forms = iterator_to_array($forms);
        $forms['value']->setData($viewData["key"]);  // if I could set the type of the form and the label text here, it would help me a lot
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('value', TextType::class)->setDataMapper($this);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'empty_data' => null,
        ));
    }
}
Quatsch
  • 361
  • 1
  • 8
  • 29
  • Did you mean to create a form element for every object thats in the array? If thats the case then a simple javascript function to loop through the array that appends dynamic elements to the DOM can be made – ahmedazhar05 Jul 04 '21 at 19:49
  • 1
    Yeah, I tried that before, but then I would need to implement all the collection-actions myself as well. Like add, delete, edit. I was hoping there is a way to do it without manual javascript/twig code, so it does not get too hard to maintain etc. But maybe that is just not doable. If I append the attributes via javascript and I change some content. How would you tell symfony to read the attribute DOMS content and convert it back to an array of JS objects? – Quatsch Jul 05 '21 at 09:57
  • Oh okay... I don't know symphony, so yeah... good luck bud – ahmedazhar05 Jul 05 '21 at 14:06

0 Answers0