简体   繁体   English

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

[英]Handle array of string in edit form in Sonata Admin Bundle

In one of my entities, I got an array attribute. 在我的一个实体中,我得到了一个数组属性。 I thought Sonata Admin Bundle could handle it but it seems that it requires some attention. 我认为Sonata Admin Bundle可以处理它,但它似乎需要一些关注。

I'm pretty sure SONATA_TYPE_COLLECTION field type could handle that but I didn't find any clue on how to configure the field in configureFormFields() 我很确定SONATA_TYPE_COLLECTION字段类型可以处理,但我没有找到任何关于如何在configureFormFields()configureFormFields()字段的线索

Do anyone know how to configure it ? 有谁知道如何配置它?

Thanks 谢谢

You can use the Sonata CollectionType class, which is capable of adding and removing elements from an array: 您可以使用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()
            ;
        }

I give you an example that I used: Entity: 我给你一个我用过的例子:实体:

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

use Sonata\\AdminBundle\\Form\\Type\\CollectionType; 使用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',
                ]
            )

form: 形成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM