【问题标题】:Vee Validate 3.0 custom classes not applied未应用 Vee Validate 3.0 自定义类
【发布时间】:2019-11-27 20:59:31
【问题描述】:

从文档中,我认为我需要使用 configure 将自定义类添加到我的验证字段中,但我无法让它工作。

这就是我目前所拥有的......

import { extend, configure, localize } from 'vee-validate'
import { required, min, max } from 'vee-validate/dist/rules'
import en from 'vee-validate/dist/locale/en.json'

// Install rules
extend('required', required)
extend('min', min)
extend('max', max)

// Install classes
configure({
  classes: {
    valid: 'is-valid',
    invalid: 'is-invalid'
  }
})

// Install messages
localize({
  en
})

在我看来....

<ValidationObserver ref="observer" v-slot="{ invalid }" tag="form" @submit.prevent="checkRef()">
  <div class="form-group">
    <label for="reference">Reference</label>
    <ValidationProvider rules="required|max:20" name="reference" v-slot="{ errors }">
      <input maxlength="20" name="reference" v-model="ref" id="reference" class="form-control"/>
      <span class="warning">{{ errors[0] }}</span>
    </ValidationProvider>
  </div>
  <button @click="checkRef" class="btn btn-primary app-button">Check Reference</button>
</ValidationObserver>

当我单击按钮时,我看到错误消息,但我没有将“无效”类应用于我的字段。

我做错了什么?

【问题讨论】:

    标签: vue.js vee-validate


    【解决方案1】:

    VeeValidate 不再自动应用这些类,因为v3 您现在必须自己绑定它。像errors 一样,您可以从插槽道具中提取classes 并将其应用于您的输入:

    <ValidationProvider rules="required|max:20" name="reference" v-slot="{ errors, classes }">
      <input maxlength="20" name="reference" v-model="ref" id="reference" class="form-control" :class="classes" />
      <span class="warning">{{ errors[0] }}</span>
    </ValidationProvider>
    

    【讨论】:

    • 啊,我明白了!我需要您在此处提供的代码,但我可以使用配置代码来覆盖类名。一如既往地感谢 logaretm
    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 2020-06-07
    • 2018-12-21
    • 2018-06-07
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多