FormEvents.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Event\PostSetDataEvent;
  12. use Symfony\Component\Form\Event\PostSubmitEvent;
  13. use Symfony\Component\Form\Event\PreSetDataEvent;
  14. use Symfony\Component\Form\Event\PreSubmitEvent;
  15. use Symfony\Component\Form\Event\SubmitEvent;
  16. /**
  17. * To learn more about how form events work check the documentation
  18. * entry at {@link https://symfony.com/doc/any/components/form/form_events.html}.
  19. *
  20. * To learn how to dynamically modify forms using events check the cookbook
  21. * entry at {@link https://symfony.com/doc/any/cookbook/form/dynamic_form_modification.html}.
  22. *
  23. * @author Bernhard Schussek <bschussek@gmail.com>
  24. */
  25. final class FormEvents
  26. {
  27. /**
  28. * The PRE_SUBMIT event is dispatched at the beginning of the Form::submit() method.
  29. *
  30. * It can be used to:
  31. * - Change data from the request, before submitting the data to the form.
  32. * - Add or remove form fields, before submitting the data to the form.
  33. *
  34. * @Event("Symfony\Component\Form\Event\PreSubmitEvent")
  35. */
  36. public const PRE_SUBMIT = 'form.pre_submit';
  37. /**
  38. * The SUBMIT event is dispatched after the Form::submit() method
  39. * has changed the view data by the request data, or submitted and mapped
  40. * the children if the form is compound, and after reverse transformation
  41. * to normalized representation.
  42. *
  43. * It's also dispatched just before the Form::submit() method transforms back
  44. * the normalized data to the model and view data.
  45. *
  46. * So at this stage children of compound forms are submitted and synchronized, unless
  47. * their transformation failed, but a parent would still be at the PRE_SUBMIT level.
  48. *
  49. * Since the current form is not synchronized yet, it is still possible to add and
  50. * remove fields.
  51. *
  52. * @Event("Symfony\Component\Form\Event\SubmitEvent")
  53. */
  54. public const SUBMIT = 'form.submit';
  55. /**
  56. * The FormEvents::POST_SUBMIT event is dispatched at the very end of the Form::submit().
  57. *
  58. * It this stage the model and view data may have been denormalized. Otherwise the form
  59. * is desynchronized because transformation failed during submission.
  60. *
  61. * It can be used to fetch data after denormalization.
  62. *
  63. * The event attaches the current view data. To know whether this is the renormalized data
  64. * or the invalid request data, call Form::isSynchronized() first.
  65. *
  66. * @Event("Symfony\Component\Form\Event\PostSubmitEvent")
  67. */
  68. public const POST_SUBMIT = 'form.post_submit';
  69. /**
  70. * The FormEvents::PRE_SET_DATA event is dispatched at the beginning of the Form::setData() method.
  71. *
  72. * It can be used to:
  73. * - Modify the data given during pre-population;
  74. * - Keep synchronized the form depending on the data (adding or removing fields dynamically).
  75. *
  76. * @Event("Symfony\Component\Form\Event\PreSetDataEvent")
  77. */
  78. public const PRE_SET_DATA = 'form.pre_set_data';
  79. /**
  80. * The FormEvents::POST_SET_DATA event is dispatched at the end of the Form::setData() method.
  81. *
  82. * This event can be used to modify the form depending on the final state of the underlying data
  83. * accessible in every representation: model, normalized and view.
  84. *
  85. * @Event("Symfony\Component\Form\Event\PostSetDataEvent")
  86. */
  87. public const POST_SET_DATA = 'form.post_set_data';
  88. /**
  89. * Event aliases.
  90. *
  91. * These aliases can be consumed by RegisterListenersPass.
  92. */
  93. public const ALIASES = [
  94. PreSubmitEvent::class => self::PRE_SUBMIT,
  95. SubmitEvent::class => self::SUBMIT,
  96. PostSubmitEvent::class => self::POST_SUBMIT,
  97. PreSetDataEvent::class => self::PRE_SET_DATA,
  98. PostSetDataEvent::class => self::POST_SET_DATA,
  99. ];
  100. private function __construct()
  101. {
  102. }
  103. }