@@ -1036,14 +1036,21 @@ the work::
1036
1036
// ...
1037
1037
}
1038
1038
1039
- To call this, add ``data-action="live#action" `` and ``data-action-name ``
1040
- to an element (e.g. a button or form):
1039
+ .. versionadded :: 2.14
1040
+
1041
+ The ``data-live-action-param `` attribute way of specifying the action
1042
+ was added in Live Components 2.14. Previously, this was done with
1043
+ ``data-live-action-name ``.
1044
+
1045
+ To call this, trigger the ``action `` method on the ``live `` Stimulus
1046
+ controller and pass ``resetMax `` as a `Stimulus action parameter `_ called
1047
+ ``action ``:
1041
1048
1042
1049
.. code-block :: html+twig
1043
1050
1044
1051
<button
1045
1052
data-action="live#action"
1046
- data-action-name ="resetMax"
1053
+ data-live- action-param ="resetMax"
1047
1054
>Reset Min/Max</button>
1048
1055
1049
1056
Done! When the user clicks this button, a POST request will be sent that
@@ -1058,14 +1065,12 @@ You can also add several "modifiers" to the action:
1058
1065
<form>
1059
1066
<button
1060
1067
data-action="live#action"
1061
- data-action-name="prevent| debounce(300)|save"
1068
+ data-live- action-param=" debounce(300)|save"
1062
1069
>Save</button>
1063
1070
</form>
1064
1071
1065
- The ``prevent `` modifier would prevent the form from submitting
1066
- (``event.preventDefault() ``). The ``debounce(300) `` modifier will add
1067
- 300ms of "debouncing" before the action is executed. In other words, if
1068
- you click really fast 5 times, only one Ajax request will be made!
1072
+ The ``debounce(300) `` adds 300ms of "debouncing" before the action is executed.
1073
+ In other words, if you click really fast 5 times, only one Ajax request will be made!
1069
1074
1070
1075
Actions & Services
1071
1076
~~~~~~~~~~~~~~~~~~
@@ -1099,22 +1104,28 @@ This means that, for example, you can use action autowiring::
1099
1104
Actions & Arguments
1100
1105
~~~~~~~~~~~~~~~~~~~
1101
1106
1102
- .. versionadded :: 2.1
1107
+ .. versionadded :: 2.14
1103
1108
1104
- The ability to pass arguments to actions was added in version 2.1.
1109
+ The ``data-live-{NAME}-param `` attribute way of specifying action
1110
+ arguments was added in Live Components 2.14. Previously, this was done
1111
+ inside the ``data-live-action-name `` attribute.
1105
1112
1106
- You can also provide custom arguments to your action:
1113
+ You can also pass arguments to your action by adding each as a
1114
+ `Stimulus action parameter `_:
1107
1115
1108
1116
.. code-block :: html+twig
1109
1117
1110
1118
<form>
1111
1119
<button
1112
1120
data-action="live#action"
1113
- data-action-name="addItem(id={{ item.id }}, itemName=CustomItem)"
1121
+ data-live-action-param="addItem"
1122
+
1123
+ data-live-id-param="{{ item.id }}"
1124
+ data-live-item-name-param="CustomItem"
1114
1125
>Add Item</button>
1115
1126
</form>
1116
1127
1117
- In your component, to allow each argument to be passed, we need to add
1128
+ In your component, to allow each argument to be passed, add
1118
1129
the ``#[LiveArg()] `` attribute::
1119
1130
1120
1131
// src/Components/ItemList.php
@@ -1135,10 +1146,6 @@ the ``#[LiveArg()]`` attribute::
1135
1146
}
1136
1147
}
1137
1148
1138
- Normally, the argument name in PHP - e.g. ``$id `` - should match the
1139
- argument name used in Twig ``id={{ item.id }} ``. But if they don't
1140
- match, you can pass an argument to ``LiveArg ``, like we did with ``itemName ``.
1141
-
1142
1149
Actions and CSRF Protection
1143
1150
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1144
1151
@@ -1216,10 +1223,11 @@ to handle the files and tell the component when the file should be sent:
1216
1223
1217
1224
.. code-block :: html+twig
1218
1225
1219
- <p>
1220
- <input type="file" name="my_file" />
1221
- <button data-action="live#action" data-action-name="files|my_action" />
1222
- </p>
1226
+ <input type="file" name="my_file" />
1227
+ <button
1228
+ data-action="live#action"
1229
+ data-live-action-param="files|my_action"
1230
+ />
1223
1231
1224
1232
To send a file (or files) with an action use ``files `` modifier.
1225
1233
Without an argument it will send all pending files to your action.
@@ -1233,11 +1241,11 @@ You can also specify a modifier parameter to choose which files should be upload
1233
1241
<input type="file" name="multiple[]" multiple />
1234
1242
1235
1243
{# Send only file from first input #}
1236
- <button data-action="live#action" data-action-name ="files(my_file)|myAction" />
1244
+ <button data-action="live#action" data-live- action-param ="files(my_file)|myAction" />
1237
1245
{# You can chain modifiers to send multiple files #}
1238
- <button data-action="live#action" data-action-name ="files(my_file)|files(multiple[])|myAction" />
1246
+ <button data-action="live#action" data-live- action-param ="files(my_file)|files(multiple[])|myAction" />
1239
1247
{# Or send all pending files #}
1240
- <button data-action="live#action" data-action-name ="files|myAction" />
1248
+ <button data-action="live#action" data-live- action-param ="files|myAction" />
1241
1249
</p>
1242
1250
1243
1251
The files will be available in a regular ``$request->files `` files bag::
@@ -1456,8 +1464,8 @@ Next, tell the ``form`` element to use this action:
1456
1464
1457
1465
{{ form_start(form, {
1458
1466
attr: {
1459
- 'data-action': 'live#action',
1460
- 'data-action-name ': 'prevent| save'
1467
+ 'data-action': 'live#action:prevent ',
1468
+ 'data-live- action-param ': 'save'
1461
1469
}
1462
1470
}) }}
1463
1471
@@ -1757,7 +1765,8 @@ and ``removeComment()`` actions:
1757
1765
{% for key, commentForm in form.comments %}
1758
1766
<button
1759
1767
data-action="live#action"
1760
- data-action-name="removeComment(index={{ key }})"
1768
+ data-live-action-param="removeComment"
1769
+ data-live-index-param="{{ key }}"
1761
1770
type="button"
1762
1771
>X</button>
1763
1772
@@ -1769,7 +1778,7 @@ and ``removeComment()`` actions:
1769
1778
1770
1779
<button
1771
1780
data-action="live#action"
1772
- data-action-name ="addComment"
1781
+ data-live- action-param ="addComment"
1773
1782
type="button"
1774
1783
>+ Add Comment</button>
1775
1784
@@ -2172,8 +2181,8 @@ re-rendered. In your template, render errors using an ``_errors`` variable:
2172
2181
2173
2182
<button
2174
2183
type="submit"
2175
- data-action="live#action"
2176
- data-action-name="prevent| save"
2184
+ data-action="live#action:prevent "
2185
+ data-live- action-param=" save"
2177
2186
>Save</button>
2178
2187
2179
2188
Once a component has been validated, the component will "remember" that
@@ -2766,7 +2775,7 @@ suppose your child component has:
2766
2775
2767
2776
.. code-block :: html
2768
2777
2769
- <button data-action =" live#action" data-action-name =" save" >Save</button >
2778
+ <button data-action =" live#action" data-live- action-param =" save" >Save</button >
2770
2779
2771
2780
When the user clicks that button, it will attempt to call the ``save ``
2772
2781
action in the *child * component only, even if the ``save `` action
@@ -2930,7 +2939,7 @@ In the ``EditPost`` template, you render the
2930
2939
2931
2940
<button
2932
2941
data-action="live#action"
2933
- data-action-name ="save"
2942
+ data-live- action-param ="save"
2934
2943
>Save</button>
2935
2944
</form>
2936
2945
</div>
@@ -3467,3 +3476,4 @@ bound to Symfony's BC policy for the moment.
3467
3476
.. _`PostMount hook` : https://symfony.com/bundles/ux-twig-component/current/index.html#postmount-hook
3468
3477
.. _`validation groups` : https://symfony.com/doc/current/form/validation_groups.html
3469
3478
.. _morphing library : https://github.com/bigskysoftware/idiomorph
3479
+ .. _`Stimulus action parameter` : https://stimulus.hotwired.dev/reference/actions#action-parameters
0 commit comments