首页 > 解决方案 > 如何以 symfony 形式显示一组 js 对象?

问题描述

Symfony 5.3 + EasyAdmin 3.3.2 + Doctrine

问 SO 问题的新方法:简短而简单(我以前被禁止提问,我不知道我做错了什么)

我所拥有的: 我有一个实体“设备”,它有一个字段“属性列表”。

属性列表的示例内容:

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

我想要什么: 该数组中的每个 js 对象都应该呈现在一个表单行中(在本例中为编辑表单)。“color”是标签文本,“blue”是输入值,“text”是表单小部件的输入 HTML 元素的类型。

我需要能够在(编辑)表单中编辑、删除和添加新属性。我认为更容易维护代码并使其尽可能减少树枝调整的数量,并改用 symfony/easyadmin 的板载工具/服务。

当前方法: 这就是我最近通过数据映射器尝试它的原因。从它可以做什么的描述听起来正是我需要的。可悲的是,使用 setData() 函数它只会更改输入值。标签适用于所有属性“值”,字段类型始终为文本。

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,
        ));
    }
}

标签: symfonydoctrinesymfony5easyadmineasyadmin3

解决方案


推荐阅读