關(guān)于element中對el-input 自定義驗(yàn)證規(guī)則
element對el-input 自定義驗(yàn)證規(guī)則
首先明確要使自定義校驗(yàn)生效的話,必須 prop 和 rules 的 鍵對應(yīng),如示例:(prop="description"在 rules 中有對應(yīng)的鍵 )
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm"> ? ? <el-form-item label="描述:" prop="description"> ? ? ? ? <el-input type="textarea" v-model="ruleForm.description" maxLengtn="128" placeholder="請輸入描述"></el-input> ? ? </el-form-item> </el-form>
rules: {
? ? description: [{ required: true, message: '請輸入描述', trigger: 'blur' }]
}自定義校驗(yàn)傳入自定義參數(shù)
elementui的自定義校驗(yàn)不能傳入自定義參數(shù),如果想傳入自定義參數(shù)的話,可以如下操作:
rules: {
? ? description: [{?
? ? ? ? validator: (rule, value, callback) => {
? ? ? ? ? this.validateType(rule, value, callback, this.params)
? ? ? ? },
? ? ? ? trigger: ['blur', 'change']
? ? }]
}this.params 相當(dāng)于自定義參數(shù),然后 this.validateType中就可以接收到自定義的參數(shù),并且也能對輸入框的值進(jìn)行校驗(yàn)了。
示例:
驗(yàn)證一個(gè)輸入框的值的類型,這個(gè)值的類型可能是['list', 'num', 'bool', 'str']中的一種或多種,但如果為 list 的話就只能是 list 類型
// 數(shù)據(jù)類型有 ['list', 'num', 'bool', 'str']
// 一個(gè)輸入框的數(shù)據(jù)類型的情況可能有
// 情況一:數(shù)據(jù)類型為 ['list'],那輸入值的數(shù)據(jù)類型就可能都滿足,就返回 true,最后將輸入框中的值用 split(',')轉(zhuǎn)為數(shù)組類型即可
// 情況二:數(shù)據(jù)類型為 ['num', 'bool', 'str'],那輸入的值比如 12/true/'abc',用 typeof value 判斷輸入的數(shù)據(jù)類型
// let allTypes = ['list', 'num', 'bool', 'str']
/**
* @param {*} arr 輸入框的數(shù)據(jù)類型
* @param {*} value 輸入框的值
*/
function checkType (arr, value) {
if (arr.includes('list') && arr.length === 1) { // 還不確定
return true
} else {
// el-input 得到的值為字符串,所以需要處理,'1', 'true', '是', 0/1,使用 JSON.parse(value) 可以將對應(yīng)類型的值轉(zhuǎn)換,如果 JSON.parse('abc') 會(huì)直接報(bào)錯(cuò)
try {
value = JSON.parse(value)
} catch (error) {
// 字符串不做處理
}
if (['是', '否'].includes(value)) {
value = value === '是'
}
return arr.some(item => {
return (typeof value).indexOf(item) !== -1
})
}
}
console.log(checkType(['num', 'str', 'bool'], 'abc'))
element-ui自定義表單驗(yàn)證,但是不出現(xiàn)小紅心了
基本上按照文檔上提供的方式做就沒啥大問題 , 我遇到的問題是 , 自定義以后不顯示小紅星了
<el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
? <el-form-item label="密碼" prop="pass">
? ? <el-input type="password" v-model="ruleForm2.pass" autocomplete="off"></el-input>
? </el-form-item>
? <el-form-item label="確認(rèn)密碼" prop="checkPass">
? ? <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off"></el-input>
? </el-form-item>
? <el-form-item label="年齡" prop="age">
? ? <el-input v-model.number="ruleForm2.age"></el-input>
? </el-form-item>
? <el-form-item>
? ? <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
? ? <el-button @click="resetForm('ruleForm2')">重置</el-button>
? </el-form-item>
</el-form><script>
? export default {
? ? data() {
? ? ? var checkAge = (rule, value, callback) => {
? ? ? ? if (!value) {
? ? ? ? ? return callback(new Error('年齡不能為空'));
? ? ? ? }
? ? ? ? setTimeout(() => {
? ? ? ? ? if (!Number.isInteger(value)) {
? ? ? ? ? ? callback(new Error('請輸入數(shù)字值'));
? ? ? ? ? } else {
? ? ? ? ? ? if (value < 18) {
? ? ? ? ? ? ? callback(new Error('必須年滿18歲'));
? ? ? ? ? ? } else {
? ? ? ? ? ? ? callback();
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }, 1000);
? ? ? };
? ? ? var validatePass = (rule, value, callback) => {
? ? ? ? if (value === '') {
? ? ? ? ? callback(new Error('請輸入密碼'));
? ? ? ? } else {
? ? ? ? ? if (this.ruleForm2.checkPass !== '') {
? ? ? ? ? ? this.$refs.ruleForm2.validateField('checkPass');
? ? ? ? ? }
? ? ? ? ? callback();
? ? ? ? }
? ? ? };
? ? ? var validatePass2 = (rule, value, callback) => {
? ? ? ? if (value === '') {
? ? ? ? ? callback(new Error('請?jiān)俅屋斎朊艽a'));
? ? ? ? } else if (value !== this.ruleForm2.pass) {
? ? ? ? ? callback(new Error('兩次輸入密碼不一致!'));
? ? ? ? } else {
? ? ? ? ? callback();
? ? ? ? }
? ? ? };
? ? ? return {
? ? ? ? ruleForm2: {
? ? ? ? ? pass: '',
? ? ? ? ? checkPass: '',
? ? ? ? ? age: ''
? ? ? ? },
? ? ? ? rules2: {
? ? ? ? ? pass: [
? ? ? ? ? ? { validator: validatePass, trigger: 'blur' }
? ? ? ? ? ],
? ? ? ? ? checkPass: [
? ? ? ? ? ? { validator: validatePass2, trigger: 'blur' }
? ? ? ? ? ],
? ? ? ? ? age: [
? ? ? ? ? ? { validator: checkAge, trigger: 'blur' }
? ? ? ? ? ]
? ? ? ? }
? ? ? };
? ? },
? ? methods: {
? ? ? submitForm(formName) {
? ? ? ? this.$refs[formName].validate((valid) => {
? ? ? ? ? if (valid) {
? ? ? ? ? ? alert('submit!');
? ? ? ? ? } else {
? ? ? ? ? ? console.log('error submit!!');
? ? ? ? ? ? return false;
? ? ? ? ? }
? ? ? ? });
? ? ? },
? ? ? resetForm(formName) {
? ? ? ? this.$refs[formName].resetFields();
? ? ? }
? ? }
? }
</script>我只是照著改了一下
let validatePass = (rule, value, callback) => {
? ? ? console.log(rule);
? ? ? console.log(value);
? ? ? console.log(callback);
? ? ? if (!value) {
? ? ? ? return callback(new Error("請?zhí)顚懝久Q"));
? ? ? }
? ? ? if (this.form.id) {
? ? ? ? callback();
? ? ? ? return true;
? ? ? }
? ? ? readScmSupplierPage({ name: this.form.name ,type:'2'})
? ? ? ? .then(res => {
? ? ? ? ? if (res.data.length > 0) {
? ? ? ? ? ? callback(new Error("名稱重復(fù)"));
? ? ? ? ? } else {
? ? ? ? ? ? callback();
? ? ? ? ? }
? ? ? ? })
? ? ? ? .catch(err => {
? ? ? ? ? console.log(err);
? ? ? ? });
? ? };基本上和自定義沒啥關(guān)系
rules: {
? ? ? ? // name: [{ required: true, message: "請輸入公司名稱", trigger: "blur" }],
? ? ? ? name: [{ required: true, validator: validatePass, trigger: "blur" }],
? ? ? ? abbreviation: [
? ? ? ? ? { required: true, message: "請輸入公司簡稱", trigger: "blur" }
? ? ? ? ]
? ? ? },只是我發(fā)現(xiàn)如果自定義了 , 在這個(gè)地方加required: true, 是不起作用的, 必須在form表單上面加
<el-form-item label="公司名稱" :label-width="formLabelWidth" prop="name" required> ? ? ? ? ? <el-input v-model="form.name"></el-input> ? ? ? ? </el-form-item>
就這樣小紅星星就出現(xiàn)啦
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解基于Vue-cli搭建的項(xiàng)目如何和后臺(tái)交互
這篇文章主要介紹了詳解基于Vue-cli搭建的項(xiàng)目如何和后臺(tái)交互,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
vue2 中如何實(shí)現(xiàn)動(dòng)態(tài)表單增刪改查實(shí)例
本篇文章主要介紹了vue2 中如何實(shí)現(xiàn)動(dòng)態(tài)表單增刪改查實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Vue v-html指令詳細(xì)解析與代碼實(shí)例(最新推薦)
v-html是Vue.js框架中的一個(gè)指令,用于將數(shù)據(jù)中的HTML代碼動(dòng)態(tài)渲染到頁面上,它主要用于渲染一些靜態(tài)的HTML內(nèi)容或者從后臺(tái)獲取的富文本數(shù)據(jù),下面介紹Vue v-html詳細(xì)解析與代碼實(shí)例,感興趣的朋友一起看看吧2024-12-12
npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated
安裝vue腳手架報(bào)錯(cuò)可能具體原因比較多,可以根據(jù)報(bào)錯(cuò)信息進(jìn)行排查,本文主要介紹了npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated,感興趣的可以了解一下2023-11-11
Vue3+Vite項(xiàng)目使用less的實(shí)現(xiàn)步驟
最近學(xué)習(xí)在vite項(xiàng)目中配置less,本文主要介紹了Vue3+Vite項(xiàng)目使用less的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
vue-element-admin項(xiàng)目導(dǎo)入和導(dǎo)出的實(shí)現(xiàn)
組件是Vue.js最強(qiáng)大的功能,這篇文章主要介紹了vue-element-admin項(xiàng)目導(dǎo)入和導(dǎo)出的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
vue中el-table樹狀表格末行合計(jì)實(shí)現(xiàn)
本文主要介紹了vue中el-table樹狀表格末行合計(jì)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

