Linter: Implement html-no-nested-forms rule - #1961
Open
irinanazarova wants to merge 1 commit into
Open
Conversation
marcoroth
reviewed
Aug 2, 2026
Owner
There was a problem hiding this comment.
Thanks @irinanazarova, this looks great!
Just a few nits and this is good to go, thank you!
|
|
||
| if (this.formDepth > 0) { | ||
| this.addOffense( | ||
| "Nested `<form>` elements are not allowed. Move the inner `<form>` outside of the enclosing form, or associate its controls using the `form` attribute.", |
Owner
There was a problem hiding this comment.
Suggested change
| "Nested `<form>` elements are not allowed. Move the inner `<form>` outside of the enclosing form, or associate its controls using the `form` attribute.", | |
| "Nested `<form>` elements are not allowed. Move the inner `<form>` outside of the enclosing `<form>`, or associate its controls using the `form` attribute.", |
| if (this.formDepth === 0) return | ||
|
|
||
| this.addOffense( | ||
| `\`${helperName}\` renders its own \`<form>\` element and cannot be nested inside another form. Move it outside of the enclosing form.`, |
Owner
There was a problem hiding this comment.
Suggested change
| `\`${helperName}\` renders its own \`<form>\` element and cannot be nested inside another form. Move it outside of the enclosing form.`, | |
| `\`${helperName}\` renders its own \`<form>\` element and cannot be nested inside another \`<form>\`. Move it outside of the enclosing \`<form>\`.`, |
| this.formDepth-- | ||
| } | ||
|
|
||
| visitERBBlockNode(node: ERBBlockNode): void { |
Owner
There was a problem hiding this comment.
Suggested change
| visitERBBlockNode(node: ERBBlockNode): void { | |
| // TODO: remove once we support transforming all the Action View form helpers | |
| visitERBBlockNode(node: ERBBlockNode): void { |
| this.formDepth-- | ||
| } | ||
|
|
||
| visitERBContentNode(node: ERBContentNode): void { |
Owner
There was a problem hiding this comment.
Suggested change
| visitERBContentNode(node: ERBContentNode): void { | |
| // TODO: remove once we support transforming all the Action View form helpers | |
| visitERBContentNode(node: ERBContentNode): void { |
| assertOffenses(dedent` | ||
| <form><div><form></form></div></form> | ||
| `) | ||
| }) |
Owner
There was a problem hiding this comment.
lets also add tests for form_tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request implements a new
html-no-nested-formsrule that disallows placing one<form>element inside another<form>element, including forms rendered by Action View helpers.HTML does not support nested forms: browsers silently drop or re-parent the inner form, leading to broken submissions and buttons that submit the wrong form. The helper case is particularly easy to miss because the nested
<form>never appears in the template. In the example above, the form rendered bybutton_tois dropped during parsing and clicking "Delete" submits the outer form instead, performing an update.The rule detects literal
<form>elements with a depth counter (same approach ashtml-no-nested-links) and form-rendering helpers via Prism (same approach asactionview-no-silent-helper). The helper list comes from the Action View helper registry viagetHelpersForTag("form"), filtered to helpers the parser does not transform yet: once a form helper becomessupported, the element branch catches its synthesized<form>element and the Prism branch stops covering it automatically.button_tois included per the discussion in #166, since it is effectively a hidden form. The offense is reported on the inner form's tag name, or on the helper call for helper-rendered forms.Nesting composed across partials (an outer form in one template, an inner form in a rendered partial) is not detectable with per-file linting.
tag.formandcontent_tag(:form)are not detected yet; the registry'sdetectStylemetadata encodes how to find these. This could be a follow-up.Resolves #166