敲除验证-如何显示错误消息 [英] Knockout Validation - How to show error messages
问题描述
我们正在使用Knockout.js和Knockout-validation插件. 当用户返回具有验证错误的页面时,我们希望显示错误消息.有人知道是否有可能在不实际更改绑定答案的情况下触发敲除验证吗?
We're using Knockout.js and the Knockout-validation plugin. When a user returns to a page that has validation errors, we want the error messages to display. Does anyone know if it's possible to trigger knockout validation without actually changing the bound answer?
推荐答案
解决方案是调用showAllMessages.如果视图模型具有嵌套的可观察对象,请确保将ko.validation.configure设置为使用深度分组,因为默认值为false.
The solution is to call showAllMessages. If the view model has nested observables, be sure to set ko.validation.configure to use deep grouping because the default value is false.
示例:
viewModel.save = function()
{
var result = ko.validation.group(viewModel, {deep: true});
if (!viewModel.isValid())
{
alert("Please fix all errors before preceding");
result.showAllMessages(true);
return false;
}
//actually save stuff, call ajax, submit form, etc
}
或者,您可以将!viewModel.isValid()
替换为result().length > 0
Alternatively, you can replace !viewModel.isValid()
with result().length > 0
这篇关于敲除验证-如何显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!