Reputation: 1075
Is it possible to use Data transformers to merge (n) fields in a form into one persistable field? If it's possible, how to do it? The cookbook only gives an example to transform one piece of data into another type, but I need to be able to dump N fields into only one for persistence. So if I'm showing 6 fields in the form, only 3 are real fields in DB table, first and second fields are to persisted as is, but the remaining 4 fields are to be store in the third table column.
Upvotes: 3
Views: 2473
Reputation: 20193
You should do it via FormEvent::POST_SUBMIT
event.
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html
Basically, something like this:
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
// entity or array
$data = $event->getData();
// get data directly from form
$concatData = $form->get('non_mapped_field1_1')->getData() . ',' . $form->get('non_mapped_field1_2')->getData();
// assumig that data is entity class
$data->setSomeField($concatData);
}
);
Upvotes: 6