-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from haacked/haacked/48-required-validation-on…
…-optional-checkboxes Fix validation for checkboxes rendered with accompanying hidden input.
- Loading branch information
Showing
11 changed files
with
168 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
@page | ||
@model DemoWeb.Pages.Demos.Checkboxes | ||
|
||
@{ | ||
Layout = "Shared/_Layout"; | ||
} | ||
|
||
<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/> | ||
|
||
<fieldset> | ||
<legend>Required ASP.NET Checkboxes with hidden input</legend> | ||
|
||
<form method="post"> | ||
<div class="form-field"> | ||
<p> | ||
ASP.NET renders a checkbox and a hidden input for each boolean property. | ||
The hidden input is used to ensure that a value is sent for the checkbox, | ||
even if it is unchecked. Unchecking it should not fail validation. | ||
</p> | ||
<label>Is checked | ||
<input asp-for="Input.IsChecked"/> | ||
</label> | ||
<span asp-validation-for="Input.IsChecked"></span> | ||
@if (Model.Input.IsChecked) { | ||
<em class="results">This is checked.</em> | ||
} else { | ||
<em class="results">This is not checked</em> | ||
} | ||
</div> | ||
|
||
<div class="form-field"> | ||
<p> | ||
However, if you manually render a checkbox, the checkbox is only submitted | ||
when checked. So this allows cases where we require at least one checkbox | ||
is selected. | ||
</p> | ||
@foreach (var animal in Model.Animals) { | ||
<input name="SelectedAnimals" | ||
type="checkbox" | ||
value="@animal" | ||
data-val="true" | ||
data-val-required="Please select at least one animal" | ||
data-rule-required="true" | ||
data-msg-required="Please select at least one animal" | ||
@if (Model.SelectedAnimals != null && Model.SelectedAnimals.Contains(animal)) { | ||
<text>checked</text> | ||
}/> | ||
<label>@animal</label> | ||
} | ||
<span asp-validation-for="SelectedAnimals"></span> | ||
@if (Model.SelectedAnimals.Any()) { | ||
<em class="results">Selected animals: @string.Join(", ", Model.SelectedAnimals)</em> | ||
} | ||
</div> | ||
|
||
<input type="submit" value="Submit"/> | ||
</form> | ||
</fieldset> | ||
|
||
@if (Model.StatusMessage is not null) { | ||
<a href="">Reset</a> | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace DemoWeb.Pages.Demos; | ||
|
||
public class Checkboxes : PageModel | ||
{ | ||
public string? StatusMessage { get; set; } | ||
|
||
public IActionResult OnPost() | ||
{ | ||
StatusMessage = "Form was submitted: " + (ModelState.IsValid | ||
? "Model is valid" | ||
: "Model is invalid"); | ||
|
||
return Page(); | ||
} | ||
|
||
[BindProperty] | ||
public InputModel Input { get; set; } = new(); | ||
|
||
[BindProperty] | ||
[Required] | ||
public List<string> SelectedAnimals { get; set; } = new(); | ||
|
||
public IReadOnlyList<string> Animals = new List<string> { "Dog", "Cat", "Fish" }; | ||
|
||
public class InputModel | ||
{ | ||
public bool IsChecked { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>@ViewData["Title"]</title> | ||
<title>ASP.NET Client Validation Demo Page</title> | ||
<link rel="stylesheet" href="/css/site.css"> | ||
<link rel="stylesheet" href="/dist/aspnet-validation.css" /></head> | ||
<body> | ||
|
||
@RenderBody() | ||
|
||
<script src="/dist/aspnet-validation.js"></script> | ||
<script> | ||
const service = new aspnetValidation.ValidationService(console); | ||
service.bootstrap(); | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@model string? | ||
|
||
@if (Model is { Length: > 0 } statusMessage) { | ||
<h1 class="status-message"> | ||
@statusMessage | ||
</h1> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,9 @@ table.form-data { | |
|
||
table.form-data th { | ||
text-align: left; | ||
} | ||
|
||
.results { | ||
margin-left: 4px; | ||
color: cadetblue; | ||
} |