|
| 1 | +# Input Transformer |
| 2 | + |
| 3 | +Input Transformers will be applied, before a form gets pre-populated with given data. |
| 4 | + |
| 5 | +Mostly this happens, if you're using a funnel output workflow. |
| 6 | +There, the form data gets normalized and needs to be transformed back, before we can populate a form with data. |
| 7 | + |
| 8 | +## Available Core Input Transformer |
| 9 | + |
| 10 | +| Available Transformer | Field | Service | |
| 11 | +|-----------------------|----------------------------------------|-------------------------------------------------------------------| |
| 12 | +| `date_transformer` | `date`, `datetime`, `time`, `birthday` | `@FormBuilderBundle\Transformer\Output\DateDataObjectTransformer` | |
| 13 | + |
| 14 | +> **Don't get confused!** |
| 15 | +> Some core output transformers also supports input transformation. |
| 16 | +> Since we won't the namespace that because of BC reasons, we'll keep it that way (for now). |
| 17 | +
|
| 18 | +## Override default Input Transformer |
| 19 | + |
| 20 | +If you want to transform the text field value for example, you need to add your own transformer. |
| 21 | +First, let FormBuilder know about your transformer. |
| 22 | + |
| 23 | +> You're also able to set up your custom input transform for your dynamic fields via the `input_transformer` configuration node. |
| 24 | +
|
| 25 | + ```yaml |
| 26 | +form_builder: |
| 27 | + types: |
| 28 | + text: |
| 29 | + input_transformer: text_input_transformer |
| 30 | +``` |
| 31 | +
|
| 32 | +Now you need to register your new transformer: |
| 33 | +
|
| 34 | +```yaml |
| 35 | + App\OutputTransformer\TextInputTransformer: |
| 36 | + autowire: true |
| 37 | + tags: |
| 38 | + - { name: form_builder.transformer.input, type: text_input_transformer } |
| 39 | +``` |
| 40 | +
|
| 41 | +*** |
| 42 | +
|
| 43 | +After that, you have to set up a PHP-Service: |
| 44 | +
|
| 45 | +```php |
| 46 | +<?php |
| 47 | + |
| 48 | +namespace App\InputTransformer; |
| 49 | + |
| 50 | +use FormBuilderBundle\Model\FieldDefinitionInterface; |
| 51 | +use FormBuilderBundle\Transformer\Input\InputTransformerInterface; |
| 52 | + |
| 53 | +class TextInputTransformer implements InputTransformerInterface |
| 54 | +{ |
| 55 | + public function getValueReverse(FieldDefinitionInterface $fieldDefinition, mixed $formValue): string |
| 56 | + { |
| 57 | + // manipulate or change the value |
| 58 | + return $rawValue; |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +*** |
| 64 | + |
| 65 | +## Custom input transformer |
| 66 | + |
| 67 | +After you've created a [custom form type](./../40_CustomFormType.md) (and/or you need it in a funnel output chanel), |
| 68 | +you may want to control the input values too. Configure your PHP Service same as explained above. |
| 69 | + |
| 70 | +```yaml |
| 71 | +form_builder: |
| 72 | + types: |
| 73 | + your_custom_type: |
| 74 | + input_transformer: custom_input_transformer |
| 75 | + |
| 76 | +services: |
| 77 | + App\OutputTransformer\MyCustomInputTransformer: |
| 78 | + autowire: true |
| 79 | + tags: |
| 80 | + - { name: form_builder.transformer.input, type: custom_input_transformer } |
| 81 | +``` |
0 commit comments