the_form_view.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. The Form View
  2. =============
  3. You've seen the absolute top of the iceberg in
  4. :doc:`the previous chapter <creating_an_admin>`. But there is a lot more to
  5. discover! In the coming chapters, you'll create an Admin class for the more
  6. complex ``BlogPost`` model. Meanwhile, you'll learn how to make things a bit
  7. more pretty.
  8. Bootstrapping the Admin Class
  9. -----------------------------
  10. The basic class definition will look the same as the ``CategoryAdmin``:
  11. .. code-block:: php
  12. // src/AppBundle/Admin/BlogPostAdmin.php
  13. namespace AppBundle\Admin;
  14. use Sonata\AdminBundle\Admin\AbstractAdmin;
  15. use Sonata\AdminBundle\Datagrid\ListMapper;
  16. use Sonata\AdminBundle\Form\FormMapper;
  17. class BlogPostAdmin extends AbstractAdmin
  18. {
  19. protected function configureFormFields(FormMapper $formMapper)
  20. {
  21. // ... configure $formMapper
  22. }
  23. protected function configureListFields(ListMapper $listMapper)
  24. {
  25. // ... configure $listMapper
  26. }
  27. }
  28. The same applies to the service definition:
  29. .. code-block:: yaml
  30. # app/config/services.yml
  31. services:
  32. # ...
  33. admin.blog_post:
  34. class: AppBundle\Admin\BlogPostAdmin
  35. arguments: [~, AppBundle\Entity\BlogPost, ~]
  36. tags:
  37. - { name: sonata.admin, manager_type: orm, label: Blog post }
  38. Configuring the Form Mapper
  39. ---------------------------
  40. If you already know the `Symfony Form component`_, the ``FormMapper`` will look
  41. very similar.
  42. You use the ``add()`` method to add fields to the form. The first argument is
  43. the name of the property the field value maps to, the second argument is the
  44. type of the field (see the `field type reference`_) and the third argument are
  45. additional options to customize the form type. Only the first argument is
  46. required as the Form component has type guessers to guess the type.
  47. The ``BlogPost`` model has 4 properties: ``id``, ``title``, ``body``,
  48. ``category``. The ``id`` property's value is generated automatically by the
  49. database. This means the form view just needs 3 fields: title, body and
  50. category.
  51. The title and body fields are simple "text" and "textarea" fields, you can add
  52. them straight away:
  53. .. code-block:: php
  54. // src/AppBundle/Admin/BlogPostAdmin.php
  55. // ...
  56. protected function configureFormFields(FormMapper $formMapper)
  57. {
  58. $formMapper
  59. ->add('title', 'text')
  60. ->add('body', 'textarea')
  61. ;
  62. }
  63. However, the category field will reference another model. How can you solve that?
  64. Adding Fields that Reference Other Models
  65. -----------------------------------------
  66. You have a couple different choices on how to add fields that reference other
  67. models. The most basic choice is to use the `entity field type`_ provided by
  68. the DoctrineBundle. This will render a choice field with the available entities
  69. as choice.
  70. .. code-block:: php
  71. // src/AppBundle/Admin/BlogPostAdmin.php
  72. // ...
  73. protected function configureFormFields(FormMapper $formMapper)
  74. {
  75. $formMapper
  76. // ...
  77. ->add('category', 'entity', array(
  78. 'class' => 'AppBundle\Entity\Category',
  79. 'property' => 'name',
  80. ))
  81. ;
  82. }
  83. .. note::
  84. The `property`_ option is not supported by Symfony >= 2.7. You should use `choice_label`_ instead.
  85. As each blog post will only have one category, it renders as a select list:
  86. .. image:: ../images/getting_started_entity_type.png
  87. When an admin would like to create a new category, they need to go to the
  88. category admin page and create a new category.
  89. Using the Sonata Model Type
  90. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. To make life easier for admins, you can use the
  92. :ref:`sonata_type_model field type <field-types-model>`. This field type will
  93. also render as a choice field, but it includes a create button to open a
  94. dialog with the admin of the referenced model in it:
  95. .. code-block:: php
  96. // src/AppBundle/Admin/BlogPostAdmin.php
  97. // ...
  98. protected function configureFormFields(FormMapper $formMapper)
  99. {
  100. $formMapper
  101. // ...
  102. ->add('category', 'sonata_type_model', array(
  103. 'class' => 'AppBundle\Entity\Category',
  104. 'property' => 'name',
  105. ))
  106. ;
  107. }
  108. .. image:: ../images/getting_started_sonata_model_type.png
  109. Using Groups
  110. ------------
  111. Currently, everything is put into one block. Since the form only has three
  112. fields, it is still usable, but it can become quite a mess pretty quick. To
  113. solve this, the form mapper also supports grouping fields together.
  114. For instance, the title and body fields can belong to the Content group and the
  115. category field to a Meta data group. To do this, use the ``with()`` method:
  116. .. code-block:: php
  117. // src/AppBundle/Admin/BlogPostAdmin.php
  118. // ...
  119. protected function configureFormFields(FormMapper $formMapper)
  120. {
  121. $formMapper
  122. ->with('Content')
  123. ->add('title', 'text')
  124. ->add('body', 'textarea')
  125. ->end()
  126. ->with('Meta data')
  127. ->add('category', 'sonata_type_model', array(
  128. 'class' => 'AppBundle\Entity\Category',
  129. 'property' => 'name',
  130. ))
  131. ->end()
  132. ;
  133. }
  134. The first argument is the name/label of the group and the second argument is an
  135. array of options. For instance, you can pass HTML classes to the group in
  136. order to tweak the styling:
  137. .. code-block:: php
  138. // src/AppBundle/Admin/BlogPostAdmin.php
  139. // ...
  140. protected function configureFormFields(FormMapper $formMapper)
  141. {
  142. $formMapper
  143. ->with('Content', array('class' => 'col-md-9'))
  144. // ...
  145. ->end()
  146. ->with('Meta data', array('class' => 'col-md-3')
  147. // ...
  148. ->end()
  149. ;
  150. }
  151. This will now result in a much nicer edit page:
  152. .. image:: ../images/getting_started_post_edit_grid.png
  153. Using Tabs
  154. ~~~~~~~~~~
  155. If you get even more options, you can also use multiple tabs by using the
  156. ``tab()`` shortcut method:
  157. .. code-block:: php
  158. $formMapper
  159. ->tab('Post')
  160. ->with('Content', ...)
  161. // ...
  162. ->end()
  163. // ...
  164. ->end()
  165. ->tab('Publish Options')
  166. // ...
  167. ->end()
  168. ;
  169. Creating a Blog Post
  170. --------------------
  171. You've now finished your nice form view for the ``BlogPost`` model. Now it's
  172. time to test it out by creating a post.
  173. After pressing the "Create" button, you probably see a green message like:
  174. *Item "AppBundle\Entity\BlogPost:00000000192ba93c000000001b786396" has been
  175. successfully created.*
  176. While it's very friendly of the SonataAdminBundle to notify the admin of a
  177. successful creation, the classname and some sort of hash aren't really nice to
  178. read. This is the default string representation of an object in the
  179. SonataAdminBundle. You can change it by defining a ``toString()`` method in the
  180. Admin class. This receives the object to transform to a string as the first parameter:
  181. .. note::
  182. No underscore prefix! ``toString()`` is correct!
  183. .. code-block:: php
  184. // src/AppBundle/Admin/BlogPostAdmin.php
  185. // ...
  186. use AppBundle\Entity\BlogPost;
  187. class BlogPostAdmin extends AbstractAdmin
  188. {
  189. // ...
  190. public function toString($object)
  191. {
  192. return $object instanceof BlogPost
  193. ? $object->getTitle()
  194. : 'Blog Post'; // shown in the breadcrumb on the create view
  195. }
  196. }
  197. Round Up
  198. --------
  199. In this tutorial, you've made your first contact with the greatest feature of
  200. the SonataAdminBundle: Being able to customize literally everything. You've
  201. started by creating a simple form and ended up with a nice edit page for your
  202. admin.
  203. In the :doc:`next chapter <the_list_view>`, you're going to look at the list
  204. and datagrid actions.
  205. .. _`Symfony Form component`: http://symfony.com/doc/current/book/forms.html
  206. .. _`field type reference`: http://symfony.com/doc/current/reference/forms/types.html
  207. .. _`entity field type`: http://symfony.com/doc/current/reference/forms/types/entity.html
  208. .. _`choice_label`: http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
  209. .. _`property`: http://symfony.com/doc/2.6/reference/forms/types/entity.html#property