|
| 1 | +@using System.ComponentModel.DataAnnotations |
| 2 | + |
| 3 | +<h3>Validation</h3> |
| 4 | + |
| 5 | +<p>InputList supports standard validation through EditContext and applies validation styles to the overall input container.</p> |
| 6 | + |
| 7 | +<EditForm Model="Profile" OnValidSubmit="HandleValidSubmit"> |
| 8 | + <DataAnnotationsValidator /> |
| 9 | + <ValidationSummary /> |
| 10 | + |
| 11 | + <div class="mb-3"> |
| 12 | + <label for="Aliases">Aliases *</label> |
| 13 | + <InputList id="Aliases" |
| 14 | + @bind-Value="Profile.Aliases" |
| 15 | + InputClass="form-control" |
| 16 | + AddClass="btn btn-outline-primary" |
| 17 | + RemoveClass="btn btn-outline-danger" /> |
| 18 | + <ValidationMessage For="@(() => Profile.Aliases)" /> |
| 19 | + </div> |
| 20 | + |
| 21 | + <button type="submit" class="btn btn-primary">Submit</button> |
| 22 | +</EditForm> |
| 23 | + |
| 24 | +@if (Submitted) |
| 25 | +{ |
| 26 | + <p class="mt-3">Form submitted successfully.</p> |
| 27 | +} |
| 28 | + |
| 29 | +<pre><code class="language-markup"><EditForm Model="Profile" OnValidSubmit="HandleValidSubmit"> |
| 30 | + <DataAnnotationsValidator /> |
| 31 | + <InputList @@bind-Value="Profile.Aliases" /> |
| 32 | + <ValidationMessage For="@@(() => Profile.Aliases)" /> |
| 33 | +</EditForm></code></pre> |
| 34 | + |
| 35 | +@code { |
| 36 | + public AliasProfile Profile { get; set; } = new(); |
| 37 | + |
| 38 | + public bool Submitted { get; set; } |
| 39 | + |
| 40 | + private void HandleValidSubmit() |
| 41 | + => Submitted = true; |
| 42 | + |
| 43 | + public class AliasProfile |
| 44 | + { |
| 45 | + [Required] |
| 46 | + [MinLength(1, ErrorMessage = "At least one alias is required.")] |
| 47 | + public IList<string> Aliases { get; set; } = new List<string>(); |
| 48 | + } |
| 49 | +} |
0 commit comments