form_help_message.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Admin
  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 Way To Define Help Messages
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. .. code-block:: php
  30. <?php
  31. // src/AppBundle/Admin/PostAdmin.php
  32. class PostAdmin extends Admin
  33. {
  34. protected function configureFormFields(FormMapper $formMapper)
  35. {
  36. $formMapper
  37. ->with('General')
  38. ->add('title')
  39. ->add('keywords')
  40. ->setHelps(array(
  41. 'title' => 'Set the title of a web page',
  42. 'keywords' => 'Set the keywords of a web page',
  43. ))
  44. ->end()
  45. ;
  46. }
  47. }
  48. Help messages in a sub-field
  49. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  50. .. code-block:: php
  51. <?php
  52. // src/AppBundle/Admin/PostAdmin.php
  53. class PostAdmin extends Admin
  54. {
  55. protected function configureFormFields(FormMapper $formMapper)
  56. {
  57. $formMapper
  58. ->add('enabled')
  59. ->add('settings', 'sonata_type_immutable_array', array(
  60. 'keys' => array(
  61. array('content', 'textarea', array(
  62. 'sonata_help' => 'Set the content'
  63. )),
  64. array('public', 'checkbox', array()),
  65. ))
  66. ;
  67. }
  68. }
  69. Advanced usage
  70. ^^^^^^^^^^^^^^
  71. Since help messages can contain HTML they can be used for more advanced solutions.
  72. See the cookbook entry :doc:`Showing image previews <../cookbook/recipe_image_previews>` for a detailed example of how to
  73. use help messages to display an image tag.
  74. Form Group Descriptions
  75. -----------------------
  76. 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.
  77. Example
  78. ^^^^^^^
  79. .. code-block:: php
  80. <?php
  81. // src/AppBundle/Admin/PostAdmin.php
  82. class PostAdmin extends Admin
  83. {
  84. protected function configureFormFields(FormMapper $formMapper)
  85. {
  86. $formMapper
  87. ->with('General', array(
  88. 'description' => 'This section contains general settings for the web page'
  89. ))
  90. ->add('title', null, array(
  91. 'help' => 'Set the title of a web page'
  92. ))
  93. ->add('keywords', null, array(
  94. 'help' => 'Set the keywords of a web page'
  95. ))
  96. ->end()
  97. ;
  98. }
  99. }