Skip to content

Commit 53fb3c7

Browse files
committed
Add new way of mapping form data
1 parent 92e8e2a commit 53fb3c7

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

form/data_mappers.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,45 @@ method::
189189
Cool! When using the ``ColorType`` form, the custom data mapper methods will
190190
create a new ``Color`` object now.
191191

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+
192231
.. caution::
193232

194233
When a form has the ``inherit_data`` option set to ``true``, it does not use the data mapper and

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy