form_field_definition.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. Form field definition
  2. =====================
  3. Example
  4. -------
  5. .. code-block:: php
  6. <?php
  7. namespace Sonta\NewsBundle\Admin;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Sonata\AdminBundle\Admin\Admin;
  10. class PostAdmin extends Admin
  11. {
  12. protected $form = array(
  13. 'author' => array('edit' => 'list'),
  14. 'enabled',
  15. 'title',
  16. 'abstract' => array('form_field_options' => array('required' => false)),
  17. 'content',
  18. );
  19. public function configureFormFields(FormMapper $form)
  20. {
  21. // equivalent to :
  22. $formMapper
  23. ->add('author', array(), array('edit' => 'list'))
  24. ->add('enabled')
  25. ->add('title')
  26. ->add('abtract', array(), array('required' => false))
  27. ->add('content')
  28. // you can define help messages like this
  29. ->setHelps(array(
  30. 'title' => $this->trans('help_post_title')
  31. ));
  32. }
  33. }
  34. .. note::
  35. By default, the form framework always sets ``required=true`` for each
  36. field. This can be an issue for HTML5 browsers as they provide client-side
  37. validation.
  38. Types available
  39. ---------------
  40. - array
  41. - boolean
  42. - choice
  43. - datetime
  44. - decimal
  45. - integer
  46. - many_to_many
  47. - many_to_one
  48. - one_to_one
  49. - string
  50. - text
  51. - date
  52. If no type is set, the Admin class will use the one set in the doctrine mapping
  53. definition.
  54. Advanced Usage: File Management
  55. --------------------------------
  56. If you want to use custom types from the Form framework you must use the
  57. ``addType`` method. (The ``add`` method uses the information provided by the
  58. model definition).
  59. .. code-block:: php
  60. <?php
  61. namespace Sonta\NewsBundle\Admin;
  62. use Sonata\AdminBundle\Form\FormMapper;
  63. use Sonata\AdminBundle\Admin\Admin;
  64. class MediaAdmin extends Admin
  65. {
  66. public function configureFormFields(FormMapper $form)
  67. {
  68. $formMapper
  69. ->add('name', array('required' => false))
  70. ->add('enabled', array('required' => false))
  71. ->add('authorName', array('required' => false))
  72. ->add('cdnIsFlushable', array('required' => false))
  73. ->add('description', array('required' => false))
  74. ->add('copyright', array('required' => false))
  75. // add a custom type, using the native form factory
  76. ->addType('binaryContent', 'file', array('type' => false, 'required' => false));
  77. }
  78. }
  79. .. note::
  80. By setting ``type=false`` in the file definition, the Form framework will
  81. provide an instance of ``UploadedFile`` for the ``Media::setBinaryContent``
  82. method. Otherwise, the full path will be provided.
  83. Advanced Usage: Many-to-one
  84. ----------------------------
  85. If you have many ``Post``s linked to one ``User``, then the ``Post`` form should
  86. display a ``User`` field.
  87. The AdminBundle provides 3 edit options:
  88. - ``standard``: default value, the user list is set in a select widget
  89. - ``list``: the user list is set in a model where you can search and select a user
  90. In both case, you can create a new ``User`` by clicking on the "+" icon.
  91. The last option, is ``inline`` this option embed the ``User`` form into the ``Post`` Form. This option is
  92. great for one-to-one, or if your want to allow the user to edit the ``User`` information.
  93. .. code-block:: php
  94. <?php
  95. namespace Sonata\NewsBundle\Admin;
  96. class PostAdmin extends Admin
  97. {
  98. protected $form = array(
  99. 'author' => array('edit' => 'list'),
  100. );
  101. }
  102. Advanced Usage: One-to-many
  103. ----------------------------
  104. Let's say you have a ``Gallery`` that links to some ``Media``s with a join table
  105. ``galleryHasMedias``. You can easily add a new ``galleryHasMedias`` row by
  106. defining one of these options:
  107. - ``edit``: ``inline|standard``, the inline mode allows you to add new rows
  108. - ``inline``: ``table|standard``, the fields are displayed into table
  109. - ``sortable``: if the model has a position field, you can enable a drag and
  110. drop sortable effect by setting ``sortable=field_name``
  111. .. code-block:: php
  112. <?php
  113. namespace Sonata\MediaBundle\Admin;
  114. use Sonata\AdminBundle\Admin\Admin;
  115. class GalleryAdmin extends Admin
  116. {
  117. protected $form = array(
  118. 'name',
  119. 'galleryHasMedias' => array(
  120. 'edit' => 'inline',
  121. 'inline' => 'table',
  122. 'sortable' => 'position'
  123. ),
  124. );
  125. }