Skip to content
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

Formaction on a button fix #92

Merged
merged 5 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Pages/Demos/FormAction.cshtml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
@page
@model DemoWeb.Pages.Demos.FormAction


@{
Layout = "Shared/_Layout";
}

<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/>

<form method="post">
<div asp-validation-summary="All">The following problems occurred when submitting the form:</div>
<div>
<label asp-for="Input.Name"></label>
<input asp-for="Input.Name" />
<span asp-validation-for="Input.Name" ></span>
</div>
<input type="submit" value="Submit" asp-page-handler="Submit" />
<input type="submit" value="Save" asp-page-handler="Save"/>
<input type="submit" />
</form>
17 changes: 17 additions & 0 deletions Pages/Demos/FormAction.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace DemoWeb.Pages.Demos;

Expand All @@ -8,6 +9,15 @@ public class FormAction : PageModel
[TempData]
public string? StatusMessage { get; set; }

[BindProperty]
public InputModel Input { get; set; } = new();

public class InputModel
{
[Display(Name = "Name")]
[Required, StringLength(50)]
public string? Name { get; set; }
}

public IActionResult OnPostSubmitAsync()
{
Expand All @@ -22,4 +32,11 @@ public IActionResult OnPostSave()

return RedirectToPage();
}

public IActionResult OnPost()
{
StatusMessage = "The button with no formaction was clicked";

return RedirectToPage();
}
}
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ export class ValidationService {
// Because the submitter is not propagated when calling
// form.submit(), we recreate it here.
const submitter = submitEvent.submitter;
const initialFormAction = form.action;
if (submitter) {
const name = submitter.getAttribute('name');
// If name is null, a submit button is not submitted.
Expand All @@ -806,9 +807,18 @@ export class ValidationService {
submitterInput.value = submitter.getAttribute('value');
form.appendChild(submitterInput)
}

const formAction = submitter.getAttribute('formaction');
if (formAction) {
form.action = formAction;
}
}

form.submit();
try {
form.submit();
} finally {
form.action = initialFormAction;
}
}
}

Expand All @@ -825,7 +835,7 @@ export class ValidationService {
let invalidFormInputUIDs = formInputUIDs.filter(uid => this.summary[uid]);

if (invalidFormInputUIDs.length > 0) {
var firstInvalid = this.elementByUID[invalidFormInputUIDs[0]] as HTMLElement;
const firstInvalid = this.elementByUID[invalidFormInputUIDs[0]] as HTMLElement;
if (firstInvalid) {
firstInvalid.focus();
}
Expand Down