在Sonata Admin Bundle中处理编辑形式的字符串数组

问题描述 投票:4回答:2

在我的一个实体中,我得到了一个数组属性。我认为Sonata Admin Bundle可以处理它,但它似乎需要一些关注。

我很确定SONATA_TYPE_COLLECTION字段类型可以处理,但我没有找到任何关于如何在configureFormFields()中配置字段的线索

有谁知道如何配置它?

谢谢

symfony sonata-admin
2个回答
0
投票

您可以使用Sonata CollectionType类,它可以添加和删除数组中的元素:

use Sonata\AdminBundle\Form\Type\CollectionType;
...
protected function configureFormFields(FormMapper $formMapper)
        {
            $formMapper
                ->with('group')
                ->add('roles', CollectionType::class, array(
                    'allow_add' => true,
                    'allow_delete' => true,
                   ))
                ->end()
            ;
        }

0
投票

我给你一个我用过的例子:实体:

 /**
 * @ORM\Column(type="array", nullable=true)
 */
private $tablaXY = [];

使用Sonata \ AdminBundle \ Form \ Type \ CollectionType;

->add('tablaXY',CollectionType::class, [
                    'required' => false,
                    'by_reference' => false, // Use this because of reasons
                    'allow_add' => true, // True if you want allow adding new entries to the collection
                    'allow_delete' => true, // True if you want to allow deleting entries
                    'prototype' => true, // True if you want to use a custom form type
                    'entry_type' => TablaType::class, // Form type for the Entity that is being attached to the object
                ],
                [
                    'edit' => 'inline',
                    'inline' => 'table',
                    'sortable' => 'position',
                ]
            )

形成:

class TablaType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('ejeX',  TextType::class,['label' => 'Eje X (texto)',
            'required' => true,'attr' => array('class' => 'form-control'),])
            ->add('ejeY', NumberType::class,['label' => 'Eje Y (Número)',
            'required' => true])
        ;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.