Skip to content

Commit

Permalink
Merge pull request #92 from haacked/haacked/formaction-fix
Browse files Browse the repository at this point in the history
Formaction on a button fix
  • Loading branch information
haacked authored Jan 6, 2024
2 parents 45565be + 95b6ad1 commit 2c76d35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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

0 comments on commit 2c76d35

Please sign in to comment.