uniapp插件uview下表單無法動態(tài)校驗(yàn)的問題解決
聲明:關(guān)于uniapp插件uview表單動態(tài)校驗(yàn)的一個解決方案
1.uview小程序必須用
// 如果需要兼容微信小程序,并且校驗(yàn)規(guī)則中含有方法等,只能通過setRules方法設(shè)置規(guī)則
this.$refs.form1.setRules(this.rules)
2.動態(tài)使用,v-for需要放在u-form下的view下面

3.綁定的校驗(yàn)規(guī)則rules和表單model下面放置一個同名數(shù)組,確保u-form能找到
form: {
dynamicAry: [],
},
myrules: {
dynamicAry: []
},
guigerules: {
name: [{
required: true,
message: '此為必填字段',
// blur和change事件觸發(fā)檢驗(yàn)
trigger: ['blur', 'change'],
},
{
min: 2,
max: 12,
message: '長度在2-12個字符之間'
},
],
price: [{
required: true,
message: '此為必填字段',
// blur和change事件觸發(fā)檢驗(yàn)
trigger: ['blur', 'change'],
},
{
validator: (rule, value, callback) => {
return uni.$u.test.amount(value);
},
message: '金額格式不對'
}
],
weight: {
type: 'number',
required: true,
message: '必須是數(shù)值',
trigger: ['change']
},
kucun: {
type: 'number',
required: true,
message: '必須是數(shù)值',
trigger: ['change']
}
},4.u-form-item中的表單必須改為 :prop="`dynamicAry.${index}.name`"

5.貼一下代碼
<u--form labelPosition="left" :model="form" :rules="myrules" ref="form2">
<view class="block" v-for=" (item,index) in form.dynamicAry" :key="index">
<view class="block__title">
<text style="flex: 1;">規(guī)格信息<text v-if="index>0">{{index}}</text></text>
<view style="width: 60px;" v-if="index>0">
<u-button type="error" icon="trash" text="刪除"></u-button>
</view>
</view>
<view class="block__content">
<u-form-item required label="規(guī)格名稱" labelWidth="80" :prop="`dynamicAry.${index}.name`" borderBottom>
<u--input v-model="item.name" border="none" placeholder="請?zhí)顚懸?guī)格名稱,如200g"></u--input>
</u-form-item>
<u-form-item required label="報(bào)價(jià)" labelWidth="80" :prop="`dynamicAry.${index}.price`" borderBottom>
<u--input type="digit" :v-model="item.price" border="none" placeholder="請輸入商品報(bào)價(jià)">
</u--input>
</u-form-item>
<u-form-item required label="重量" labelWidth="80" :prop="`dynamicAry.${index}.weight`" borderBottom>
<u--input type="number" :v-model="item.weight" disabledColor="#ffffff" placeholder="請輸入商品重量"
border="none"></u--input>
<text @click="showGuigeWeightUnit(index)">{{item.weightUnit}}</text>
<u-icon slot="right" name="arrow-right"></u-icon>
</u-form-item>
<u-form-item required label="庫存" labelWidth="80" :prop="`dynamicAry.${index}.kucun`" borderBottom>
<u--input type="number" :v-model="item.kucun" border="none" placeholder="請輸入商品庫存">
</u--input>
</u-form-item>
</view>
</view>
</u--form>6.在校驗(yàn)規(guī)則中加入觸發(fā)器 trigger: ['blur', 'change'],比如change,值變動后會觸發(fā)校驗(yàn)
7.重要的一點(diǎn),修改uview下的u-form,因?yàn)楦臑閿?shù)組后,const rule = this.formRules[child.prop];無法找到rule,如 child.prop=‘dynamicAry.0.name’,rule返回的是undefined,uniapp提供一個uni.$u.getProperty可以找到

