我更喜欢自己使用指令版本。将 VeeValidate 导入组件后,您可以访问所有内置验证,v-validate
并可以使用管道运算符将它们链接在一起|
:
<template>
<input name="email" type="text" v-model="emailData" v-validate="'required|email'">
<button @click="submit()"></button>
</template>
<script>
import VeeValidate from 'vee-validate'; //wherever your plugin is stored
data() {
return {
emailData: null,
},
},
methods: {
// call this method upon form submission, this will validate all fields using the specified validation
submit() {
this.$validator.validateAll().then((result) => {
if (result) {
// form/input(s) passed validation. Do something
}
});
}
</script>
如果您想在不使用指令的情况下完成此操作,则应使用以下方法:
import { Validator } from 'vee-validate';
const validator = new Validator();
const expressions = {
required: true,
regex: /\d{11}/
};
validator.attach({ name: 'tel', rules: expression });
// continue with validator.validate() etc...