form_field_definition.rst 4.3 KB

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