【问题标题】:Add custom errors to vee validator(ErrorBag)向 vee 验证器添加自定义错误(ErrorBag)
【发布时间】:2019-03-27 19:31:00
【问题描述】:

是否可以将自定义错误添加到 ErrorBag 中

我正在使用 nuxtjs。我已经通过 nuxt.config.js 将 vee-validate 注册到我的插件中

不过效果很好

我想在模板中使用相同的错误代码

例如:

 <template>
   <div v-if="errors.all().length>0">
       //loop through
   </div>
 </template>

我正在使用 axios 来获取用户信息。 如果请求没有返回我预期的数据集。我在想我可以简单地

 this.errors.push('this is my error message') //-> or some variant of this

当我这样做时,我知道 this.errors.push 不是函数

我知道

this.errors = ErrorBag{ __ob__: Observer} //-> has items and a vmId attributes

如果我修改代码以推送到 ErrorBag 上,我会得到未定义的推送

【问题讨论】:

    标签: vue.js vuejs2 vee-validate


    【解决方案1】:

    在 ErrorBag 的 API 中有记录。您可以添加自定义消息,例如:

    // For example, you may want to add an error related to authentication:
    errors.add({
      field: 'auth',
      msg: 'Wrong Credentials'
    });
    

    在此处查看文档以获取更多信息:https://vee-validate.logaretm.com/v2/api/errorbag.html

    【讨论】:

    猜你喜欢
    • 2012-05-07
    • 2014-04-05
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2013-08-04
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多