【问题标题】:Kendo UI: Displaying server-side errors using Kendo ValidatorKendo UI:使用 Kendo Validator 显示服务器端错误
【发布时间】:2013-08-08 22:44:04
【问题描述】:
我有一个使用Kendo Validator 进行客户端验证的网络表单。一旦客户端验证成功,表单值将被发送到执行额外验证的 Web 服务,并保存数据或发回由表单字段键入的错误消息的 JSON 对象。这些字段名称与验证器元素上的data-for 属性相匹配。有没有办法使用Kendo Validator 显示这些错误?
我意识到您可以设置自定义规则来对每个字段进行服务器端验证。这是关于一次验证所有字段并显示多个错误。
【问题讨论】:
标签:
validation
kendo-ui
server-side-validation
kendo-validator
【解决方案2】:
您可以附加到响应事件和调用函数,当服务器返回一些错误时显示验证错误。
这是可以做到这一点的javascript部分:
validationMessageTemplateForReplace = kendo.template(
'<div class="k-widget k-tooltip k-tooltip-validation k-invalid-msg field-validation-error" style="margin: 0.5em; display: block; " data-for="#=field#" data-valmsg-for="#=field#" id="#=field#_validationMessage">' +
'<span class="k-icon k-warning"> </span>#=message#<div class="k-callout k-callout-n"></div></div>');
function onResponseEnd(response) {
if (response.errors) onError(response.errors, $('#myForm'));
}
function onError(errors, element) {
for (var error in errors) {
addValidationMessage(element, error, errors[error].errors);
}
}
function addValidationMessage(container, name, errors) {
//add the validation message to the form
var found = container.find("[data-for=" + name + "],[data-val-msg-for=" + name + "],[data-valmsg-for=" + name + "]");
if (found.length > 0) {
found.replaceWith(bs.validationMessageTemplateForReplace({ field: name, message: errors[0] }));
return true;
}
return false;
}
也许this 示例项目也会对您有所帮助。它与网格弹出编辑有关,但显示了您想要实现的机制。