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

时间:2014-09-13 04:03:59

标签: symfony sonata-admin

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

我非常确定SONATA_TYPE_COLLECTION字段类型可以处理,但我没有找到关于如何在configureFormFields()

中配置字段的任何线索

有谁知道如何配置它?

由于

2 个答案:

答案 0 :(得分: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()
            ;
        }

答案 1 :(得分: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])
        ;
    }
}