当前位置:网站首页>Blazor University (34)表单 —— 获得表单状态
Blazor University (34)表单 —— 获得表单状态
2022-06-29 00:39:00 【dotNET跨平台】
原文链接:https://blazor-university.com/forms/accessing-form-state/
获得表单状态
源代码[1]
有时,我们需要获得 <EditForm> 子内容中的表单状态。最常见的用途是当我们需要访问输入的 CSS 类时,指示输入是否被修改或有效/无效。
例如,如果我们使用 Bootstrap 创建一个带有 @ 符号的电子邮件输入控件,我们最终可能会得到如下所示的标记。
<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>
然而,问题在于,当用户输入无效值时,CSS 无效类仅应用于 <InputText> 控件。

如果我们想将 CSS 无效类应用到输入组本身,我们可以使用从 <EditForm> 组件传递给我们的 EditContext。
<EditForm> 的 ChildContent 参数是 RenderFragment<EditContext>,这意味着 EditContext 实例通过名为 context 的变量(或我们告诉 Blazor 使用的任何别名)传递到其内部内容。有关更多信息,请参阅使用 RenderFragments 模板化组件[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>第 1 行
使用
Context=语法,我们告诉 Blazor 在传入其EditContext时使用变量名称CurrentEditContext。第 6 行
使用
EditContext.FieldCssClass方法根据输入的状态(修改/有效/无效)获取正确的 CSS 类名称。

如果我们愿意,我们可以用一些简单的 CSS 隐藏生成的 <input> HTML 元素上的红色轮廓。
.input-group > input.invalid
{
outline: none;
}该 CSS 告诉浏览器,如果应用了无效类的 <input> HTML 元素直接由应用了 input-group CSS 类的 HTML 元素作为父元素,则该元素不应具有红色轮廓。

参考资料
[1]
源代码: https://github.com/mrpmorris/blazor-university/tree/master/src/Forms/AccessingFormState
边栏推荐
- HandlerThread使用及原理
- 每日一题:数组中数字出现的次数2
- pinhole camera model
- The magical zero knowledge proof can not only keep secrets, but also make others believe you!
- MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
- [staff] accent mark, gradually stronger mark and gradually weaker mark
- Single machine multi instance MySQL master-slave replication
- Use menu resources to implement option menus and context menus
- 这玩意叫跳表?
- 学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条
猜你喜欢

Nodejs安装和下载

MySQL 8.0 above reporting 2058 solution

PR 2021 quick start tutorial, how to use audio editing in PR?

oracle 去掉html标签
![[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

Reprint: VTK notes - clipping and segmentation - irregular closed loop clipping -vtkselectpolydata class (black mountain old demon)

滑环的基本结构及工作原理分析

Pinhole camera with added lens

戴口罩人脸数据集和戴口罩人脸生成方法

Getting started with SQL
随机推荐
SCP copy folder
Xiaobai's e-commerce business is very important to choose the right mall system!
Reasons for high price of optical fiber slip ring
点击劫持:X-Frame-Options未配置
每日一题:消失的数字
Use and principle of handlerthread
Is it safe to open an account on great wisdom
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached
请问同花顺上开户安全吗
Excel使用过程中的参考资料
Realization of beauty system with MATLAB
11.目标分割
Analysis of basic structure and working principle of slip ring
Mysql的四种引擎介绍
光纤滑环价格过高的原因
搭建单机 nacos 负载均衡ribbon 轮询策略 权重2种方式
架构实战营|模块5
LG. Hankson's interesting questions, C language
Haskell configuring vs code development environment (june2022)
Roson's QT journey 80 qurl class