12. Form Types

12.2. FieldDescription options

The fourth parameter to FormMapper::add() allows you to pass in FieldDescription options as an array. The most useful of these is admin_code, which allows you to specify which admin to use for managing this relationship. It is most useful for inline editing in conjunction with the Sonata\AdminBundle\Form\Type\AdminType form type.

The value used should be the admin service name, not the class name. If you do not specify an admin_code in this way, the default admin class for the field’s model type will be used.

For example, to specify the use of the admin class which is registered as sonata.admin.imageSpecial for managing the image1 field from our PageAdmin example above:

// src/Admin/PageAdmin.php

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\Type\AdminType;

final class PageAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form
            ->add('image1', AdminType::class, [], [
                'admin_code' => 'sonata.admin.imageSpecial'
            ])
        ;
    }
}

Other specific field configuration options are detailed in the related abstraction layer documentation.

12.3. Types options

12.3.1. General

You can use any of the Symfony form options to customize the form fields. For instance

  • You can set the label option to false if you don’t want to show it:

    // src/Admin/PageAdmin.php
    
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Admin\AbstractAdmin;
    
    final class PageAdmin extends AbstractAdmin
    {
        protected function configureFormFields(FormMapper $form): void
        {
            $form
                ->add('status', null, [
                    'label' => false
                ])
            ;
        }
    }
    
  • You can use the help option to add messages that are rendered together with form fields:

    // src/Admin/PostAdmin.php
    
    final class PostAdmin extends AbstractAdmin
    {
        protected function configureFormFields(FormMapper $form): void
        {
            $form
                ->with('General')
                    ->add('title', null, [
                        'help' => 'Set the title of a web page'
                    ])
                    ->add('keywords', null, [
                        'help' => 'Set the keywords of a web page'
                    ])
                ->end()
            ;
        }
    }
    
Example of the two form fields with help messages.

12.3.2. Symfony\Component\Form\Extension\Core\Type\ChoiceType

  • sortable: This option can be added for multiple choice widget to activate select2 sortable:

    // src/Admin/PageAdmin.php
    
    use Sonata\AdminBundle\Form\FormMapper;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    use Sonata\AdminBundle\Admin\AbstractAdmin;
    
    final class PageAdmin extends AbstractAdmin
    {
        protected function configureFormFields(FormMapper $form): void
        {
            $form
                ->add('multiChoices', ChoiceType::class, [
                    'multiple' => true,
                    'sortable' => true,
                ])
            ;
        }
    }