【发布时间】:2020-01-06 13:16:01
【问题描述】:
我正在努力从 VeeValidate v2 升级到 v3。由于他们已经删除了 ErrorBag 的概念,我正在努力弄清楚如何处理后端验证。
以前(参见下面的代码),我只是在运行客户端验证,如果通过,则调用服务器验证路由,如果失败,我将使用 VeeValidate 中的 errors.add 函数。
任何帮助将不胜感激。只需要知道在 VeeValidate v3 中完成后端验证处理。谢谢!
validateStep(step) {
this.$validator.validateAll(step).then((result) => {
// If client-side validation passes, move into this block.
if (result) {
// Then run server-side validation.
axios
.post(`/ajax/validate-stuff`, this.postData)
// If server-side validation Passes:
.then(function (response) {
// Do the things
})
// If server-side validation Fails:
.catch(function (error) {
// Add errors to VeeValidate Error Bag
var entries = Object.entries(error.response.data.errors);
entries.forEach(function(item, index) {
this.Errors.add({
field: item[0],
msg: item[1][0]
});
});
});
}
});
}
【问题讨论】:
标签: vue.js vee-validate