简体   繁体   中英

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.

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()

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:

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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