当前位置:网站首页>Blazor University (34) forms - get form status
Blazor University (34) forms - get form status
2022-06-29 00:46:00 【Dotnet cross platform】
Link to the original text :https://blazor-university.com/forms/accessing-form-state/
Get form status
Source code [1]
Sometimes , We need to get <EditForm> Form status in the sub content . The most common use is when we need to access the input CSS Class time , Indicates whether the input is modified or valid / Invalid .
for example , If we use Bootstrap Create a with @ E-mail input control for symbol , We may end up with the following markup .
<div class="form-group">
<label for="EmailAddress">Email address</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">@@</span>
</div>
<InputText @bind-Value=MyContact.EmailAddress id="EmailAddress" class="form-control" type="email" />
</div>
</div>
However , The problem lies in , When the user enters an invalid value ,CSS Invalid class only applies to <InputText> Control .

If we want to CSS Invalid class applied to input group itself , We can use from <EditForm> Components are passed to us EditContext.
<EditForm> Of ChildContent Parameter is RenderFragment<EditContext>, It means EditContext The instance is named context The variable of ( Or we tell Blazor Any alias used ) To its internal content . For more information , Please see use RenderFragments Templated components [2].
<EditForm [email protected] Context="CurrentEditContext">
<DataAnnotationsValidator />
<div class="form-group">
<label for="EmailAddress">Email address</label>
<div class="input-group @CurrentEditContext.FieldCssClass( () => MyContact.EmailAddress)">
<div class="input-group-prepend">
<span class="input-group-text">@@</span>
</div>
<InputText @bind-Value=MyContact.EmailAddress id="EmailAddress" class="form-control" type="email" />
</div>
</div>
</EditForm>The first 1 That's ok
Use
Context=grammar , We tell Blazor Before passing in itsEditContextUse variable names whenCurrentEditContext.The first 6 That's ok
Use
EditContext.FieldCssClassMethod according to the status of the input ( modify / It works / Invalid ) Get the right CSS Class name .

If we want to , We can use some simple CSS Hide generated <input> HTML The red outline on the element .
.input-group > input.invalid
{
outline: none;
} The CSS Tell the browser , If an invalid class is applied <input> HTML The element is directly applied by input-group CSS Class HTML Element as parent element , Then the element should not have a red outline .

Reference material
[1]
Source code : https://github.com/mrpmorris/blazor-university/tree/master/src/Forms/AccessingFormState
边栏推荐
猜你喜欢

卷绕工艺与叠片工艺的对比
![[staff] accent mark, gradually stronger mark and gradually weaker mark](/img/5d/5738bd5503d7ed0621932f901c2e8d.jpg)
[staff] accent mark, gradually stronger mark and gradually weaker mark
![[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code](/img/b5/02979b50db885f0606dce455182ac4.jpg)
[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code

12. object detection mask RCNN

Reasons for high price of optical fiber slip ring
![[image registration] improved SAR image registration based on sar-sift with matlab code](/img/69/4e78c6cef45b2e4d133222a4372e43.jpg)
[image registration] improved SAR image registration based on sar-sift with matlab code
![[MCU club] design of classroom number detection based on MCU [simulation design]](/img/87/6dc27c90c9f9d26a6338b3618e974b.jpg)
[MCU club] design of classroom number detection based on MCU [simulation design]

LeetCode每日一题:实现strStr()

Go1.18 new feature: discard strings Title Method, a new pit!

MySQL 8.0 above reporting 2058 solution
随机推荐
毕业三年的25岁码农总结
Accessories and working process of machine vision system
HandlerThread使用及原理
企业和IT领导者对创新的误解
FATAL ERROR: Could not find ./ bin/my_ print_ Solutions to defaults
如果你会玩这4个自媒体运营工具,副业收入6000+很轻松
点击劫持:X-Frame-Options未配置
Reasons for high price of optical fiber slip ring
同期群分析是什么?教你用 SQL 来搞定
Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
JVM底层又是如何实现synchronized的
大型网站架构基础之笔记
Blazor University (34)表单 —— 获得表单状态
每日一题: 数组中数字出现的次数
Click hijack: X-FRAME-OPTIONS is not configured
Haskell configuring vs code development environment (june2022)
Résumé de Manon, 25 ans, diplômé en trois ans
[staff] accent mark, gradually stronger mark and gradually weaker mark
var、let、const 三者的区别
Mapbox GL loading local publishing DEM data