Version

Documentation » Admin

17. Field Types »

« 15. Form Types

16. Form Help Messages and Descriptions

16.1. Help Messages

Help messages are short notes that are rendered together with form fields. They are generally used to show additional information so the user can complete the form element faster and more accurately. The text is not escaped, so HTML can be used.

16.1.1. Example

<?php
// src/AppBundle/Admin/PostAdmin.php

class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title', null, array(
                    'help' => 'Set the title of a web page'
                ))
                ->add('keywords', null, array(
                    'help' => 'Set the keywords of a web page'
                ))
            ->end()
        ;
    }
}

16.1.2. Alternative Ways To Define Help Messages

All at once

<?php
// src/AppBundle/Admin/PostAdmin.php

class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title')
                ->add('keywords')
                ->setHelps(array(
                    'title' => 'Set the title of a web page',
                    'keywords' => 'Set the keywords of a web page',
                ))
            ->end()
        ;
    }
}

or step by step.

<?php
// src/AppBundle/Admin/PostAdmin.php

class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title')
                ->add('keywords')
                ->setHelp('title', 'Set the title of a web page')
                ->setHelp('keywords', 'Set the keywords of a web page')
            ->end()
        ;
    }
}

This can be very useful if you want to apply general help messages via an AdminExtension. This Extension for example adds a note field to some entities which use a custom trait.

<?php

namespace AppBundle\Admin\Extension;

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

class NoteAdminExtension extends AbstractAdminExtension
{

    // add this field to the datagrid every time its available
    /**
     * @param DatagridMapper $datagridMapper
     */
    public function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('note')
        ;
    }

    // here we don't add the field, because we would like to define
    // the place manually in the admin. But if the filed is available,
    // we want to add the following help message to the field.
    /**
     * @param FormMapper $formMapper
     */
    public function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->addHelp('note', 'Use this field for an internal note.')
        ;
    }

    // if the field exists, add it in a special tab on the show view.
    /**
     * @param ShowMapper $showMapper
     */
    public function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->with('Internal')
                ->add('note')
            ->end()
        ;
    }
}

16.1.3. Help messages in a sub-field

<?php
// src/AppBundle/Admin/PostAdmin.php

class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('enabled')
            ->add('settings', 'sonata_type_immutable_array', array(
                'keys' => array(
                    array('content', 'textarea', array(
                        'sonata_help' => 'Set the content'
                    )),
                    array('public', 'checkbox', array()),
            ))
        ;
    }
}

16.1.4. Advanced usage

Since help messages can contain HTML they can be used for more advanced solutions. See the cookbook entry Showing image previews for a detailed example of how to use help messages to display an image tag.

16.2. Form Group Descriptions

A form group description is a block of text rendered below the group title. These can be used to describe a section of a form. The text is not escaped, so HTML can be used.

16.2.1. Example

<?php
// src/AppBundle/Admin/PostAdmin.php

class PostAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General', array(
                'description' => 'This section contains general settings for the web page'
            ))
                ->add('title', null, array(
                    'help' => 'Set the title of a web page'
                ))
                ->add('keywords', null, array(
                    'help' => 'Set the keywords of a web page'
                ))
            ->end()
        ;
    }
}

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