vue @vuelidate父子組件綁定注意事項(xiàng)
vue @vuelidate父子組件綁定注意
如果父子組件都有驗(yàn)證的,不能這樣寫
const V2$ = useVuelidate(rules, formdata)
const submitBtn = async () => {
const V2$ = useVuelidate(rules, formdata)如果這樣寫的話,在父組件驗(yàn)證時(shí),也會(huì)把子組件的驗(yàn)證代上。
正確寫法
const submitBtn = async () => {
const V2$ = useVuelidate(rules, formdata)
let validate =await V2$.value.$validate()要在自己驗(yàn)證的方法內(nèi)部進(jìn)行綁定
Vuelidate表單驗(yàn)證規(guī)則
Vuelidate中的必填驗(yàn)證規(guī)則
用到的技術(shù) vue + quasar + vuelidate(網(wǎng)址)
引用方法如下:
Vue.use(window.vuelidate.default)
var minLength = window.validators.minLength
var required = window.validators.required
var requiredIf = window.validators.requiredIf
var email = window.validators.email
var helpers = window.validators.helpers
validations: {
baseInfoForm: {
mobilePhone:{ required: required },
col19:{ required: required },
kitchenTime:{ required: requiredIf(function(baseInfoForm) {
return baseInfoForm.col19 == 'Y'
//當(dāng)col19的值為Y時(shí),才有kitchenTime的驗(yàn)證
})}
}
一個(gè)簡單的例子:
<q-field
icon="stay_primary_portrait"
:error="readonly == false? $v.baseInfoForm.mobilePhone.$error: false"
error-label="手機(jī)號(hào)碼不能為空" //警告提示信息
class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
<q-input
:readonly="readonly"
@blur="$v.baseInfoForm.mobilePhone.$touch"
:float-label="$t('手機(jī)號(hào)碼')"
type="number"
v-model="baseInfoForm.mobilePhone"></q-input>
</q-field>
<q-field
v-if="baseInfoForm.col19 == 'Y' "
icon="access_time"
:error="readonly == false? $v.baseInfoForm.kitchenTime.$error: false"
error-label="進(jìn)入廚電行業(yè)時(shí)間不能為空"
class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
<q-datetime
type="date"
format="YYYY-MM-DD"
format-model="string"
:float-label="$t('若是,進(jìn)入廚電行業(yè)時(shí)間')"
@blur="$v.baseInfoForm.kitchenTime.$touch"
v-model="baseInfoForm.kitchenTime"></q-datetime>
</q-field>效果如下:


具體代碼實(shí)現(xiàn)如下:
<div id="q-supplierBaseInfo" v-cloak>
<q-field
icon="stay_primary_portrait"
:error="readonly == false? $v.baseInfoForm.mobilePhone.$error: false"
error-label="手機(jī)號(hào)碼不能為空"
class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
<q-input
:readonly="readonly"
@blur="$v.baseInfoForm.mobilePhone.$touch"
:float-label="$t('手機(jī)號(hào)碼')"
type="number"
v-model="baseInfoForm.mobilePhone"></q-input>
</q-field>
<q-field
icon="work"
:error="readonly == false? $v.baseInfoForm.col19.$error: false"
error-label="是否為供應(yīng)商不能為空"
class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
<q-select
@blur="$v.baseInfoForm.col19.$touch"
:float-label="$t('是否為供應(yīng)商')"
:options="col19Options"
v-model="baseInfoForm.col19"></q-select>
</q-field>
</div>
<script>
// quasar components use
Quasar.i18n.set(Quasar.i18n[LANGUGE])
Vue.use(window.vuelidate.default)
var minLength = window.validators.minLength
var required = window.validators.required
var requiredIf = window.validators.requiredIf
var helpers = window.validators.helpers
var supplierInvite = new Vue({
el: '#q-supplierBaseInfo',
i18n: i18n,
data: function() {
return {
col19Options:[
{
label: '是',
value: 'Y'
},
{
label: '否',
value: 'N'
}
],
baseInfoForm: { col19:''}
},
validations: {
baseInfoForm: {
col19:{ required: required },
kitchenTime:{ required: requiredIf(function(baseInfoForm) {
return baseInfoForm.col19 == 'Y'
})}
}
}
})
</script>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vxe-table動(dòng)態(tài)渲染列,刷新列寬的方式
這篇文章主要介紹了vxe-table動(dòng)態(tài)渲染列,刷新列寬的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Vue.js?中?LocalStorage?與?SessionStorage操作示例最佳實(shí)踐
文章介紹了LocalStorage和SessionStorage的區(qū)別,包括生命周期、作用域、數(shù)據(jù)類型限制、基礎(chǔ)操作和進(jìn)階應(yīng)用場景,同時(shí),還討論了最佳實(shí)踐、注意事項(xiàng)和常見錯(cuò)誤及其解決方案,感興趣的朋友跟隨小編一起看看吧2026-01-01
vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用)
這篇文章主要介紹了vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Vue中的watch是什么以及watch和computed的區(qū)別
這篇文章主要介紹了Vue中的watch是什么以及watch和computed的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Vue使用ElementUI動(dòng)態(tài)修改table單元格背景顏色或文本顏色
本文主要介紹了Vue使用ElementUI動(dòng)態(tài)修改table單元格背景顏色或文本顏色,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

