Skip to content

Commit 88e9b4a

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [Form] Adding Stimulus code Events are dispatched, not thrown [Doctrine/Events] Fix bad argument name in example
2 parents e49b0db + 6eb0b31 commit 88e9b4a

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

components/http_kernel.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ Handling Exceptions: the ``kernel.exception`` Event
514514
:ref:`Kernel Events Information Table <component-http-kernel-event-table>`
515515

516516
If an exception is thrown at any point inside ``HttpKernel::handle()``, another
517-
event - ``kernel.exception`` is thrown. Internally, the body of the ``handle()``
517+
event - ``kernel.exception`` is dispatched. Internally, the body of the ``handle()``
518518
method is wrapped in a try-catch block. When any exception is thrown, the
519519
``kernel.exception`` event is dispatched so that your system can somehow respond
520520
to the exception.

doctrine/events.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ with the ``doctrine.event_listener`` tag:
200200
201201
use App\EventListener\SearchIndexer;
202202
203-
return static function (ContainerConfigurator $container) {
203+
return static function (ContainerConfigurator $configurator) {
204204
$services = $configurator->services();
205205
206206
// listeners are applied by default to all Doctrine connections

form/form_collections.rst

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ it will receive an *unknown* number of tags. Otherwise, you'll see a
238238

239239
The ``allow_add`` option also makes a ``prototype`` variable available to you.
240240
This "prototype" is a little "template" that contains all the HTML needed to
241-
dynamically create any new "tag" forms with JavaScript. To render the prototype, add
241+
dynamically create any new "tag" forms with JavaScript.
242+
243+
Let's start with plain JavaScript (Vanilla JS) – if you're using Stimulus, see below.
244+
245+
To render the prototype, add
242246
the following ``data-prototype`` attribute to the existing ``<ul>`` in your
243247
template:
244248

@@ -334,6 +338,49 @@ into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` obje
334338

335339
You can find a working example in this `JSFiddle`_.
336340

341+
JavaScript with Stimulus
342+
~~~~~~~~~~~~~~~~~~~~~~~~
343+
344+
If you're using `Stimulus`_, wrap everything in a ``<div>``:
345+
346+
.. code-block:: html+twig
347+
348+
<div {{ stimulus_controller('form-collection') }}
349+
data-form-collection-index-value="{{ form.tags|length > 0 ? form.tags|last.vars.name + 1 : 0 }}"
350+
data-form-collection-prototype-value="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}"
351+
>
352+
<ul {{ stimulus_target('form-collection', 'collectionContainer') }}></ul>
353+
<button type="button" {{ stimulus_action('form-collection', 'addCollectionElement') }}>Add a tag</button>
354+
</div>
355+
356+
Then create the controller:
357+
358+
.. code-block:: javascript
359+
360+
// assets/controllers/form-collection_controller.js
361+
362+
import { Controller } from '@hotwired/stimulus';
363+
364+
export default class extends Controller {
365+
static targets = ["collectionContainer"]
366+
367+
static values = {
368+
index : Number,
369+
prototype: String,
370+
}
371+
372+
addCollectionElement(event)
373+
{
374+
const item = document.createElement('li');
375+
item.innerHTML = this.prototypeValue.replace(/__name__/g, this.indexValue);
376+
this.collectionContainerTarget.appendChild(item);
377+
this.indexValue++;
378+
}
379+
}
380+
381+
Handling the new Tags in PHP
382+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
383+
337384
To make handling these new tags easier, add an "adder" and a "remover" method
338385
for the tags in the ``Task`` class::
339386

@@ -662,3 +709,4 @@ the relationship between the removed ``Tag`` and ``Task`` object.
662709
.. _`@a2lix/symfony-collection`: https://github.com/a2lix/symfony-collection
663710
.. _`symfony-collection`: https://github.com/ninsuo/symfony-collection
664711
.. _`ArrayCollection`: https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html
712+
.. _`Stimulus`: https://symfony.com/doc/current/frontend/encore/simple-example.html#stimulus-symfony-ux

0 commit comments

Comments
 (0)