-
-
Notifications
You must be signed in to change notification settings - Fork 115
Support form submission from buttons and inputs outside form using form attribute #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
644007c
fc32443
ff0de7b
8c440c2
150e5df
32d7373
36a5d18
29cbd3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,10 +176,63 @@ private static bool TryGetParentFormElementSpecialCase( | |
| IHtmlButtonElement { Type: "submit", Form: not null } button => button.Form, | ||
| _ => null | ||
| }; | ||
|
|
||
| // If form is still null, try to find it by the form attribute for submit buttons/inputs | ||
| if (form is null && element.HasAttribute("form")) | ||
| { | ||
| var isSubmitElement = element switch | ||
| { | ||
| IHtmlInputElement { Type: "submit" } => true, | ||
| IHtmlButtonElement { Type: "submit" } => true, | ||
| _ => false | ||
| }; | ||
|
|
||
| if (isSubmitElement) | ||
| { | ||
| var formId = element.GetAttribute("form"); | ||
| if (!string.IsNullOrEmpty(formId)) | ||
| { | ||
| // Try to find the form element by traversing up to find a common ancestor | ||
| // and then searching down for the form | ||
| form = FindFormById(element, formId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return form is not null | ||
| && form.TryGetEventId(Htmlizer.ToBlazorAttribute("onsubmit"), out eventId); | ||
| } | ||
|
|
||
| private static IHtmlFormElement? FindFormById(IElement element, string formId) | ||
| { | ||
| // First try the owner's GetElementById (most efficient if it works) | ||
| var formByOwner = element.Owner?.GetElementById(formId) as IHtmlFormElement; | ||
| if (formByOwner is not null) | ||
| { | ||
| return formByOwner; | ||
| } | ||
|
|
||
| // If GetElementById didn't work (which can happen with Blazor's incremental DOM rendering), | ||
| // traverse up the DOM tree to find a common ancestor and search its children | ||
| // This handles cases where the button and form are siblings or in different subtrees | ||
| var current = element.Parent as IElement; | ||
| while (current is not null) | ||
| { | ||
| // Search children of current element for the form with matching ID | ||
| foreach (var child in current.Children) | ||
| { | ||
| if ((child.Id == formId || child.GetAttribute("id") == formId) && child is IHtmlFormElement htmlForm) | ||
| { | ||
| return htmlForm; | ||
| } | ||
| } | ||
|
|
||
| // Move up to parent to widen the search | ||
| current = current.Parent as IElement; | ||
| } | ||
|
||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static bool EventIsDisabled(this IElement element, string eventName) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the HTML standard allow for an tag to be used in this scenario? If so, also add a test case for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the HTML5 standard allows
<input type="submit">with theformattribute. Added test cases in commit 36a5d18 for both button and input elements outside forms using the form attribute.