修改后的validateField函數(shù):
// 對部分表單字段進(jìn)行校驗(yàn)
async validateField(value, callback, event = null) {
// $nextTick是必須的,否則model的變更,可能會延后于此方法的執(zhí)行
this.$nextTick(() => {
// 校驗(yàn)錯誤信息,返回給回調(diào)方法,用于存放所有form-item的錯誤信息
const errorsRes = [];
// 如果為字符串,轉(zhuǎn)為數(shù)組
value = [].concat(value);
// 歷遍children所有子form-item
this.children.map((child) => {
// 用于存放form-item的錯誤信息
const childErrors = [];
if (value.includes(child.prop)) {
// 獲取對應(yīng)的屬性,通過類似'a.b.c'的形式
const propertyVal = uni.$u.getProperty(
this.model,
child.prop
);
// 屬性鏈數(shù)組
const propertyChain = child.prop.split(".");
const propertyName =
propertyChain[propertyChain.length - 1];
//修改:將const改為let
let rule = this.formRules[child.prop];
//修改:鏈?zhǔn)绞菬o法通過上面的方式獲取的,改為下面的方式
if(!rule){
rule=uni.$u.getProperty(
this.formRules,
child.prop
);
}
// 如果不存在對應(yīng)的規(guī)則,直接返回,否則校驗(yàn)器會報(bào)錯
if (!rule) return;
// rule規(guī)則可為數(shù)組形式,也可為對象形式,此處拼接成為數(shù)組
const rules = [].concat(rule);
// 對rules數(shù)組進(jìn)行校驗(yàn)
for (let i = 0; i < rules.length; i++) {
const ruleItem = rules[i];
// 將u-form-item的觸發(fā)器轉(zhuǎn)為數(shù)組形式
const trigger = [].concat(ruleItem?.trigger);
// 如果是有傳入觸發(fā)事件,但是此form-item卻沒有配置此觸發(fā)器的話,不執(zhí)行校驗(yàn)操作
if (event && !trigger.includes(event)) continue;
// 實(shí)例化校驗(yàn)對象,傳入構(gòu)造規(guī)則
const validator = new Schema({
[propertyName]: ruleItem,
});
validator.validate({
[propertyName]: propertyVal,
},
(errors, fields) => {
if (uni.$u.test.array(errors)) {
errorsRes.push(...errors);
childErrors.push(...errors);
}
child.message =
childErrors[0]?.message ?? null;
}
);
}
}
});
// 執(zhí)行回調(diào)函數(shù)
typeof callback === "function" && callback(errorsRes);
});
},
// 校驗(yàn)全部數(shù)據(jù)7.動態(tài)添加
addGuige() {
this.form.dynamicAry.push({
name: '',
price: '',
weight: '',
weightUnit: '克',
kucun: '',
});
this.myrules.dynamicAry.push(this.guigerules);
// 如果需要兼容微信小程序,并且校驗(yàn)規(guī)則中含有方法等,只能通過setRules方法設(shè)置規(guī)則
//微信小程序:動態(tài)設(shè)置才能生效,每次添加都需要,動態(tài)綁定無效
this.$refs.form2.setRules(this.myrules)
},至此,解決完成,折騰了2個小時?。?!
這是一個比較簡單的解決方案!
補(bǔ)充知識:uView 循環(huán)表單校驗(yàn)
uView 默認(rèn)只能對 model 屬性的對象進(jìn)行校驗(yàn),對一些包含 列表 的表單來說,無法利用現(xiàn)有的功能實(shí)現(xiàn)校驗(yàn),這里推薦一種簡單的方式實(shí)現(xiàn) 列表 校驗(yàn)。
實(shí)現(xiàn)思路:由于 uniapp 修改源碼比較簡單,可以直接修改源碼添加擴(kuò)展功能。在 u-form-item 源碼中額外添加 prop:target,表示將要校驗(yàn)屬性所屬的目標(biāo)對象。
// u-form-item 源碼文件中
// 在 props 屬性中添加
target: {
?? ?type: Object,
},
// 找到 validation 方法
this.fieldValue = this.target[this.prop];
// 修改成如下
if (this.target) {
?? ?this.fieldValue = this.target[this.prop];
} else {
?? ?this.fieldValue = this.parent.model[this.prop];
}使用方式,這樣即可對列表每一項(xiàng)的 name 屬性進(jìn)行校驗(yàn)
<u-form-item v-for="item in list" prop="name" :target="item"></u-form-item>
總結(jié)
到此這篇關(guān)于uniapp插件uview下表單無法動態(tài)校驗(yàn)的問題解決的文章就介紹到這了,更多相關(guān)uniapp uview表單無法動態(tài)校驗(yàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
擁有一個屬于自己的javascript表單驗(yàn)證插件
這篇文章主要幫助大家擁有一個屬于自己的javascript表單驗(yàn)證插件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
屏蔽網(wǎng)頁右鍵復(fù)制和ctrl+c復(fù)制的js代碼
解決的方法就是直接把網(wǎng)頁保存下來然后刪掉下面這段js代碼,然后就可以正常用右鍵菜單,也可以通過設(shè)置瀏覽器的安全級別到最高級別來解決問題2013-01-01
JS導(dǎo)入本地json文件數(shù)據(jù)的三類方法舉例講解
作為一名剛?cè)胄械拈_發(fā)者,你可能會遇到需要在JavaScript中引用JSON文件的情況,下面這篇文章主要給大家介紹了關(guān)于JS導(dǎo)入本地json文件數(shù)據(jù)的三類方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
js實(shí)現(xiàn)點(diǎn)擊切換和自動播放的輪播圖
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)點(diǎn)擊切換和自動播放的輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

