Skip to content

Commit cb649a9

Browse files
committed
Add EditForm validation sample
1 parent 005956f commit cb649a9

File tree

6 files changed

+128
-36
lines changed

6 files changed

+128
-36
lines changed

src/Components/Samples/BlazorUnitedApp/BlazorUnitedApp.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
55
<IsShippingPackage>false</IsShippingPackage>
66
<Nullable>enable</Nullable>
7+
8+
<InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.AspNetCore.Http.Validation.Generated</InterceptorsNamespaces>
9+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
710
</PropertyGroup>
811

912
<ItemGroup>
1013
<ProjectReference Include="..\BlazorUnitedApp.Client\BlazorUnitedApp.Client.csproj" />
1114
</ItemGroup>
1215

16+
<ItemGroup>
17+
<ProjectReference Include="$(RepoRoot)/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Microsoft.AspNetCore.Http.ValidationsGenerator.csproj"
18+
OutputItemType="Analyzer"
19+
ReferenceOutputAssembly="false" />
20+
</ItemGroup>
21+
1322
<ItemGroup>
1423
<Reference Include="Microsoft.AspNetCore" />
1524
<Reference Include="Microsoft.AspNetCore.Components.Endpoints" />

src/Components/Samples/BlazorUnitedApp/Pages/Index.razor

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,4 @@
22
@using BlazorUnitedApp.Data;
33
<PageTitle>Index</PageTitle>
44

5-
<EditForm Model="Value" method="POST" OnSubmit="DisplayCustomer" FormName="customer">
6-
<div>
7-
<label>
8-
<span>Name</span>
9-
<InputText @bind-Value="Value!.Name" />
10-
</label>
11-
</div>
12-
<AddressEditor @bind-Value="Value.BillingAddress" />
13-
<input type="submit" value="Send" />
14-
</EditForm>
15-
16-
@if (_submitted)
17-
{
18-
<!-- Display customer data -->
19-
<h3>Customer</h3>
20-
<p>Name: @Value!.Name</p>
21-
<p>Street: @Value.BillingAddress.Street</p>
22-
<p>City: @Value.BillingAddress.City</p>
23-
<p>State: @Value.BillingAddress.State</p>
24-
<p>Zip: @Value.BillingAddress.Zip</p>
25-
}
26-
27-
@code {
28-
29-
public void DisplayCustomer()
30-
{
31-
_submitted = true;
32-
}
33-
34-
[SupplyParameterFromForm] Customer? Value { get; set; }
35-
36-
protected override void OnInitialized() => Value ??= new();
37-
38-
bool _submitted = false;
39-
public void Submit() => _submitted = true;
40-
}
5+
<h1>Welcome to Blazor</h1>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
@page "/validated-form"
2+
@rendermode InteractiveServer
3+
4+
@using System.ComponentModel.DataAnnotations
5+
@using System.Text.Json
6+
@using BlazorUnitedApp.Validation
7+
8+
<PageTitle>Validated Form</PageTitle>
9+
10+
<h1>Validated Form</h1>
11+
12+
<EditForm Model="@Model" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
13+
<div class="alert @StatusClass">@StatusMessage</div>
14+
15+
<DataAnnotationsValidator />
16+
<ValidationSummary />
17+
18+
<div class="form-group">
19+
<label for="name">Author.Name: </label>
20+
<InputText Id="name" Class="form-control" @bind-Value="@Model.Author.Name"></InputText>
21+
<ValidationMessage For="@(() => Model.Author.Name)" />
22+
</div>
23+
<div class="form-group">
24+
<label for="name">Author.Email: </label>
25+
<InputText Id="name" Class="form-control" @bind-Value="@Model.Author.Email"></InputText>
26+
<ValidationMessage For="@(() => Model.Author.Email)" />
27+
</div>
28+
<div class="form-group">
29+
<label for="age">Author.Age: </label>
30+
<InputNumber Id="age" Class="form-control" @bind-Value="@Model.Author.Age"></InputNumber>
31+
<ValidationMessage For="@(() => Model.Author.Age)" />
32+
</div>
33+
<div class="form-group">
34+
<label for="city">Author.Address.City: </label>
35+
<InputText Id="city" Class="form-control" @bind-Value="@Model.Author.Address.City"></InputText>
36+
<ValidationMessage For="@(() => Model.Author.Address.City)" />
37+
</div>
38+
<div class="form-group">
39+
<label for="street">Author.Address.Street: </label>
40+
<InputText Id="street" Class="form-control" @bind-Value="@Model.Author.Address.Street"></InputText>
41+
<ValidationMessage For="@(() => Model.Author.Address.Street)" />
42+
</div>
43+
<div class="form-group">
44+
<label for="body">Text: </label>
45+
<InputTextArea Id="body" Class="form-control" @bind-Value="@Model.Text"></InputTextArea>
46+
<ValidationMessage For="@(() => Model.Text)" />
47+
</div>
48+
<button type="submit">Ok</button>
49+
50+
</EditForm>
51+
52+
@code
53+
{
54+
private string? StatusMessage;
55+
private string? StatusClass;
56+
57+
private GuestbookEntry Model = new GuestbookEntry();
58+
59+
protected void HandleValidSubmit()
60+
{
61+
StatusClass = "alert-info";
62+
StatusMessage = DateTime.Now + " Handle valid submit";
63+
}
64+
65+
protected void HandleInvalidSubmit()
66+
{
67+
StatusClass = "alert-danger";
68+
StatusMessage = DateTime.Now + " Handle invalid submit";
69+
}
70+
}

src/Components/Samples/BlazorUnitedApp/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
builder.Services.AddSingleton<WeatherForecastService>();
1515

16+
builder.Services.AddValidation();
17+
1618
var app = builder.Build();
1719

1820
// Configure the HTTP request pipeline.

src/Components/Samples/BlazorUnitedApp/Shared/NavMenu.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<span class="bi bi-house-door-fill" aria-hidden="true"></span> Home
1515
</NavLink>
1616
</div>
17+
<div class="nav-item px-3">
18+
<NavLink class="nav-link" href="validated-form">
19+
<span class="bi bi-plus-square-fill" aria-hidden="true"></span> Validated form
20+
</NavLink>
21+
</div>
1722
<div class="nav-item px-3">
1823
<NavLink class="nav-link" href="counter">
1924
<span class="bi bi-plus-square-fill" aria-hidden="true"></span> Counter
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.ComponentModel.DataAnnotations;
5+
using Microsoft.AspNetCore.Http.Validation;
6+
7+
namespace BlazorUnitedApp.Validation;
8+
9+
#pragma warning disable ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
10+
[ValidatableType]
11+
#pragma warning restore ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
12+
public class GuestbookEntry
13+
{
14+
[Required]
15+
public Author Author { get; set; } = new Author();
16+
17+
[Required]
18+
public string? Text { get; set; }
19+
}
20+
21+
public class Author
22+
{
23+
[Required]
24+
public string? Name { get; set; }
25+
26+
[EmailAddress]
27+
public string? Email { get; set; }
28+
29+
public Address Address { get; set; } = new Address();
30+
31+
[Range(0, 150)]
32+
public int? Age { get; set; }
33+
}
34+
35+
public class Address
36+
{
37+
[Required]
38+
public string? City { get; set; }
39+
40+
public string? Street { get; set; }
41+
}

0 commit comments

Comments
 (0)