javascript - 重新评估选项对验证规则没有影响
问题描述
我有一个模型,其名称字段已针对唯一值进行了远程验证。它按预期工作。现在,如果我想更新一些其他属性并且不更改名称字段,在这种情况下我也会在名称字段上收到验证错误。根据 devextreme 文档,该reevaluate
选项应设置为 false 以不验证值未更改的字段。我相应地配置了验证器,但这没有效果。
下面是验证器配置脚本的视图(其他字段通过 ASP.net 验证属性配置):
@model Sender
<form asp-action=@(ViewBag.Mode == "new" ? "AddSender" : "UpdateSender") asp-controller="Sender" method="post">
@using (Html.DevExtreme().ValidationGroup())
{
@Html.HiddenFor(m => Model.Id)
@(Html.DevExtreme().Form<Sender>()
.ID("form")
.FormData(Model)
.ColCount(1)
.Items(items => {
items.AddSimpleFor(m => Model.Name).Label(l => l.Text("Név")).Editor(e => e.TextBox().ID("sendername")).IsRequired(true);//.Editor(e => e.Autocomplete()
//.DataSource(d => d.StaticJson().Url(Url.Action("GetNames", "Sender")))
//.SearchMode(DropDownSearchMode.StartsWith)
//.ValueExpr("Name"));
items.AddSimpleFor(m => Model.Address);
items.AddSimpleFor(m => Model.ContactPerson);
items.AddSimpleFor(m => Model.ContactEmail);
items.AddGroup().Items(groupItem => groupItem.AddSimple().Template(
@<text>
<div style="text-align: right">
@(Html.DevExtreme().Button().ID("save").Text("Mentés").Width(100).Type(ButtonType.Success).UseSubmitBehavior(true))
@(Html.DevExtreme().Button().ID("cancel").Text("Mégsem").Width(100).Type(ButtonType.Normal).OnClick("close_onClick"))
</div>
</text>));
})
.LabelLocation(FormLabelLocation.Top)
)
}
</form>
<script>
$("#sendername").dxTextBox().dxValidator({
validationRules: [{
type: "async",
message: "Partner with this name already exists",
reevaluate: false,
validationCallback: function (params) {
return DevExpress.aspnet.sendValidationRequest("Name", params.value, "/MasterData/Sender/ValidateSenderName", "POST");
}
}, {
type: "required",
message: "Field value cannot be null"
}
]
});
</script>
我该如何解决这个问题?
解决方案
我建议Remote
为模型的“名称”字段添加属性,而不是创建验证器。而且我在您的代码中发现了错误,您为“发件人名称”创建了 dxTextBox,但它是在 Razor 中创建的。这篇博文描述了在 ASP.NET Core 和 ASP.NET MVC 中使用异步验证。
推荐阅读
- pandas - 如何从熊猫中绘制 fdate 并选择日期格式?
- kotlin - PageKeyedDataSource loadAfter 连续调用
- vue.js - 如何使用 jest.fn() 模拟属性
- r - 如何在 R 中按组顺序标记类别?
- r - 在 R 中读取具有不同布局的多个 Excel 文件
- postgresql - 带有 lambdas 的 AWS RDS Postgresql API 在创建后非常慢
- cmake - 在 Ubuntu 上构建 X86 LLVM
- python - 说列表 = [9, 8, 9, 10]。例如,我如何找到 9 的位置?
- office365 - 添加选项以在安装期间配置 Office 365 加载项
- python - 如何删除 VS Code 中的特定 linting 高亮显示?