Skip to content

Commit

Permalink
Merge pull request #118 from haacked/haacked/117-submit-value-propaga…
Browse files Browse the repository at this point in the history
…tion-fix

Submit value propagation fix
  • Loading branch information
haacked authored Aug 8, 2024
2 parents 09628cb + c65285f commit b8b9965
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Pages/Demos/SubmitButton.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<form method="post">
<div class="form-field">
<p>
This simple test demonstrates that we ensure that submit buttons.
This simple test demonstrates that we ensure that submit button values are submitted.
</p>

<input type="submit" asp-for="SubmitButtonValue" value="Input Type Submit" />
Expand Down
33 changes: 33 additions & 0 deletions Pages/Demos/SubmitButtonDownload.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@page
@model DemoWeb.Pages.Demos.SubmitButtonDownload

@{
Layout = "Shared/_Layout";
}

<fieldset>
<legend>Required ASP.NET Checkboxes with hidden input</legend>

@DateTime.UtcNow

<form method="post">
<div class="form-field">
<p>
This simple test demonstrates that we ensure that submit button values are submitted
correctly even when the form is posting to a download (and the page is not refreshed).
</p>

Required: <input asp-for="SomeRequiredValue" />

<input type="submit" asp-for="SubmitButtonValue" value="PDF" />
<input type="submit" asp-for="SubmitButtonValue" value="Excel" />
</div>
</form>
</fieldset>

@section Scripts {
<script>
const service = new aspnetValidation.ValidationService(console);
service.bootstrap();
</script>
}
21 changes: 21 additions & 0 deletions Pages/Demos/SubmitButtonDownload.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DemoWeb.Pages.Demos;

public class SubmitButtonDownload : PageModel
{
[BindProperty]
[Required]
public string? SubmitButtonValue { get; set; }

[BindProperty]
[Required]
public string? SomeRequiredValue { get; set; }

public IActionResult OnPost()
{
return File([0], "application/octet-stream", $"DEMO-{SubmitButtonValue}.txt");
}
}
1 change: 1 addition & 0 deletions Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ul>
<li><a asp-page="Demos/Checkboxes">Checkboxes and Radio Buttons</a></li>
<li><a asp-page="Demos/SubmitButton">Submit Button</a></li>
<li><a asp-page="Demos/SubmitButtonDownload">Submit Button with Download</a></li>
<li><a asp-page="Demos/RemovedInputs">Removed Inputs</a></li>
<li><a asp-page="Demos/SelectInput">Select Input and Validation Summary</a></li>
<li><a asp-page="Demos/FormAction">Form Action</a></li>
Expand Down
7 changes: 6 additions & 1 deletion dist/aspnet-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,13 @@ var ValidationService = /** @class */ (function () {
// Because the submitter is not propagated when calling
// form.submit(), we recreate it here.
var submitter = submitEvent.submitter;
var submitterInput = null;
var initialFormAction = form.action;
if (submitter) {
var name_1 = submitter.getAttribute('name');
// If name is null, a submit button is not submitted.
if (name_1) {
var submitterInput = document.createElement('input');
submitterInput = document.createElement('input');
submitterInput.type = 'hidden';
submitterInput.name = name_1;
submitterInput.value = submitter.getAttribute('value');
Expand All @@ -619,6 +620,10 @@ var ValidationService = /** @class */ (function () {
form.submit();
}
finally {
if (submitterInput) {
// Important to clean up the submit input we created.
form.removeChild(submitterInput);
}
form.action = initialFormAction;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aspnet-client-validation",
"version": "0.10.1",
"version": "0.10.2",
"description": "Enables ASP.NET MVC client-side validation, without jQuery!",
"main": "dist/aspnet-validation.js",
"style": "dist/aspnet-validation.css",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,13 @@ export class ValidationService {
// Because the submitter is not propagated when calling
// form.submit(), we recreate it here.
const submitter = submitEvent.submitter;
let submitterInput: HTMLInputElement | null = null;
const initialFormAction = form.action;
if (submitter) {
const name = submitter.getAttribute('name');
// If name is null, a submit button is not submitted.
if (name) {
const submitterInput = document.createElement('input');
submitterInput = document.createElement('input');
submitterInput.type = 'hidden';
submitterInput.name = name;
submitterInput.value = submitter.getAttribute('value');
Expand All @@ -825,6 +826,10 @@ export class ValidationService {
try {
form.submit();
} finally {
if (submitterInput) {
// Important to clean up the submit input we created.
form.removeChild(submitterInput);
}
form.action = initialFormAction;
}
}
Expand Down

0 comments on commit b8b9965

Please sign in to comment.