How to flatten the rendering of a nested form

62 Views Asked by At

In my use case, i use the Symfony form system mainly for the rendering facilities. I don't use the form system facilities to handle the request data sent by the form which is handled by other piece of internal code.

My form contains a nested form with some form elements. I would like to flatten the rendering of these nested form elements to make them appearing at the same level of the parent form, but preserving the fact that the key/values of the elements of the nested form are under the key name of the nested form in the request data when the form is submited.

Exemple:

My form is actually rendered like this:

<form>
elem1label:        input1
elem2label:        input2
nestformlabel:     nestelem1label : input1
                   nestelem2label : input2

submit button
</form>

Data in the HTTP request when the form is submitted is formated like this:

[
  'form' => [
     'elem1' => val,
     'elem2' => val,
     'nestform' => [
         nestelem1 => val,
         nestelem2 => val
     ]
  ]
]

I would like to render this form like this (ie without all embedding div created around the nested elements like if they were top level elements):

<form>
elem1label:        input1
elem2label:        input2
nestelem1label:    input1
nestelem2label:    input2

submit button
</form>

But i would like to keep the data structure that is submited by the form:

[
  'form' => [
     'elem1' => val,
     'elem2' => val,
     'nestform' => [
         nestelem1 => val,
         nestelem2 => val
     ]
  ]
]

Is there a simple way to flatten the rendering without having to deal with form theme customizations ? Because i build the form object pretty manually using the formbuilder, without any "domain" object attached to it, i though it would be possible to not declare a nested Formtype, but define every elements in top level form and changing the element path, something like [subkey]elemname but i have not found anything in this direction. I played with the "property_path" but without luck.. Of course i can add some processing of the request data server side to renormalize the data array like expected but i hope there is a simpler and nicer way to handle this.

1

There are 1 best solutions below

2
sgt-hartman On

So, answering my own question, i've successfully achieved using theming as a workaround:

{%- block _<root_form_name>_<nested_form_name>_row -%}
    {{- form_widget(form) -}}
{%- endblock -%}

This removes all embedding containers divs for the nested form elements and appear like they are top form elements.

Still looking for a more straighforward solution if exists so i don't set this answer as the accepted one.