@@ -1163,9 +1163,9 @@ Uploading files
1163
1163
1164
1164
.. versionadded :: 2.9
1165
1165
1166
- The ability to pass arguments to actions was added in version 2.9.
1166
+ The ability to upload files to actions was added in version 2.9.
1167
1167
1168
- Files aren't send to the component by default. You need to use a live action
1168
+ Files aren't sent to the component by default. You need to use a live action
1169
1169
to handle the files and tell the component when the file should be sent:
1170
1170
1171
1171
.. code-block :: html+twig
@@ -1179,24 +1179,46 @@ To send a file (or files) with an action use `files` modifier.
1179
1179
Without an argument it will send all pending files to your action.
1180
1180
You can also specify a modifier parameter to choose which files should be upload.
1181
1181
1182
- The files will be available in a regular `$request->files ` files bag.
1183
- You can use `data-model ` as a key for your files instead of relying on
1184
- input `name ` attribute.
1185
1182
1186
1183
.. code-block :: html+twig
1187
1184
1188
1185
<p>
1189
- <input type="file" name="my_file" data-model="single" />
1186
+ <input type="file" name="my_file" />
1190
1187
<input type="file" name="multiple[]" multiple />
1191
1188
1192
1189
{# Send only file from first input #}
1193
- <button data-action="live#action" data-action-name="files(single)|my_action " />
1190
+ <button data-action="live#action" data-action-name="files(my_file)|myAction " />
1194
1191
{# You can chain modifiers to send multiple files #}
1195
- <button data-action="live#action" data-action-name="files(single )|files(multiple[])|my_action " />
1192
+ <button data-action="live#action" data-action-name="files(my_file )|files(multiple[])|myAction " />
1196
1193
{# Or send all pending files #}
1197
- <button data-action="live#action" data-action-name="files|my_action " />
1194
+ <button data-action="live#action" data-action-name="files|myAction " />
1198
1195
</p>
1199
1196
1197
+ The files will be available in a regular `$request->files ` files bag::
1198
+
1199
+ // src/Components/FileUpload.php
1200
+ namespace App\Components;
1201
+
1202
+ use Symfony\Component\HttpFoundation\Request;
1203
+ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1204
+ use Symfony\UX\LiveComponent\Attribute\LiveAction;
1205
+ use Symfony\UX\LiveComponent\DefaultActionTrait;
1206
+
1207
+ #[AsLiveComponent]
1208
+ class FileUpload
1209
+ {
1210
+ use DefaultActionTrait;
1211
+
1212
+ #[LiveAction]
1213
+ public function myAction(Request $request)
1214
+ {
1215
+ $file = $request->files->get('my_file');
1216
+ $multiple = $request->files->all('multiple');
1217
+
1218
+ // Handle files
1219
+ }
1220
+ }
1221
+
1200
1222
.. tip ::
1201
1223
1202
1224
Remember that in order to send multiple files from a single input you
0 commit comments