|
9 | 9 | </head>
|
10 | 10 | <body>
|
11 | 11 |
|
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 | +} |
13 | 37 |
|
14 | 38 | <fieldset>
|
15 | 39 | <legend>Form 1</legend>
|
|
27 | 51 | </div>
|
28 | 52 |
|
29 | 53 | <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> |
38 | 83 | </div>
|
39 | 84 |
|
40 | 85 | <button type="submit" class="btn">Save</button>
|
|
102 | 147 | <script src="/dist/aspnet-validation.js"></script>
|
103 | 148 | <script>
|
104 | 149 | 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 | +
|
105 | 171 | service.bootstrap();
|
106 | 172 | </script>
|
107 | 173 | </body>
|
|
0 commit comments