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

Propagate submit value #52

Merged
merged 3 commits into from
Jul 3, 2023
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
44 changes: 44 additions & 0 deletions Pages/Demos/SubmitButton.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@page
@model DemoWeb.Pages.Demos.SubmitButton

@{
Layout = "Shared/_Layout";
}

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

<form method="post">
<div class="form-field">
<p>
This simple test demonstrates that we ensure that submit buttons.
</p>

<input type="submit" asp-for="SubmitButtonValue" value="Input Type Submit" />
<input type="submit" value="Input Type Submit No Name" />
<input type="submit" name="SubmitButtonValue" />
<button type="submit" name="SubmitButtonValue" value="Submit That!">Button with name and value</button>

<button type="submit" value="Submit That!">Button no name</button>
<button type="submit" name="SubmitButtonValue">Button with name, no value</button>

@if (Model.SubmitButtonValue is not null) {
<span class="results">
The submitted button value is "@Model.SubmitButtonValue".
</span>

<p>The submitted form collection:</p>
<table class="form-data">
@foreach (var item in Request.Form) {
<tr>
<th>@item.Key</th>
<td>@item.Value</td>
</tr>
}
</table>
}


</div>
</form>
</fieldset>
17 changes: 17 additions & 0 deletions Pages/Demos/SubmitButton.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DemoWeb.Pages.Demos;

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

public void OnPost()
{
SubmitButtonValue ??= "Form submitted, but button value not submitted";
}
}
7 changes: 6 additions & 1 deletion Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<h1>Other Demos</h1>
<ul>
<li><a asp-page="Demos/Checkboxes">Checkboxes</a></li>
<li><a asp-page="Demos/SubmitButton">Submit Button</a></li>
</ul>

@if (Model.StatusMessage != null) {
Expand All @@ -27,6 +28,10 @@
<th>Selected Color: </th>
<td>@Model.SelectedColor</td>
</tr>
<tr>
<th>Button Value: </th>
<td>@Model.SubmitButton</td>
</tr>
</table>
}

Expand Down Expand Up @@ -68,7 +73,7 @@
<span asp-validation-for="SelectedColor"></span>
</div>

<button type="submit" class="btn">Save</button>
<input type="submit" asp-for="SubmitButton" class="btn" value="Save" />
</form>
</fieldset>

Expand Down
3 changes: 3 additions & 0 deletions Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class IndexModel : PageModel
[Required]
public string? SelectedColor { get; set; }

[BindProperty]
public string? SubmitButton { get; set; }

public string[] Colors = new[] { "Red", "Green", "Blue" };

public IActionResult OnPost()
Expand Down
14 changes: 14 additions & 0 deletions dist/aspnet-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ var ValidationService = /** @class */ (function () {
this.submitValidForm = function (form, submitEvent) {
var newEvent = new SubmitEvent('submit', submitEvent);
if (form.dispatchEvent(newEvent)) {
// Because the submitter is not propagated when calling
// form.submit(), we recreate it here.
var submitter = submitEvent.submitter;
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.type = 'hidden';
submitterInput.name = name_1;
submitterInput.value = submitter.getAttribute('value');
form.appendChild(submitterInput);
}
}
form.submit();
}
};
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.

15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,21 @@ export class ValidationService {
submitValidForm = (form: HTMLFormElement, submitEvent: SubmitEvent) => {
const newEvent = new SubmitEvent('submit', submitEvent);
if (form.dispatchEvent(newEvent)) {
// Because the submitter is not propagated when calling
// form.submit(), we recreate it here.
const submitter = submitEvent.submitter;
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.type = 'hidden';
submitterInput.name = name;
submitterInput.value = submitter.getAttribute('value');
form.appendChild(submitterInput)
}
}

form.submit();
}
}
Expand Down