form_field_definition.rst 5.1 KB

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