簡體   English   中英

在Sonata Admin Bundle中處理編輯形式的字符串數組

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

在我的一個實體中,我得到了一個數組屬性。 我認為Sonata Admin Bundle可以處理它,但它似乎需要一些關注。

我很確定SONATA_TYPE_COLLECTION字段類型可以處理,但我沒有找到任何關於如何在configureFormFields()configureFormFields()字段的線索

有誰知道如何配置它?

謝謝

您可以使用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()
            ;
        }

我給你一個我用過的例子:實體:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM