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. Adding a FormBuilderInterface

You can add Symfony FormBuilderInterface instances to the FormMapper. This allows you to re-use a model form type. When adding a field using a FormBuilderInterface, the type is guessed.

Given you have a PostType like this:

// src/Form/PostType.php

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\AbstractType;

class PostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('author', EntityType::class, [
                'class' => User::class
            ])
            ->add('title', TextType::class)
            ->add('body', TextareaType::class)
        ;
    }
}

you can reuse it like this:

// src/Admin/Post.php

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use App\Form\PostType;

final class Post extend AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $builder = $form->getFormBuilder()->getFormFactory()->createBuilder(PostType::class);

        $form
            ->with('Post')
                ->add($builder->get('title'))
                ->add($builder->get('body'))
            ->end()
            ->with('Author')
                ->add($builder->get('author'))
            ->end()
        ;
    }
}

12.4. Types options

12.4.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.4.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,
                ])
            ;
        }
    }