You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #14241 [Form] Add new way of mapping form data using callback functions (yceruto)
This PR was merged into the master branch.
Discussion
----------
[Form] Add new way of mapping form data using callback functions
Documenting new feature symfony/symfony#37968
I don't think we need to cover all possible situations; the current example covers 3 of them, except the second one, but it seems enough for me, wdyt?
> [...] you'll have to write your own data mapper in the following situations:
>* When the property path differs for reading and writing
>* When several form fields are mapped to a single method
>* When you need to read data based on the model's state
>* When the mapping of the model depends on the submitted form data
...
By the way, the second situation "when several form fields are mapped to a single method" can be achieved this way:
```php
$builder
->add('foo', TextType::class, [
'setter' => function (Foobar $foobar, string $value) use (&$foo) {
$foo = $value;
},
])
->add('bar', TextType::class, [
'setter' => function (Foobar $foobar, string $bar) use (&$foo) {
$foobar->doSomething($foo, $bar);
},
])
;
```
Since the data mapper will follow the same order in which the fields were defined, we can trust that the foo "setter" will be executed first and so the second "setter" will have the right value at the moment this function `$foobar->doSomething($foo, $bar)` is being executed.
---
ping @alcaeus :)
Commits
-------
53fb3c7 Add new way of mapping form data
0 commit comments