Skip to content

Commit 4d2a39a

Browse files
committed
Add broken sample (WIP) trying to figure out validation with normal form
1 parent a2ac3a7 commit 4d2a39a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@page "/"
2+
@using System.ComponentModel.DataAnnotations;
3+
4+
<h1>Form Validation using DataAnnotationsValidator and EditForm</h1>
5+
6+
<div class="container">
7+
@((MarkupString)_showMessage)
8+
9+
<br />
10+
<div class="row">
11+
<div class="col-md-6">
12+
<CascadingValues EditContext>
13+
<form method="POST" @formname="info" @onsubmit="HandleSubmit">
14+
<DataAnnotationsValidator />
15+
<div class="mb-3">
16+
<label class="form-label" for="PersonInfo.Name">Please enter your name</label><br />
17+
<InputText @bind-Value="PersonInfo.Name" class="form-control" />
18+
<ValidationMessage For="() => PersonInfo.Name" class="text-danger" />
19+
</div>
20+
<div class="mb-3">
21+
<label for="PersonInfo.Description" class="form-label">Describe yourself</label>
22+
<InputTextArea @bind-Value="PersonInfo.Description" class="form-control" />
23+
<ValidationMessage For="() => PersonInfo.Description" class="text-danger" />
24+
</div>
25+
<div class="mb-3">
26+
<label for="PersonInfo.Hobby" class="form-label">Hobby</label>
27+
<InputSelect @bind-Value="PersonInfo.Hobby" class="form-select">
28+
<option>Football</option>
29+
<option>Basketball</option>
30+
<option>Tennis</option>
31+
</InputSelect>
32+
</div>
33+
<div class="mb-3 form-check">
34+
<InputCheckbox @bind-Value="PersonInfo.IsMarried" class="form-check-input" />
35+
<label for="PersonInfo.IsMarried" class="form-check-label">Is married?</label>
36+
</div>
37+
<div class="mb-3 form-check">
38+
@{ var idx = 0;}
39+
<InputRadioGroup @bind-Value="PersonInfo.Nationality">
40+
@foreach(var val in new string[] { "USA", "Indonesia", "Egypt"})
41+
{
42+
var id = "nationality" + idx;
43+
<InputRadio class="formcheck-input" Value="@val" id="@id"/>
44+
<label class="form-check-label" for="@id">@val</label>
45+
<br/>
46+
idx++;
47+
}
48+
</InputRadioGroup>
49+
<ValidationMessage For="() => PersonInfo.Nationality" class="text-danger" />
50+
</div>
51+
<div class="mb-3">
52+
<button type="submit" class="btn btn-primary">Submit</button>
53+
</div>
54+
</form>
55+
</EditContext>
56+
</div>
57+
</div>
58+
</div>
59+
60+
@code
61+
{
62+
[SupplyParameterFromForm(Handler="info")]// matches @fornname"info"
63+
public Person PersonInfo { get; set; } = new();
64+
65+
string _showMessage = string.Empty;
66+
67+
void HandleSubmit()
68+
{
69+
_showMessage = $"Name: {PersonInfo.Name}<br />Description: {PersonInfo.Description}<br />Hobby: {PersonInfo.Hobby}<br />Is married: {PersonInfo.IsMarried}<br />Nationality: {PersonInfo.Nationality}<br/><br/>";
70+
}
71+
72+
public class Person
73+
{
74+
[Required]
75+
public string Name { get; set; } = string.Empty;
76+
77+
[Required]
78+
public string Description { get; set; } = string.Empty;
79+
80+
public string Hobby { get; set; } = string.Empty;
81+
82+
public bool IsMarried { get; set; }
83+
84+
[Required]
85+
public string Nationality { get; set; } = string.Empty;
86+
}
87+
}

0 commit comments

Comments
 (0)