公共文件夹下,如:validators.js
export const customRule = (rule, value, callback) => {
if (value === 'example') {
callback(new Error('This field cannot be "example".'));
} else {
callback();
}
};
页面中
<template>
<el-input v-model="inputValue" placeholder="Enter a value" :rules="customRules"></el-input>
</template>
<script>
import { customRule } from './validators.js';
export default {
data() {
return {
inputValue: '',
customRules: [
{ validator: customRule, trigger: 'blur' }
]
};
}
};
</script>