I have a Symfony form, some fields are from entity, some not (unmapped).
I need to get full form data with those unmapped fields. I know there is a way to get those one-by-one (eg $form['unmapped']) but is there more intelligent way?
- viewData is not it - does not contain unmapped fields
- extraData - comment in code: "The submitted values that don't belong to any children."
asked May 3, 2025 at 13:45
JohnSmith
5081 gold badge5 silver badges21 bronze badges
-
Some would argue the "more intelligent way" would be to utilize DTO's. Though this is old, it is still helpful: symfonycasts.com/screencast/symfony-forms/form-dtocraigh– craigh2025年05月03日 15:09:21 +00:00Commented May 3, 2025 at 15:09
-
It would but seems a bit overengineering for this problem.JohnSmith– JohnSmith2025年05月05日 18:19:59 +00:00Commented May 5, 2025 at 18:19
2 Answers 2
I don't think there is a simple method in form object.
You can get them like this:
$extraFields = array_diff_key(
$form->all(),
get_object_vars($form->getData()) // or just $form->getData()
);
$unmappedData = array_map(fn ($field) => $field->getData(), $extraFields);
Sign up to request clarification or add additional context in comments.
1 Comment
JohnSmith
This is simpler, no?
$data = array_map(fn ($v) => $v->getData(), $form->all());. Or why array_diff_key is needed? I need full form data - both mapped and unmapped.Seems there is no method for this.
I needed to iterate over all form fields and get their data:
$data = array_map(fn ($v) => $v->getData(), $form->all());
If you have nested forms, then something along those lines (untested):
$data = array_map(function ($field) {
if ($field->getConfig()->getCompound()) {
$fieldsData = ... // call recursively
return $fieldsData;
}
return $field->getData();
}, $form->all());
answered May 5, 2025 at 7:57
JohnSmith
5081 gold badge5 silver badges21 bronze badges
Comments
lang-php