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."
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 at 15:09
  • It would but seems a bit overengineering for this problem. Commented May 5 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); 
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()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.