form_field_definition.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 $formMapper)
  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. - ``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.
  91. With the ``standard`` and ``list`` options, you can create a new ``User`` by clicking on the "+" icon.
  92. .. code-block:: php
  93. <?php
  94. namespace Sonata\NewsBundle\Admin;
  95. class PostAdmin extends Admin
  96. {
  97. protected $form = array(
  98. 'author' => array('edit' => 'list'),
  99. );
  100. }
  101. Advanced Usage: One-to-many
  102. ----------------------------
  103. Let's say you have a ``Gallery`` that links to some ``Media``s with a join table
  104. ``galleryHasMedias``. You can easily add a new ``galleryHasMedias`` row by
  105. defining one of these options:
  106. - ``edit``: ``inline|standard``, the inline mode allows you to add new rows
  107. - ``inline``: ``table|standard``, the fields are displayed into table
  108. - ``sortable``: if the model has a position field, you can enable a drag and
  109. drop sortable effect by setting ``sortable=field_name``
  110. .. code-block:: php
  111. <?php
  112. namespace Sonata\MediaBundle\Admin;
  113. use Sonata\AdminBundle\Admin\Admin;
  114. class GalleryAdmin extends Admin
  115. {
  116. protected $form = array(
  117. 'name',
  118. 'galleryHasMedias' => array(
  119. 'edit' => 'inline',
  120. 'inline' => 'table',
  121. 'sortable' => 'position'
  122. ),
  123. );
  124. }