symfony: Wrong content of Form::getViewData() on forms whith children

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.2.7 (exists from 2.8 and may be early)

Code example (in controller):

$form =  $this->createFormBuilder(null, [
        'csrf_protection' => false,
    ])
    ->add('e', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
        'class'     => 'AppBundle:Entity',
        'multiple'  => true,
    ])
    ->getForm()
    ->submit(['e' => [3, 180]])
;

as result $form->getViewData() is not plain array eq to sumbitted data but:

array:1 [▼
  "e" => ArrayCollection {#6289 ▼
    -elements: array:2 [▶]
  }
]

bug in Form::submit() $viewData = $this->config->getCompound() ? $this->viewData : $submittedData; line 607 initially takes viewData, but in processig children elements used PropertyPathMapper (for nested forms) $this->config->getDataMapper()->mapFormsToData($childrenIterator, $viewData); line 630 , which fill $viewData with Model Data $this->propertyAccessor->setValue($data, $propertyPath, $form->getData()); line 93 and as result in $viewData we have mixed content of View and Model Data

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

@xabbuh and that’s the problem. In any case when I use method getViewData I expect receive View Data but receive mixed content. And this behavior is not described.

This is a behaviour that has caused us issues too. We have the same use case as described above in that we have a form which drives a paginated set of data and the form data needs to be represented in a form suitable for paginating (within a URL), therefore we need the form view data fully transformed to viewData.

In our case, to work around this issue we had to choose between (a) refactoring nested forms into flat forms without nesting OR (b) manually walking the form building viewdata for each subform. I chose the former, but appreciate for others this won’t be possible.

Hmm, but

$normData = $this->viewToNorm($viewData);

line 634 send to ViewTransformer mix of view data (from main form) and model data (from children forms). Is it really expected?

And yes, currently I’m using ViewTransformer in the main form to fix this problem but this transformer looks weird. =)