Skip to content

Commit 3f36f08

Browse files
committed
Add an example for file processors with multiple elements
Closes #528
1 parent 19ad003 commit 3f36f08

1 file changed

Lines changed: 61 additions & 6 deletions

File tree

docs/php/api/form_builder/form_fields.md

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,6 @@ ContentLanguageFormField::create()
585585

586586
The `FileProcessorFormField` is used to upload files to the server via the file processor.
587587

588-
By default, `FileProcessorFormField` objects register a [custom form field data processor](validation_data.md#customformfielddataprocessor) to add the appropriate array of fileIDs to the `$parameters` array directly, using the object property as the array key.
589-
If `isSingleFileUpload()` is enabled, the value will be added to the data sub-array of the parameters array and the value can be null.
590-
591588
The checks for the correct file size or extension don't take place in the FormField, this must be defined in the `IFileProcessor`.
592589

593590
__The field supports other settings:__
@@ -596,7 +593,10 @@ __The field supports other settings:__
596593
- `context(array $context)` and `getContext()` can be used to set and get the context of the file processor.
597594
- `singleFileUpload($singleFileUpload = true)` and `isSingleFileUpload()` can be used to set and check if only one file can be uploaded.
598595

599-
Example:
596+
#### Single File Upload
597+
598+
When `singleFileUpload()` is enabled, the field value is a single integer (the file ID) or `null`.
599+
The value is added to the `data` sub-array of the parameters array, making it suitable for direct database column mapping.
600600

601601
```php
602602
FileProcessorFormField::create('exampleFileID')
@@ -606,13 +606,68 @@ FileProcessorFormField::create('exampleFileID')
606606
->bigPreview()
607607
```
608608

609+
#### Multiple File Uploads
610+
611+
When `singleFileUpload()` is not called, the field defaults to accepting multiple files.
612+
The field value is an array of file IDs.
613+
Since arrays cannot be stored in a single database column, the field automatically registers a [custom form field data processor](validation_data.md#customformfielddataprocessor) that places the array of file IDs into the `$parameters` array directly, using the object property as the array key.
614+
615+
This means the file IDs will **not** be part of `$parameters['data']` and must be handled explicitly in your code.
616+
617+
```php
618+
FileProcessorFormField::create('attachments')
619+
->objectType('foo.bar.example')
620+
->label('foo.bar.example.attachments')
621+
```
622+
623+
The file IDs are available in the form data under the object property key:
624+
625+
```php
626+
$formData = $this->form->getData();
627+
628+
// File IDs are in the top-level parameters array, not in 'data'.
629+
$fileIDs = $formData['attachments']; // int[]
630+
```
631+
632+
If the default data processor does not match your data structure, you can add your own `CustomFormDataProcessor` after building the form.
633+
The following example converts the array of file IDs to a comma-separated string for storage in a single database column:
634+
635+
```php
636+
$this->form->getDataHandler()->addProcessor(
637+
new CustomFormDataProcessor(
638+
'attachments',
639+
static function (IFormDocument $document, array $parameters) {
640+
$field = $document->getNodeById('attachments');
641+
\assert($field instanceof FileProcessorFormField);
642+
643+
$value = $field->getValue();
644+
if ($value === null || $value === []) {
645+
$parameters['data']['attachments'] = '';
646+
} else {
647+
$parameters['data']['attachments'] = \implode(',', $value);
648+
}
649+
650+
return $parameters;
651+
},
652+
static function (IFormDocument $document, array $data, IStorableObject $object) {
653+
$value = $data['attachments'] ?? '';
654+
if ($value !== '') {
655+
$data['attachments'] = \array_map(\intval(...), \explode(',', $value));
656+
} else {
657+
$data['attachments'] = [];
658+
}
659+
660+
return $data;
661+
}
662+
)
663+
);
664+
```
665+
609666
#### Additional Buttons
610667

611668
Additional buttons can be added with `addActionButton(string $actionName, string $title, string $template, string $application = 'wcf', ?IFontAwesomeIcon $icon = null)`.
612669
When this button is pressed, the `fileProcessorCustomAction` event is fired, which can be used via JavaScript to perform additional actions.
613670

614-
Example:
615-
616671
```ts
617672
document.getElementById('exampleFileIDContainer').addEventListener('fileProcessorCustomAction', (event: CustomEvent<string>) => {
618673
const actionName = event.detail;

0 commit comments

Comments
 (0)