form_help_message.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. Form Help Messages and Descriptions
  2. ===================================
  3. Help Messages
  4. -------------
  5. 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.
  6. Example
  7. ^^^^^^^
  8. .. code-block:: php
  9. <?php
  10. // src/AppBundle/Admin/PostAdmin.php
  11. class PostAdmin extends AbstractAdmin
  12. {
  13. protected function configureFormFields(FormMapper $formMapper)
  14. {
  15. $formMapper
  16. ->with('General')
  17. ->add('title', null, array(
  18. 'help' => 'Set the title of a web page'
  19. ))
  20. ->add('keywords', null, array(
  21. 'help' => 'Set the keywords of a web page'
  22. ))
  23. ->end()
  24. ;
  25. }
  26. }
  27. Alternative Ways To Define Help Messages
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. All at once
  30. .. code-block:: php
  31. <?php
  32. // src/AppBundle/Admin/PostAdmin.php
  33. class PostAdmin extends AbstractAdmin
  34. {
  35. protected function configureFormFields(FormMapper $formMapper)
  36. {
  37. $formMapper
  38. ->with('General')
  39. ->add('title')
  40. ->add('keywords')
  41. ->setHelps(array(
  42. 'title' => 'Set the title of a web page',
  43. 'keywords' => 'Set the keywords of a web page',
  44. ))
  45. ->end()
  46. ;
  47. }
  48. }
  49. or step by step.
  50. .. code-block:: php
  51. <?php
  52. // src/AppBundle/Admin/PostAdmin.php
  53. class PostAdmin extends AbstractAdmin
  54. {
  55. protected function configureFormFields(FormMapper $formMapper)
  56. {
  57. $formMapper
  58. ->with('General')
  59. ->add('title')
  60. ->add('keywords')
  61. ->setHelp('title', 'Set the title of a web page')
  62. ->setHelp('keywords', 'Set the keywords of a web page')
  63. ->end()
  64. ;
  65. }
  66. }
  67. This can be very useful if you want to apply general help messages via an ``AdminExtension``.
  68. This Extension for example adds a note field to some entities which use a custom trait.
  69. .. code-block:: php
  70. <?php
  71. namespace AppBundle\Admin\Extension;
  72. use Sonata\AdminBundle\Admin\AbstractAdminExtension;
  73. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  74. use Sonata\AdminBundle\Form\FormMapper;
  75. use Sonata\AdminBundle\Show\ShowMapper;
  76. class NoteAdminExtension extends AbstractAdminExtension
  77. {
  78. // add this field to the datagrid every time its available
  79. /**
  80. * @param DatagridMapper $datagridMapper
  81. */
  82. public function configureDatagridFilters(DatagridMapper $datagridMapper)
  83. {
  84. $datagridMapper
  85. ->add('note')
  86. ;
  87. }
  88. // here we don't add the field, because we would like to define
  89. // the place manually in the admin. But if the filed is available,
  90. // we want to add the following help message to the field.
  91. /**
  92. * @param FormMapper $formMapper
  93. */
  94. public function configureFormFields(FormMapper $formMapper)
  95. {
  96. $formMapper
  97. ->addHelp('note', 'Use this field for an internal note.')
  98. ;
  99. }
  100. // if the field exists, add it in a special tab on the show view.
  101. /**
  102. * @param ShowMapper $showMapper
  103. */
  104. public function configureShowFields(ShowMapper $showMapper)
  105. {
  106. $showMapper
  107. ->with('Internal')
  108. ->add('note')
  109. ->end()
  110. ;
  111. }
  112. }
  113. Help messages in a sub-field
  114. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  115. .. code-block:: php
  116. <?php
  117. // src/AppBundle/Admin/PostAdmin.php
  118. class PostAdmin extends AbstractAdmin
  119. {
  120. protected function configureFormFields(FormMapper $formMapper)
  121. {
  122. $formMapper
  123. ->add('enabled')
  124. ->add('settings', 'sonata_type_immutable_array', array(
  125. 'keys' => array(
  126. array('content', 'textarea', array(
  127. 'sonata_help' => 'Set the content'
  128. )),
  129. array('public', 'checkbox', array()),
  130. ))
  131. ;
  132. }
  133. }
  134. Advanced usage
  135. ^^^^^^^^^^^^^^
  136. Since help messages can contain HTML they can be used for more advanced solutions.
  137. See the cookbook entry :doc:`Showing image previews <../cookbook/recipe_image_previews>` for a detailed example of how to
  138. use help messages to display an image tag.
  139. Form Group Descriptions
  140. -----------------------
  141. 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.
  142. Example
  143. ^^^^^^^
  144. .. code-block:: php
  145. <?php
  146. // src/AppBundle/Admin/PostAdmin.php
  147. class PostAdmin extends AbstractAdmin
  148. {
  149. protected function configureFormFields(FormMapper $formMapper)
  150. {
  151. $formMapper
  152. ->with('General', array(
  153. 'description' => 'This section contains general settings for the web page'
  154. ))
  155. ->add('title', null, array(
  156. 'help' => 'Set the title of a web page'
  157. ))
  158. ->add('keywords', null, array(
  159. 'help' => 'Set the keywords of a web page'
  160. ))
  161. ->end()
  162. ;
  163. }
  164. }