form_field_definition.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Form field definition
  2. =====================
  3. Example
  4. -------
  5. .. code-block:: php
  6. <?php
  7. namespace Sonta\NewsBundle\Admin;
  8. use Sonata\AdminBundle\Admin\Admin;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  11. use Sonata\AdminBundle\Datagrid\ListMapper;
  12. use Sonata\AdminBundle\Show\ShowMapper;
  13. use Sonata\AdminBundle\Validator\ErrorElement;
  14. class PostAdmin extends Admin
  15. {
  16. protected function configureFormFields(FormMapper $formMapper)
  17. {
  18. $formMapper
  19. ->add('author', 'sonata_type_model', array(), array('edit' => 'list'))
  20. ->add('enabled')
  21. ->add('title')
  22. ->add('abtract', null, array('required' => false))
  23. ->add('content')
  24. // you can define help messages like this
  25. ->setHelps(array(
  26. 'title' => $this->trans('help_post_title')
  27. ));
  28. }
  29. public function validate(ErrorElement $errorElement, $object)
  30. {
  31. // conditional validation, see the related section for more information
  32. if ($object->getEnabled()) {
  33. // abstract cannot be empty when the post is enabled
  34. $errorElement
  35. ->with('abtract')
  36. ->assertNotBlank()
  37. ->assertNotNull()
  38. ->end()
  39. ;
  40. }
  41. }
  42. }
  43. .. note::
  44. By default, the form framework always sets ``required=true`` for each
  45. field. This can be an issue for HTML5 browsers as they provide client-side
  46. validation.
  47. Types available
  48. ---------------
  49. - array
  50. - checkbox
  51. - choice
  52. - datetime
  53. - decimal
  54. - integer
  55. - text
  56. - date
  57. - time
  58. - datetime
  59. If no type is set, the Admin class will use the one set in the doctrine mapping
  60. definition.
  61. Advanced Usage: File Management
  62. -------------------------------
  63. If you want to use custom types from the Form framework you must use the
  64. ``addType`` method. (The ``add`` method uses the information provided by the
  65. model definition).
  66. .. code-block:: php
  67. <?php
  68. namespace Sonata\MediaBundle\Admin;
  69. use Sonata\AdminBundle\Admin\Admin;
  70. use Sonata\AdminBundle\Form\FormMapper;
  71. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  72. use Sonata\AdminBundle\Datagrid\ListMapper;
  73. use Sonata\AdminBundle\Show\ShowMapper;
  74. class MediaAdmin extends Admin
  75. {
  76. protected function configureFormFields(FormMapper $form)
  77. {
  78. $formMapper
  79. ->add('name', null, array('required' => false))
  80. ->add('enabled', null, array('required' => false))
  81. ->add('authorName', null, array('required' => false))
  82. ->add('cdnIsFlushable', null, array('required' => false))
  83. ->add('description', null, array('required' => false))
  84. ->add('copyright', null, array('required' => false))
  85. ->add('binaryContent', 'file', array('required' => false));
  86. }
  87. }
  88. .. note::
  89. By setting ``type=false`` in the file definition, the Form framework will
  90. provide an instance of ``UploadedFile`` for the ``Media::setBinaryContent``
  91. method. Otherwise, the full path will be provided.
  92. Advanced Usage: Many-to-one
  93. ---------------------------
  94. If you have many ``Post``s linked to one ``User``, then the ``Post`` form should
  95. display a ``User`` field.
  96. The AdminBundle provides 3 edit options:
  97. - ``standard``: default value, the ``User`` list is set in a select widget
  98. - ``list``: the ``User`` list is set in a model where you can search and select a user
  99. - ``inline``: embed the ``User`` form into the ``Post`` form, great for one-to-one, or if your want to allow the user to edit the ``User`` information.
  100. With the ``standard`` and ``list`` options, you can create a new ``User`` by clicking on the "+" icon.
  101. .. code-block:: php
  102. <?php
  103. namespace Sonata\NewsBundle\Admin;
  104. use Sonata\AdminBundle\Admin\Admin;
  105. use Sonata\AdminBundle\Form\FormMapper;
  106. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  107. use Sonata\AdminBundle\Datagrid\ListMapper;
  108. use Sonata\AdminBundle\Show\ShowMapper;
  109. class PostAdmin extends Admin
  110. {
  111. protected function configureFormFields(FormMapper $formMapper)
  112. {
  113. $formMapper
  114. ->with('General')
  115. ->add('enabled', null, array('required' => false))
  116. ->add('author', 'sonata_type_model', array(), array('edit' => 'list'))
  117. ->add('title')
  118. ->add('abstract')
  119. ->add('content')
  120. ->end()
  121. ->with('Tags')
  122. ->add('tags', 'sonata_type_model', array('expanded' => true))
  123. ->end()
  124. ->with('Options', array('collapsed' => true))
  125. ->add('commentsCloseAt')
  126. ->add('commentsEnabled', null, array('required' => false))
  127. ->add('commentsDefaultStatus', 'choice', array('choices' => Comment::getStatusList()))
  128. ->end()
  129. ;
  130. }
  131. }
  132. Advanced Usage: One-to-many
  133. ---------------------------
  134. Let's say you have a ``Gallery`` that links to some ``Media``s with a join table
  135. ``galleryHasMedias``. You can easily add a new ``galleryHasMedias`` row by
  136. defining one of these options:
  137. - ``edit``: ``inline|standard``, the inline mode allows you to add new rows
  138. - ``inline``: ``table|standard``, the fields are displayed into table
  139. - ``sortable``: if the model has a position field, you can enable a drag and
  140. drop sortable effect by setting ``sortable=field_name``
  141. .. code-block:: php
  142. <?php
  143. namespace Sonata\MediaBundle\Admin;
  144. use Sonata\AdminBundle\Admin\Admin;
  145. use Sonata\AdminBundle\Form\FormMapper;
  146. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  147. use Sonata\AdminBundle\Datagrid\ListMapper;
  148. class GalleryAdmin extends Admin
  149. {
  150. protected function configureFormFields(FormMapper $formMapper)
  151. {
  152. $formMapper
  153. ->add('code')
  154. ->add('enabled')
  155. ->add('name')
  156. ->add('defaultFormat')
  157. ->add('galleryHasMedias', 'sonata_type_collection', array(), array(
  158. 'edit' => 'inline',
  159. 'inline' => 'table',
  160. 'sortable' => 'position'
  161. ))
  162. ;
  163. }
  164. }