- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Please note that although this is closely related to this question, the answer there is specific to one known model property, whereas I want this to work for any property, so the component is reusable.
我想使用 Blazor <ValidationMessage>
组件内的标记。但是,当我这样做时,不会显示验证消息。
例如,以下组件 ( FormRowText.razor
) 创建包含 <input type="text />
的行(在 Bootstrap 网格意义上)对于模型上的命名属性...
<div class="form-group row">
<label for="@PropertyName" class="col-lg-2 col-form-label">@(Caption ?? PropertyName)</label>
<div class="col-lg-10 input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="far fa-@Icon"></i>
</span>
</div>
<input class="form-control" Value="@Value" type="text"
id="@PropertyName" name="@PropertyName" @onchange="@OnChanged" />
</div>
</div>
@code {
[Parameter]
public string PropertyName { get; set; }
[Parameter]
public string Value { get; set; }
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
[Parameter]
public string Caption { get; set; }
[Parameter]
public string Icon { get; set; }
private async Task OnChanged(ChangeEventArgs cea) =>
await ValueChanged.InvokeAsync(cea.Value?.ToString() ?? "");
}
它是这样使用的...
<EditForm ...>
<FormRowText PropertyName="@nameof(Customer.Email)" @bind-Value="_customer.Email" Icon="at" />
</EditForm>
如果我添加 <ValidationMessage>
在这个标记之后,它工作正常,但如果我将它添加到组件中(这是我想要它的位置),它就不起作用。
我看到了this answer来自@enet,它展示了如何针对特定的已知模型属性执行此操作。但是,我的组件设计用于任何模型属性,我不确定如何修改它。
我尝试为模型添加一个参数(在文件顶部使用 @typeparam T
)...
[Parameter]
public T Model { get; set; }
...并在 HTML 中添加以下内容...
<ValidationMessage For="() => PropertyName" />
但是这不起作用。
有什么想法吗?
最佳答案
您可以将 EditContext 级联到您的组件并使用它来通知当前字段已更改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
public partial class FormRowText
{
[CascadingParameter] EditContext CascadedEditContext { get; set; }
[Parameter]
public string PropertyName { get; set; }
[Parameter]
public string Value { get; set; }
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
[Parameter]
public string Caption { get; set; }
[Parameter]
public string Icon { get; set; }
[Parameter] public Expression<Func<string>> ValueExpression { get; set; }
private async Task OnChanged(ChangeEventArgs cea)
{
await ValueChanged.InvokeAsync(cea.Value?.ToString() ?? "");
var identifier = CascadedEditContext.Field(PropertyName);
CascadedEditContext.NotifyFieldChanged(identifier);
}
}
<div class="form-group row">
<label for="@PropertyName" class="col-lg-2 col-form-label">@(Caption ?? PropertyName)</label>
<div class="col-lg-10 input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="far fa-@Icon"></i>
</span>
</div>
<input class="form-control" value="@Value" type="text" id="@PropertyName" name="@PropertyName"
@onchange="@OnChanged" />
<ValidationMessage For="@ValueExpression" />
</div>
</div>
你还需要
public class Customer
{
[Required]
public string Name { get; set; }
public Country Country { get; set; } = Country.Israel;
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
}
@page "/"
@using System.ComponentModel.DataAnnotations;
@using Microsoft.AspNetCore.Components.Forms;
<EditForm EditContext="EditContext" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<label for="name">Name: </label>
<InputText @bind-Value="@customer.Name"></InputText>
<ValidationMessage For="@(() => customer.Name)" />
</div>
<div class="form-group">
<FormRowDropdownEnum @bind-Value="customer.Country" Caption="Country" T="Country"></FormRowDropdownEnum>
<ValidationMessage For="@(() => customer.Country)" />
<FormRowText @bind-Value="customer.Email" Caption="Email"
PropertyName="@nameof(Customer.Email)" Icon="at"></FormRowText>
</div>
<p>
<button type="submit" class="btn btn-primary">Save</button>
</p>
</EditForm>
@code{
private Customer customer = new Customer();
private EditContext EditContext;
protected override void OnInitialized()
{
EditContext = new EditContext(customer);
}
public async Task HandleValidSubmit()
{
await Task.Delay(1);
Console.WriteLine("Saving...");
}
}
当您使用 EditForm 组件时,最好使用 Forms 组件,例如 InputText 组件等。
以下代码片段描述了如何通过对 InputText 组件进行子类化并添加新属性来实现 FormRowText 组件。请注意,组件的功能不需要 PropertyName
@inherits InputText
<div class="form-group row">
<label for="@PropertyName" class="col-lg-2 col-form-label">@(Caption ?? PropertyName)</label>
<div class="col-lg-10 input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="far fa-@Icon"></i>
</span>
</div>
<input class="form-control" @bind="CurrentValue" type="text" id="@PropertyName" name="@PropertyName" />
<ValidationMessage For="@ValueExpression" />
</div>
</div>
@code{
[Parameter]
public string Caption { get; set; }
[Parameter]
public string Icon { get; set; }
[Parameter]
public string PropertyName { get; set; }
}
关于blazor - 如何在组件中使用 <ValidationMessage>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65224446/
Please note that although this is closely related to this question, the answer there is specific to
我最近遇到了一个小问题。创建新学生时,我必须测试 StudentNo 是否已被使用,它必须是唯一的。测试在 Controller 中。当 StudentNo 已经在数据库中时,我添加了一个自定义错误:
我们的 MVC3 模型包括一组这样的字符串 [CannotBeEmpty(ErrorMessageResourceName = "ColorCodes", ErrorMessageResourceTy
我过去常常在一个公共(public)库中收集我所有的验证约束。在 jar 的根目录中,我放置了一个 ValidationMessages_it.properties 文件。一切正常,如果我将这个库放入
我有一个 Controller ,我将错误(异常)添加到 ModelState。在 View 中,当我尝试通过 Html.ValidationMessage() 显示消息时,它没有显示消息。谁能帮我解
我需要在教科书输入中强制执行动态小数位数。我已经看到使用属性属性和正则表达式强制设置小数位数的堆栈溢出示例。 我的要求是动态的。 @Html.TextBoxFor() 是动态生成的,每个都链接到一个属
我有一个项目需要一些内部化。具体来说,我有一个名为“LocalizedString”的类,它包含特定文本的英语和德语翻译。 这看起来像这样: [ComplexType] public class
我对 JSR303 和特殊的 Hibernate-Validator 有疑问。 我想从属性文件中读取验证消息。此属性文件存在于部署到 servlet 容器的 war 文件中。问题是,它没有被访问。我有
我正在测试一系列内容是否为空。每次我找到一个,我都会将其保存在一个数组中,以便在验证消息中实现它。 我想要的输出如下所示: 字段 1 为必填项 字段 4 为必填 等等... 但我似乎无法开始新的一行。
我在 MVC 中使用 ValidationMessage 控件。验证每个属性时,可能会显示多个错误消息,但 ValidationMessage 仅显示列表中的第一个错误消息。 这是一个例子: Mode
我想减少以下代码。下面的代码有效,但它不是很整洁等。 Name: " /> 我已经读到我需要扩展 Html 帮助程序,以便我可以返回图像而不是包含默认元素和文本错误的文本。 我
我正在使用 knockout 验证,我想通过我自己的函数显示自定义消息这是我的 ko 验证代码 ko.validation.init({ // registerExtenders: true
我在正确设置自定义 Bean 验证消息时遇到了一些困难。我添加了以下约束定义并将其放在一些 bean 上,更好地说是属性。 @Constraint(validatedBy = NobleValidat
出于验证目的,我使用了 validationMessage 和 ValidationMessageFor Helpers。两者都显示错误消息,那么验证消息和验证消息之间的主要区别是什么。我在各种网站上
我正在尝试在表单的多个部分上使用knockout.validation。其中大部分都工作正常,并且可以使用内置的消息附加程序,但我有 1 个特定字段,我需要自己手动将消息添加到 DOM。 不过,我在让
如何更改验证消息的资源包?默认值是将我的 ValidationMessages.properties 读取为 ISO-8859-1(我猜),但它是 UTF-8。我试过这个: @Configuratio
我可能只是做得不对,但就是这样。 我有一个 Spring Boot 应用程序,其中添加了 Hibernate Validator,并且有一个如下所示的命令对象: public class Signup
我正在尝试通过使用检查模型中特定字段是否存在 ValidationMessage String.IsNullOrEmpty(Html.ValidationMessageFor(m => m.amoun
我在 JBOSS EAP 6.3 服务器中收到以下错误消息。我正在使用 seam 2.3 和 hibernate 4。我不确定这个问题。 com.google.gwt 模块给出此错误消息。 07:59
我正在使用 Spring Boot 构建 Web API。端点之一采用 JSON 发布请求。我想在某些字段上设置验证消息。所以我在/src/main/resources 下添加了一个 Validati
我是一名优秀的程序员,十分优秀!