0

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
2
  • 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-dto Commented May 3, 2025 at 15:09
  • It would but seems a bit overengineering for this problem. Commented May 5, 2025 at 18:19

2 Answers 2

0

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);
answered May 4, 2025 at 10:58
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.