当前位置:网站首页>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
边栏推荐
- Cross domain problem of canvas drawing caused by background image cache
- Analysis of basic structure and working principle of slip ring
- Is it safe to open an account on the flush
- 戴口罩人脸数据集和戴口罩人脸生成方法
- FATAL ERROR: Could not find ./ bin/my_ print_ Solutions to defaults
- Nodejs安装和下载
- Notes on the infrastructure of large websites
- [image detection] recognition of the front and back of a coin based on texture features with matlab code attached
- JVM底层又是如何实现synchronized的
- Haskell 配置 VS code 开发环境 (2022年6月)
猜你喜欢

HandlerThread使用及原理

由背景图缓存导致的canvas绘图跨域问题

每日一题:消失的数字

Daily question 1: the number of numbers in the array 2

12.物體檢測Mask-Rcnn

Reasons for high price of optical fiber slip ring

WPF implementation calls local camera~

Operation level smart campus system source code smart campus applet source code + electronic class card + face recognition system

【leetcode】1719. Number of schemes for reconstructing a tree

浏览器缓存库设计总结(localStorage/indexedDB)
随机推荐
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
[gym 102423]-elven efficiency | thinking
卷绕工艺与叠片工艺的对比
ES6:let、const、箭头函数
Jbridge bridging frame technology for AI computing power landing
企业和IT领导者对创新的误解
广度度优先搜索实现抓牛问题
Pinhole camera with added lens
【leetcode】17. Letter combination of telephone number
启牛学堂帮忙开证券账户是安全靠谱的吗?个人如何开
用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]
每日一题:数组中数字出现的次数2
Basic use of Chrome browser
光纤滑环价格过高的原因
Go1.18 new feature: discard strings Title Method, a new pit!
Redis common command manual
Cross domain problem of canvas drawing caused by background image cache
mysql 高可用双主同步
Is it safe to open an account on great wisdom
mysql 8.0以上报2058 解决方式