the_form_view.rst 7.9 KB

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