@@ -189,6 +189,45 @@ method::
189
189
Cool! When using the ``ColorType `` form, the custom data mapper methods will
190
190
create a new ``Color `` object now.
191
191
192
+ Mapping Form Fields Using Callbacks
193
+ -----------------------------------
194
+
195
+ Conveniently, you can also map data from and into a form field by using the
196
+ ``getter `` and ``setter `` options. For example, suppose you have a form with some
197
+ fields and only one of them needs to be mapped in some special way or you only
198
+ need to change how it's written into the underlying object. In that case, register
199
+ a PHP callable that is able to write or read to/from that specific object::
200
+
201
+ public function buildForm(FormBuilderInterface $builder, array $options)
202
+ {
203
+ // ...
204
+
205
+ $builder->add('state', ChoiceType::class, [
206
+ 'choices' => [
207
+ 'active' => true,
208
+ 'paused' => false,
209
+ ],
210
+ 'getter' => function (Task $task, FormInterface $form): bool {
211
+ return !$task->isCancelled() && !$task->isPaused();
212
+ },
213
+ 'setter' => function (Task &$task, bool $state, FormInterface $form): void {
214
+ if ($state) {
215
+ $task->activate();
216
+ } else {
217
+ $task->pause();
218
+ }
219
+ },
220
+ ]);
221
+ }
222
+
223
+ If available, these options have priority over the property path accessor and
224
+ the default data mapper will still use the :doc: `PropertyAccess component </components/property_access >`
225
+ for the other form fields.
226
+
227
+ .. versionadded :: 5.2
228
+
229
+ The ``getter `` and ``setter `` options were introduced in Symfony 5.2.
230
+
192
231
.. caution ::
193
232
194
233
When a form has the ``inherit_data `` option set to ``true ``, it does not use the data mapper and
0 commit comments