Skip to content

Commit 60f71fc

Browse files
committed
Add code example for the usage of CustomFormDataProcessor
See #421
1 parent 708aac1 commit 60f71fc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/php/api/form_builder/validation_data.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,38 @@ This way, the relevant database object action method has access to the data to s
111111
The constructor of `CustomFormDataProcessor` requires an id (that is primarily used in error messages during the validation of the second parameter) and callables for `IFormDataProcessor::processFormData()` and `IFormDataProcessor::processObjectData()` which are passed the same parameters as the `IFormDataProcessor` methods.
112112
Only one of the callables has to be given, the other one then defaults to simply returning the relevant array unchanged.
113113

114+
##### Example
115+
116+
The following source code adds a custom processor that handles the return of `MultilineItemListFormField` and converts the content of an array into a multiline string in order to store it in the database.
117+
118+
```php
119+
$form->getDataHandler()->addProcessor(
120+
new CustomFormDataProcessor(
121+
'additionalItems',
122+
static function (IFormDocument $document, array $parameters) {
123+
$additionalItems = $document->getNodeById('additionalItems');
124+
\assert($additionalItems instanceof MultilineItemListFormField);
125+
126+
$value = $additionalItems->getValue();
127+
if ($value === null || $value === []) {
128+
$parameters['data']['additionalItems'] = null;
129+
} else {
130+
$parameters['data']['additionalItems'] = \implode("\n", $value);
131+
}
132+
133+
return $parameters;
134+
},
135+
static function (IFormDocument $document, array $data, IStorableObject $object) {
136+
if ($object->additionalItems !== null) {
137+
$data['additionalItems'] = \explode("\n", $data['additionalItems']);
138+
}
139+
140+
return $data;
141+
}
142+
)
143+
);
144+
```
145+
114146

115147
#### `VoidFormDataProcessor`
116148

0 commit comments

Comments
 (0)