[Form] Add "form_attr" FormType option #40430
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is this about
This PR add support for
form
attribute to Symfony Form (browser compatibility).The
form
attribute allows form elements to override their associated form (which is their nearest ancestor form element by default). This is extremely useful to solve nested form problem and allows form children to be rendered outside form tag while still working as expected.New "form_attr" FormType option
form_attr
type:
bool
orstring
default:false
If set to
true
on a root form, addsform
attribute on every children with their root form id.This allows you to render form children outside the form tag and avoid nested form problem in some situations while keeping the form working properly.
If set to
true
on a child, addsform
attribute on it with its root form id.This allows you to render that child outside the form tag and avoid nested form problem in some situations while keeping the form working properly.
If root form has no
id
(this may happen by create an unnamed form), you can set it to astring
identifier to be used atFormView
level to link children and root form anyway.Usage on Root Form Example
Form Type
Enable the feature by setting
form_attr
totrue
on the root form.Twig
The following Twig template works properly even if form children are outside their form tag (browser compatibility).
✅ Every form elements work properly even outside form tag.
Usage on Form Child Example
Enable the feature by setting
form_attr
totrue
on selected child.Twig
The following Twig template works properly even if
form.perPage
is outside form tag (browser compatibility).✅
form.perPage
element work properly even outside form tag.