Skip to content

Commit 15e5855

Browse files
authored
Merge pull request #43 from AndyButland/feature/demo-for-checkbox-and-radio-lists
Updated demonstration web app to reproduce checkbox and radio button list validation.
2 parents 5c8a8eb + cfcbaf9 commit 15e5855

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

Pages/Index.cshtml

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,31 @@
99
</head>
1010
<body>
1111

12-
<h1 class="status-message">@Model.StatusMessage</h1>
12+
@if (Model.StatusMessage != null)
13+
{
14+
<h1 class="status-message">@Model.StatusMessage</h1>
15+
16+
<h2>Submitted Values</h2>
17+
18+
<table class="form-data">
19+
<tr>
20+
<th>Id: </th>
21+
<td>@Model.Id</td>
22+
</tr>
23+
<tr>
24+
<th>Control: </th>
25+
<td>@Model.Control</td>
26+
</tr>
27+
<tr>
28+
<th>Selected Animals: </th>
29+
<td>@(Model.SelectedAnimals != null ? string.Join(", ", Model.SelectedAnimals) : null)</td>
30+
</tr>
31+
<tr>
32+
<th>Selected Color: </th>
33+
<td>@Model.SelectedColor</td>
34+
</tr>
35+
</table>
36+
}
1337

1438
<fieldset>
1539
<legend>Form 1</legend>
@@ -27,14 +51,35 @@
2751
</div>
2852

2953
<div class="form-field">
30-
<label>
31-
<input asp-for="Color" /> Red
32-
</label>
33-
34-
<label>
35-
<input asp-for="Color" /> Blue
36-
</label>
37-
<span asp-validation-for="Color"></span>
54+
@foreach (var animal in Model.Animals)
55+
{
56+
<input name="SelectedAnimals"
57+
type="checkbox"
58+
value="@animal"
59+
data-val="true"
60+
data-val-required="Please select at least one animal"
61+
data-rule-required="true"
62+
data-msg-required="Please select at least one animal"
63+
@if (Model.SelectedAnimals != null && Model.SelectedAnimals.Contains(animal)) { <text>checked</text> } />
64+
<label>@animal</label>
65+
}
66+
<span asp-validation-for="SelectedAnimals"></span>
67+
</div>
68+
69+
<div class="form-field">
70+
@foreach (var color in Model.Colors)
71+
{
72+
<input name="SelectedColor"
73+
type="radio"
74+
value="@color"
75+
data-val="true"
76+
data-val-required="Please select a color"
77+
data-rule-required="true"
78+
data-msg-required="Please select a color"
79+
@if (Model.SelectedColor == color) { <text>checked</text> } />
80+
<label>@color</label>
81+
}
82+
<span asp-validation-for="SelectedColor"></span>
3883
</div>
3984

4085
<button type="submit" class="btn">Save</button>
@@ -102,6 +147,27 @@
102147
<script src="/dist/aspnet-validation.js"></script>
103148
<script>
104149
const service = new aspnetValidation.ValidationService(console);
150+
151+
var customRequired = function (value, element) {
152+
153+
// Handle single and multiple checkboxes/radio buttons.
154+
if (element.type.toLowerCase() === "checkbox" || element.type.toLowerCase() === "radio") {
155+
const allElementsOfThisName = element.form.querySelectorAll("input[name='" + element.name + "']");
156+
for (let i = 0; i < allElementsOfThisName.length; i++) {
157+
if (allElementsOfThisName[i].checked === true) {
158+
return true;
159+
}
160+
}
161+
162+
return false;
163+
}
164+
165+
// Use default required for all other field types.
166+
return Boolean(value);
167+
}
168+
169+
service.addProvider("required", customRequired);
170+
105171
service.bootstrap();
106172
</script>
107173
</body>

Pages/Index.cshtml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ public class IndexModel : PageModel
2121

2222
[BindProperty]
2323
[Required]
24-
public string? Color { get; set; }
24+
public List<string>? SelectedAnimals { get; set; }
25+
26+
public string[] Animals = new[] { "Dog", "Cat", "Fish" };
27+
28+
[BindProperty]
29+
[Required]
30+
public string? SelectedColor { get; set; }
31+
32+
public string[] Colors = new[] { "Red", "Green", "Blue" };
2533

2634
public IActionResult OnPost()
2735
{

wwwroot/css/site.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ body {
3131
font-size: 2em;
3232
font-weight: bold;
3333
}
34+
35+
.input-validation-error + label {
36+
color: red;
37+
}
38+
39+
.input-validation-valid + label {
40+
color: green;
41+
}
42+
43+
table.form-data {
44+
margin-bottom: 10px;
45+
}
46+
47+
table.form-data th {
48+
text-align: left;
49+
}

0 commit comments

Comments
 (0)