NuxtJS-vee-validate不返回自定义消息

问题描述 投票:0回答:1

vee-validate.js

import { extend } from 'vee-validate'
import * as rules from 'vee-validate/dist/rules'

const phone = {
  getMessage (field) {
    return `The ${field} must be a valid phone number`
  },
  validate (value) {
    const PhoneNumber = require('awesome-phonenumber')
    const pn = new PhoneNumber(value)
    return pn.isValid()
  }
}

extend('phone', phone)

组件中的用法:

 <ValidationProvider v-slot="{ errors }" rules="required|phone">
    <input
      v-model="number"
      placeholder="Ex. +13198832832"
      type="tel"
      name="phonenumber"
      label="Phone Number*"
      />
      <ValidationDisplay :message="errors[0]" />
 </ValidationProvider>

出于某种原因,验证消息始终为:{field} is not valid.而不是我指定的名称。

vue.js vuejs2 nuxt.js vee-validate
1个回答
0
投票

您需要在ValidationProvider组件上提供一个字段名称。这就是错误消息中使用的内容。

<ValidationProvider v-slot="{ errors }" rules="required|phone" name="Phone Number">
    <input
      v-model="number"
      placeholder="Ex. +13198832832"
      type="tel"
      name="phonenumber"
      label="Phone Number*"
      />
      <ValidationDisplay :message="errors[0]" />
 </ValidationProvider>
© www.soinside.com 2019 - 2024. All rights reserved.