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.
而不是我指定的名称。
您需要在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>