【问题标题】:symfony form Collection type from array来自数组的symfony表单集合类型
【发布时间】:2020-11-19 11:55:10
【问题描述】:

我想生成一个表单,我可以在其中添加以键/值作为 json 存储在数据库中的 descriptionAttributes。我希望用户可以添加/或删除描述。 这是我的表单类型:

class HardwareKindType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('type', TextType::class, array(
                'label' => 'Art:',
                'required' => true,))
            ->add('descriptionTemplate', CollectionType::class,array(
                'entry_type' => FormType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'constraints' => [
                    new Type('array')
                ]
            ));


    }/**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Hardware\HardwareKind'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_hardwarekind';
    }
}

这是我的实体

class HardwareKind
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=32)
     */
    private $type;

    /**
     * @var array
     *
     * @ORM\Column(name="description_template", type="json")
     */
    private $descriptionTemplate;


    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set type.
     *
     * @param string $type
     *
     * @return HardwareKind
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type.
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set descriptionTemplate.
     *
     * @param array $descriptionTemplate
     *
     * @return HardwareKind
     */
    public function setDescriptionTemplate($descriptionTemplate)
    {
        $this->descriptionTemplate = $descriptionTemplate;

        return $this;
    }

    /**
     * Get descriptionTemplate.
     *
     * @return array
     */
    public function getDescriptionTemplate()
    {
        return $this->descriptionTemplate;
    }
}

但这只显示一个字段Art:,没有描述模板。当我将entry_type 更改为TextType::class 时,它只显示一个文本字段,但没有其他内容,也没有添加另一个字段。

【问题讨论】:

    标签: forms symfony doctrine


    【解决方案1】:

    集合类型字段需要另一种形式,详情请查看此答案https://stackoverflow.com/a/56124819/2463644

    【讨论】:

    • 所以没有办法使用'allow_add' => true'allow_delete' => true 插入新的和删除不必要的数组项?
    • 这就是 'allow_add' 和 'allow_delete' 的用途,但要让它发挥作用还有很多工作要做。您确实需要一个描述模板表单类型,您需要使用 js 编码的按钮,这些按钮将从您的主表单中添加/删除这些子表单。检查这两个链接 symfony.com/doc/current/form/form_collections.htmlsymfony.com/doc/current/reference/forms/types/… 以获取有关 CollectionType 和嵌入表单的详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多