Skip to content

Commit 093244f

Browse files
committedJan 3, 2025
minor #6650 Added documentation on JavaScript events (rimi-itk)
This PR was merged into the 4.x branch. Discussion ---------- Added documentation on JavaScript events First stab at adding the missing JavaScripts events documentation (cf. #5922). I've added a general overview of all JavaScript events on the Events page and details on collection events on the Collection Field page. Any suggestions on other or better ways to organize the event documentation are welcome. I've tried to document the additional information, if any, available in the event, i.e. the event's `detail` property value, but I'm not sure if I've done it in the best way possible. Any suggestions for improvements are welcome. Commits ------- b8ce3e1 Added documentation on JavaScript events
2 parents b3d234b + b8ce3e1 commit 093244f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
 

‎doc/events.rst

+43
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,47 @@ property of the ``BlogPost`` entity before persisting it:
7777
}
7878
}
7979
80+
JavaScript Events
81+
-----------------
82+
83+
EasyAdmin triggers some `JavaScript events`_ when the user interacts with entity forms:
84+
85+
================================ ============================================== ================================ ==========
86+
Event type Occurs when Event detail Cancelable
87+
================================ ============================================== ================================ ==========
88+
``'ea.form.error'`` User submits a form that has validation errors ``{page: pageName, form: form}`` true
89+
-------------------------------- ---------------------------------------------- -------------------------- ----------
90+
``'ea.form.submit'`` User submits a form ``{page: pageName, form: form}`` true
91+
-------------------------------- ---------------------------------------------- -------------------------------- ----------
92+
``'ea.collection.item-added'`` Item added to collection ``{newElement: element}`` false
93+
-------------------------------- ---------------------------------------------- -------------------------------- ----------
94+
``'ea.collection.item-removed'`` Item removed from collection false
95+
================================ ============================================== ================================ ==========
96+
97+
(see `CustomEvent: detail property
98+
<https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail>`_ for
99+
details on "Event detail" and `Event: cancelable property
100+
<https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable>`_ for
101+
details on "Cancelable".)
102+
103+
Example usage:
104+
105+
.. code-block:: javascript
106+
107+
document.addEventListener('ea.form.error', (event) => {
108+
const {page, form} = event.detail
109+
alert(`The ${page} form contains errors. Please resolve these before submitting again.`)
110+
})
111+
112+
document.addEventListener('ea.form.submit', (event) => {
113+
const {page, form} = event.detail
114+
console.debug(`${page} form submitted`, form)
115+
})
116+
117+
See :doc:`Collection Field JavaScript Events
118+
</fields/CollectionField#javascript-events>` for details on and example use of
119+
the ``'ea.collection.*'`` events.
120+
121+
80122
.. _`Symfony events`: https://symfony.com/doc/current/event_dispatcher.html
123+
.. _`JavaScript events`: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events

‎doc/fields/CollectionField.rst

+25
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,30 @@ class name of the controller as the first argument::
195195

196196
The ``useEntryCrudForm()`` method requires Symfony 6.1 or newer version.
197197

198+
JavaScript Events
199+
-----------------
200+
201+
When an item is added to a collection field, a `CustomEvent
202+
<https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent>`_ with type
203+
``'ea.collection.item-added'`` is dispatched, and when an item is removed, an
204+
`Event <https://developer.mozilla.org/en-US/docs/Web/API/Event/Event>`_ with
205+
type ``'ea.collection.item-removed'`` dispatched.
206+
207+
The ``'ea.collection.item-added'`` event contains information about the added
208+
item in the `detail property
209+
<https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail>`_:
210+
211+
.. code-block:: javascript
212+
213+
document.addEventListener('ea.collection.item-added', (event) => {
214+
const {newElement} = event.detail
215+
console.debug(newElement, 'added to collection')
216+
})
217+
218+
document.addEventListener('ea.collection.item-removed', (event) => {
219+
// Do something with the event
220+
console.debug('item removed from collection')
221+
})
222+
198223
.. _`CollectionType`: https://symfony.com/doc/current/reference/forms/types/collection.html
199224
.. _`documentation about Symfony CollectionType options`: https://symfony.com/doc/current/reference/forms/types/collection.html#field-options

0 commit comments

Comments
 (0)
Please sign in to comment.