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