当前位置:网站首页>Blazor University (31) form - Validation
Blazor University (31) form - Validation
2022-06-22 03:47:00 【Dotnet cross platform】
Link to the original text :https://blazor-university.com/forms/validation/
verification
Source code [1]
DataAnnotationsValidator yes Blazor Standard validator types in . stay EditForm Adding this component to the component will enable based on System.ComponentModel.DataAnnotations.ValidationAttribute Of .NET Form validation for properties .
First , We will create a short example , Then we will learn what is happening behind the scenes . First , Create a model that we can edit , And decorate its properties with some data annotations for verification .
public class Person
{
[Required]
public string Name { get; set; }
[Range(18, 80, ErrorMessage = "Age must be between 18 and 80.")]
public int Age { get; set; }
}The first 3 Please specify
NameAttribute cannot be null Or empty .The first 5 Behavior
AgeProperty specifies a valid range of values ( from 18 To 80), An appropriate error message is also provided to display to the user .
Add validation
By default Blazor Edit in application Index.razor page , And provide some tags to edit Person Example .
<EditForm [email protected]>
<div class="form-group">
<label for="Name">Name</label>
<InputText @bind-Value=Person.Name class="form-control" id="Name" />
</div>
<div class="form-group">
<label for="Age">Age</label>
<InputNumber @bind-Value=Person.Age class="form-control" id="Age" />
</div>
<input type="submit" class="btn btn-primary" value="Save"/>
</EditForm>
@code {
Person Person = new Person();
} Running the application now will cause the user to be presented with a form that does not validate their input . To ensure that the form is validated , We must specify an authentication mechanism . stay EditForm Add a... In the component DataAnnotationsValidator Components .
<EditForm [email protected]>
<DataAnnotationsValidator/>
<div class="form-group">
<label for="Name">Name</label>
<InputText @bind-Value=Person.Name class="form-control" id="Name" />
</div>
<div class="form-group">
<label for="Age">Age</label>
<InputNumber @bind-Value=Person.Age class="form-control" id="Age" />
</div>
<input type="submit" class="btn btn-primary" value="Save"/>
</EditForm>Run the application and click “ preservation ” Button will update the user interface , To provide a visual indication of errors in user input .

The validation error message is displayed
There are two ways to display authentication error messages to users . We can add a ValidationSummary To display a complete list of all errors in the form . We can also use ValidationMessage Component to display error messages for specific inputs on the form . These components are not mutually exclusive , So you can use them at the same time .
ValidationSummary Components can simply be placed in our markup EditForm in ; No additional parameters are required at all .

because ValidationMessage Component displays an error message for a single field , It requires us to specify the identification of the field . To ensure that our parameter values remain correct after refactoring ( for example , When we refactor Person Class )Blazor We are required to specify an expression when identifying fields . be known as For Parameters in ValidationMessage As defined below :
[Parameter]
public Expression<Func<T>> For { get; set; } This means that we should use lambda Expression to specify the identification of the field , This expression can “ Double quotes ” Or wrapped in @(...)
Double quotes
<ValidationMessage For="() => Person.Name"/>Razor expression
<ValidationMessage [email protected]( () => Person.Name )/>
The two forms are equivalent . The quoted form is easier to read , and razor Expressions make it clear to other developers that we are defining expressions rather than strings .
@page "/"
@using Models
<EditForm [email protected]>
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="form-group">
<label for="Name">Name</label>
<InputText @bind-Value=Person.Name class="form-control" id="Name" />
<ValidationMessage For="() => Person.Name"/>
</div>
<div class="form-group">
<label for="Age">Age</label>
<InputNumber @bind-Value=Person.Age class="form-control" id="Age" />
<ValidationMessage [email protected](() => Person.Age) />
</div>
<input type="submit" class="btn btn-primary" value="Save"/>
</EditForm>
@code {
Person Person = new Person();
}
Reference material
[1]
Source code : https://github.com/mrpmorris/blazor-university/tree/master/src/Forms/ValidatingUserInput
边栏推荐
- SSM住院管理系统
- Analyzing iceberg merge tasks to resolve data conflicts
- 内网穿透
- Float floating point number understanding
- docker 安装redis
- [网鼎杯 2018]Fakebook1 参考
- Use of shutter stream
- 【牛客刷题-SQL大厂面试真题】NO1.某音短视频
- [leetcode] 17 backtracking (letter combination of telephone number)
- Balanced binary tree -- adjusting transformation rules
猜你喜欢

平衡二叉树——调整变换规则

Dart异步是怎麼實現

八大排序之直接插入排序

A component required a bean of type 'com. example. demo3.service. UserServiceImp' that could not be fou

C51的一些日记

docker 安装redis

MySQL index creation, optimization analysis and index optimization

Wireshark数据包分析——wireshark0051.pcap

Dart异步是怎么实现

MySQL 45 lecture notes (I) execution of an SQL statement
随机推荐
倍福TwinCAT 3 气缸动作程序编写
VIM from dislike to dependence (18) -- advanced search mode
AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了
嵌入式软件测试的经验经历和总结
How to break through the sales dilemma of clothing stores
How to randomly assign 1000 to 10 numbers
快速掌握 ASP.NET 身份认证框架 Identity - 用户注册
【牛客刷题-SQL大厂面试真题】NO1.某音短视频
Nepal graph learning Chapter 2_ A bug before version v2.6.1 caused OOM
C # custom sorting
Balanced binary tree -- adjusting transformation rules
告警日志中出现ORA-48132 ORA-48170
Research on std:: move and std:: forward right value reference
ORA-32700: error occurred in DIAG Group Service
L'avenir est venu: l'âge du nuage
3DE robot suction cup grabbing box
云原生架构(02)-什么是云原生
Rabbmitmq publish subscribe mode < 2 >
replacement has 2 rows, data has 0, 解决R语言如何动态生成dataframe
Rabbmitmq simple mode < 1 >