Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issues with field order on form rendering #19

Open
barbieswimcrew opened this issue Feb 25, 2024 · 3 comments
Open

Issues with field order on form rendering #19

barbieswimcrew opened this issue Feb 25, 2024 · 3 comments

Comments

@barbieswimcrew
Copy link

Hey guys! I just realized that the order of the fields is not respected when rendering the form with

{{ form_start(form) }}
{{ form_rest(form) }}
{{ form_end(form) }}

The dependent fields are packed at the end instead of at the position where the addDependent method is located in the fluent interface of the form builder.

So for example:

$builder
    // ...
    ->add('fieldOne')
    ->addDependent('depFieldOne', ['fieldOne'], // ...)
    ->add('fieldTwo')
    ->addDependent('depFieldTwo', ['fieldTwo'], // ...)
    // ...

The rendered field order will be:

fieldOne
fieldTwo
depFieldOne
depFieldTwo

Is there a way to get the dependent fields in the correct order? I would expect the order:

fieldOne
depFieldOne
fieldTwo
depFieldTwo

Of course, I could render them in manual order by using {{ form_row(form.<fieldName>) }} but it would be just great if the correct order as defined in the builder would work out of the box ❤️

@leevigraham
Copy link

leevigraham commented Apr 19, 2024

Symfony 5.3 added priority: https://symfony.com/blog/new-in-symfony-5-3-form-field-sorting which could work here.

https://symfony.com/doc/current/reference/forms/types/form.html#priority

My guess is that the dependent fields are being added in POST_SUBMIT which adds them to the end of the form.

@bocharsky-bw
Copy link
Member

Good catch! @barbieswimcrew , could you give the form fields priority feature a try to see if that solves your issue?

@barbieswimcrew
Copy link
Author

@leevigraham @bocharsky-bw sorry for my late reply... I tried priority now and it seems to work. Thanks for that hint! But... to be honest... I think it would still be much more fancy to use if the order of the form fields would follow the order the fields get defined in the form builder.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants