我正在寻找一种优雅的方式来显示来自服务器端API的验证消息,而无需创建自定义验证器或对UI中所有可能的消息进行硬编码。
我需要将错误消息添加到特定字段以及整个表单。
这必须在Angular 2.0.0-beta.3中有效
我向您展示了确定的displayErrors函数(处理遵循JSONAPI标准的服务器端验证):
您将需要Underscore.js
displayErrors(error: ErrorResponse) {
let controls = this.supportRequestForm.controls;
let grouped = _.groupBy(error['errors'], function(e) {
return e['source']['pointer'];
});
_.each(grouped, function(value, key, object) {
let attribute = key.split('/').pop();
let details = _.map(value, function(item) { return item['detail']; });
controls[attribute].setErrors({ remote: details.join(', ') });
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句