Version

Documentation » MongoDB Admin

7. Form types and data transformers »

« 5. Filter Field Definition

6. Form field definition

6.1. Example

namespace Sonata\NewsBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Validator\ErrorElement;

final class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('author', 'sonata_type_model', [], ['edit' => 'list'])
            ->add('enabled')
            ->add('title')
            ->add('abstract', null, ['required' => false])
            ->add('content')

            // you can define help messages like this
            ->setHelps([
               'title' => $this->trans('help_post_title')
            ]);
    }

    public function validate(ErrorElement $errorElement, $object)
    {
        // conditional validation, see the related section for more information
        if ($object->getEnabled()) {
            // abstract cannot be empty when the post is enabled
            $errorElement
                ->with('abstract')
                    ->assertNotBlank()
                    ->assertNotNull()
                ->end()
            ;
        }
    }
}

Note

By default, the form framework always sets required=true for each field. This can be an issue for HTML5 browsers as they provide client-side validation.

6.2. Types available

  • array
  • checkbox
  • choice
  • datetime
  • decimal
  • integer
  • text
  • date
  • time
  • datetime

If no type is set, the Admin class will use the one set in the doctrine mapping definition.

6.3. Short Object Placeholder

When using Many-to-One or One-to-One relations with Sonata Type fields, a short object description is used to represent the target object. If no object is selected, a ‘No selection’ placeholder will be used. If you want to customize this placeholder, you can use the corresponding option in the form field definition:

namespace Sonata\NewsBundle\Admin;

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

final class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('enabled', null, ['required' => false])
                ->add('author', 'sonata_type_model_list', [], [
                    'placeholder' => 'No author selected',
                ]);
    }
}

This placeholder is translated using the SonataAdminBundle catalogue.

6.4. Advanced Usage: File Management

If you want to use custom types from the Form framework you must use the addType method. (The add method uses the information provided by the model definition):

  namespace Sonata\MediaBundle\Admin;

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

  final class MediaAdmin extends AbstractAdmin
  {
      protected function configureFormFields(FormMapper $formMapper)
      {
          $formMapper
              ->add('name', null, ['required' => false])
              ->add('enabled', null, ['required' => false])
              ->add('authorName', null, ['required' => false])
              ->add('cdnIsFlushable', null, ['required' => false])
              ->add('description', null, ['required' => false])
              ->add('copyright', null, ['required' => false])
              ->add('binaryContent', 'file', ['required' => false]);
      }
}

Note

By setting type=false in the file definition, the Form framework will provide an instance of UploadedFile for the Media::setBinaryContent method. Otherwise, the full path will be provided.

6.5. Advanced Usage: Many-to-one

If you have many Post``s linked to one ``User, then the Post form should display a User field.

The AdminBundle provides 3 edit options:

  • standard: default value, the User list is set in a select widget
  • list: the User list is set in a model where you can search and select a user
  • inline: embed the User form into the Post form, great for one-to-one, or if your want to allow the user to edit the User information.

With the standard and list options, you can create a new User by clicking on the “+” icon:

namespace Sonata\NewsBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;

final class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('enabled', null, ['required' => false])
                ->add('author', 'sonata_type_model_list', [

                    // Specify a custom label
                    'btn_add' => 'Add author',

                    // which will be translated
                    'btn_list' => 'button.list',

                    // or hide the button
                    'btn_delete' => false,

                    //Custom translation domain for buttons
                    'btn_catalogue' => 'SonataNewsBundle',
                ], ['edit' => 'list'])
                ->add('title')
                ->add('abstract')
                ->add('content')
            ->end()
            ->with('Tags')
                ->add('tags', 'sonata_type_model', ['expanded' => true])
            ->end()
            ->with('Options', ['collapsed' => true])
                ->add('commentsCloseAt')
                ->add('commentsEnabled', null, ['required' => false])
                ->add('commentsDefaultStatus', 'choice', ['choices' => Comment::getStatusList()])
            ->end()
        ;
    }
}

6.6. Advanced Usage: One-to-many

Let’s say you have a Gallery that links to some Media``s with a join table ``galleryHasMedias. You can easily add a new galleryHasMedias row by defining one of these options:

  • edit: inline|standard, the inline mode allows you to add new rows
  • inline: table|standard, the fields are displayed into table
  • sortable: if the model has a position field, you can enable a drag and drop sortable effect by setting sortable=field_name
namespace Sonata\MediaBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;

final class GalleryAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('code')
            ->add('enabled')
            ->add('name')
            ->add('defaultFormat')
            ->add('galleryHasMedias', 'sonata_type_collection', [], [
                'edit' => 'inline',
                'inline' => 'table',
                'sortable'  => 'position',
            ]);
    }
}

Found a typo? Something is wrong in this documentation? Just fork and edit it!