Skip to content

form_attr = true at form level does not work for fields rendered after form_end #48794

@Thibibi

Description

@Thibibi

Symfony version(s) affected

6.1.7

Description

Hi guys, thanks for the great work you do on Symfony! Let me show you what brings me here.

My goal is to have a button outside the <form> and </form> tags (for various reasons I would not like to discuss here), html5 allows this if controls outside the tags point to a form with their form attribute. According to the documentation, there is an option called form_attr that You can also set this to true on a root form to automatically set the "form" attribute on all its children.

Nice! I tried, and it failed. The 'form_attr' => true option was set on root form, but it does not give a form="name" attribute for children rendered after the form_end.

EditorshipType:

$builder
    ->add('entity', EntityType::class, blah blah blah)
    ->add('request', SubmitType::class, blah blah blah);          <-- Button will be outside the form tags

Controller:

$formName = 'form1';
                                  // Set the name
$form = $formFactory->createNamed($formName, EditorshipType::class, $editorshipDecision, [
    'attr' => [
        // Set the id
        'id' => $formName,
    ],
    // Command sub fields to have form = name
    'form_attr' => true,
]);

Template:

<tr>
    <td>{{ form_start(form) }}{{ form_row(form.entity) }}{{ form_end(form, {'render_rest': false}) }}</td>
    <td>{{ form_row(form.request) }}</td>
</tr>

Resulting html:

<tr>
    <td>
        <form id="form1" name="form1" method="post">
            <select id="form1_entity" name="form1[entity]" form="form1" bla bla bla>
                <option value="">Choose a seminar...</option>
                <option value="3">Blah blah blah</option>
            </select>
        </form>
    </td>
    <td>
        <button type="submit" id="form1_request" name="form1[request]">Request</button>
    </td>
</tr>

Notice my button rendered after the </form> tag does not have the form="form1" attribute. The select rendered between <form> and </form> does.

Of course, as a workaround, I can put the form attribute on the button by its field options, so it's not blocking:
EditorshipType:

$builder
    ->add('entity', EntityType::class, blah blah blah)
    ->add('request', SubmitType::class, [
        'attr' => ['form' => $builder->getName()],
    ]);

How to reproduce

See the code above, I made it easy to read.

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions