@@ -541,6 +541,56 @@ class RandomNumberComponent
541541}
542542```
543543
544+ ##### Passing arguments to live action
545+
546+ You can also provide custom arguments to your action. For example Symfony Form CollectionType needs some JavaScript
547+ to make form dynamic, but it can be avoided.
548+
549+ In component template we use "add entry" and "remove" entry buttons and configure the custom actions that are
550+ implemented in your custom live component. Specific part for remove entry is using an named action argument to provide
551+ the index of the entry which we need to remove. You can provide as many arguments as you need.
552+
553+ ``` twig
554+ <form>
555+ <div>
556+ Entries:
557+ <button data-action="live#action" data-action-name="addEntry">Add new entry</button>
558+ </div>
559+ {% for entry in this.formValues.entries %}
560+ .. render your entry ..
561+ <button data-action="live#action" data-action-name="removeEntry(index={{ loop.index0 }})">remove entry</button>
562+ {% endfor %}
563+ </form>
564+ ```
565+
566+ In component for custom arguments to be injected we need to use ` #[LiveArg('..')] ` attribute, otherwise it would be
567+ ignored.
568+
569+ ``` php
570+ // src/Components/CollectionComponent.php
571+ namespace App\Components;
572+
573+ // ...
574+ use Symfony\UX\LiveComponent\Attribute\LiveArg;
575+ use Psr\Log\LoggerInterface;
576+
577+ class CollectionComponent
578+ {
579+ // ...
580+
581+ public function addEntry()
582+ {
583+ $this->formValues['entries'][] = [];
584+ }
585+
586+ #[LiveAction]
587+ public function removeEntry(#[LiveArg] int $index)
588+ {
589+ array_splice($this->formValues['entries'], $index, 1);
590+ }
591+ }
592+ ```
593+
544594### Actions and CSRF Protection
545595
546596When you trigger an action, a POST request is sent that contains
0 commit comments