您好,登錄后才能下訂單哦!
今天小編給大家分享一下vue動態添加表單validateField驗證功能如何實現的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
vue動態添加表單validateField驗證,代碼如下所示:
<template> <el-form ref="form" :model="form" :rules="rules" label-width="100px"> <div v-for="(input, index) in inputs" :key="index"> <el-form-item :label="'Name ' + (index + 1)" :prop="'name' + index"> <el-input v-model="input.name" @blur="validateName(index)"></el-input> </el-form-item> <el-form-item :label="'Age ' + (index + 1)" :prop="'age' + index"> <el-input v-model.number="input.age" @blur="validateAge(index)"></el-input> </el-form-item> </div> <el-button type="primary" @click="addInput">Add input</el-button> <el-button type="primary" @click="submitForm">Submit</el-button> </el-form> </template> <script> export default { data() { return { form: {}, inputs: [{name: '',age: ''}], rules: { name0: { required: true, pattern: /^[A-Za-z]+$/, message: 'Name can only contain letters', trigger: 'blur' }, age0: { required: true, pattern: /^\d+$/, message: 'Age can only contain numbers', trigger: 'blur' }, } } }, methods: { addInput() { const index = this.inputs.length this.inputs.push({ name: '', age: '' }) this.$set(this.form, `name${index}`, '') this.$set(this.form, `age${index}`, '') this.$set(this.rules, `name${index}`, { required: true, pattern: /^[A-Za-z]+$/, message: 'Name can only contain letters', trigger: 'blur' }) this.$set(this.rules, `age${index}`, { required: true, pattern: /^\d+$/, message: 'Age can only contain numbers', trigger: 'blur' }) }, validateName(index) { this.$refs.form.validateField(`name${index}`) }, validateAge(index) { this.$refs.form.validateField(`age${index}`) }, submitForm() { this.$refs.form.validate(valid => { if (valid) { // submit form } else { console.log('validation failed') } }) } } } </script>
可以使用 Element UI 的表單組件結合 Vue 的動態組件來實現動態添加多組輸入框,并對每個輸入框進行校驗。Element UI 提供了很多內置的校驗規則和提示信息,可以方便地應用到表單中。
首先,我們需要在 Vue 實例中聲明一個 inputs 數組來存儲每個輸入組的數據。在添加輸入組時,我們只需要向 inputs 數組中添加一個新的對象即可。
在模板中,我們使用 Element UI 的表單組件來渲染輸入框,并使用 v-for 指令循環渲染多組輸入框。在每個輸入框中,我們使用 v-model 指令將輸入值綁定到 inputs 數組中的數據屬性上。對于 name 和 age 輸入框,我們使用 pattern 規則來進行校驗,并在 rules 對象中提供了相應的錯誤提示信息。在 checkNameInput 和 checkAgeInput 方法中,我們調用 $refs.form.validateField 方法來手動觸發校驗,并將當前輸入對象作為參數傳遞進去。
最后,我們需要在 Vue 實例中聲明一個 form 對象來維護表單數據,并將 rules 對象傳遞給 el-form 組件的 rules 屬性。這樣,每次輸入框的值發生變化時,就會自動觸發 Element UI 的校驗機制,并顯示相應的錯誤提示信息。
補充:vue動態表單添加表單驗證
<el-form ref="conditionListForm" :rules="ConditionListRules" :model="scope.row.conditionListForm"> <el-table ref="conditionListDia" :data="scope.row.conditionListForm.conditionListDia" border :header-cell- size="mini" > <el-table-column label="限制條件名稱" align="center"> <el-table-column label="條件組合" align="center" size="mini" min-width="40" > <template slot-scope="scopeChild"> <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionRelation' " :rules="ConditionListRules.conditionRelation"> <el-select v-model="scopeChild.row.conditionRelation" :disabled="scope.row.conditionListForm.conditionListDia.length > 1 || scope.row.id > 0" size="mini" placeholder="請選擇" clearable @change="showconditionRelation(scopeChild.row.conditionRelation)" > <el-option v-for="item in compositionConditionList" :key="item.key" :disabled="item.baseDataStatus==='0'" :label="item.label" :value="item.key" /> </el-select> </el-form-item> </template> </el-table-column> <el-table-column label="條件分組" size="mini" align="center" min-width="30" > <template slot-scope="scopeChild"> <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].medalConditionClassname' " :rules="ConditionListRules.medalConditionClassname"> <el-select v-model="scopeChild.row.medalConditionClassname" :disabled="scope.row.id > 0" size="mini" placeholder="請選擇" clearable @change="defDataClick(scopeChild)" > <el-option v-for="item in fatherNameList" :key="item.id" :label="item.medalConditionClassname" :value="item.id" /> </el-select> </el-form-item> </template> </el-table-column> <el-table-column label="條件" size="mini" align="center" min-width="40" > <template slot-scope="scopeChild"> <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionId' " :rules="ConditionListRules.conditionId"> <el-select v-model="scopeChild.row.conditionId" size="mini" :disabled="scope.row.id > 0" placeholder="請選擇" clearable @change="getRelationship(scopeChild.row.conditionId,scopeChild.row.medalConditionClassnameList,scopeChild.row)" > <el-option v-for="item in scopeChild.row.medalConditionClassnameList" :key="item.defId" :label="item.medalConditionName" :value="item.defId" /> </el-select> </el-form-item> </template> </el-table-column> </el-table-column> <el-table-column label="運算關系" align="center" size="mini" min-width="50" > <template slot-scope="scopeChild"> <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionOperation' " :rules="ConditionListRules.conditionOperation"> <el-select v-model="scopeChild.row.conditionOperation" size="mini" placeholder="請選擇" :disabled="scope.row.id > 0" clearable @change="conditionOperationEmpty(scopeChild.row)" > <el-option v-for="item in scopeChild.row.conditionOperationValueList" :key="item.key" :disabled="item.baseDataStatus==='0'" :label="item.value" :value="item.key" /> </el-select> </el-form-item> </template> </el-table-column> <el-table-column label="值" align="center" size="mini" min-width="120" > <template slot-scope="scopeChild"> <el-form-item v-if="scopeChild.row.medalConditionControlType === '0'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionTrueValueList' " :rules="ConditionListRules.conditionTrueValueList"> <el-select v-model="scopeChild.row.conditionTrueValueList" multiple size="mini" clearable placeholder="請選擇" @change="conditionValueEmpty(scopeChild)" > <el-option v-for="item in scopeChild.row.conditionValueList" :key="item.value" :label="item.key" :value="item.value" /> </el-select> </el-form-item> <el-form-item v-if="scopeChild.row.medalConditionControlType == '1'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionValue' " :rules="ConditionListRules.conditionValue"> <el-input v-model="scopeChild.row.conditionValue" clearable placeholder="請輸入" align="center" size="mini" maxlength="60" show-word-limit @blur="conditionValueEmpty(scopeChild)" /> </el-form-item> <el-form-item v-if="scopeChild.row.medalConditionControlType == '2'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionTrueValueList' " :rules="ConditionListRules.conditionTrueValueList"> <el-date-picker v-model="scopeChild.row.conditionTrueValueList" type="daterange" range-separator="至" start-placeholder="開始日期" size="mini" end-placeholder="結束日期" /> </el-form-item> <el-form-item v-if="scopeChild.row.medalConditionControlType === '3'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionValue' " :rules="ConditionListRules.conditionValue"> <el-select v-model="scopeChild.row.conditionValue" size="mini" clearable placeholder="請選擇" @change="conditionValueEmpty(scopeChild)" > <el-option v-for="item in scopeChild.row.conditionValueList" :key="item.value" :label="item.key" :value="item.value" /> </el-select> </el-form-item> <el-form-item v-if="scopeChild.row.medalConditionControlType === '4'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionTrueValueList' " :rules="ConditionListRules.conditionTrueValueList"> <el-time-picker v-model="scopeChild.row.conditionTrueValueList" size="mini" is-range clearable range-separator="至" start-placeholder="開始時間" end-placeholder="結束時間" placeholder="選擇時間范圍" format="HH:mm" @change="conditionValueEmpty(scopeChild)" /> </el-form-item> <el-form-item v-if="scopeChild.row.medalConditionControlType == '5'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionValue' " :rules="ConditionListRules.conditionValue"> <el-input-number v-model="scopeChild.row.conditionValue" size="mini" :min="0" label="" @change="conditionValueEmpty(scope)" /> </el-form-item> </template> </el-table-column> <el-table-column label="操作" align="center" min-width="50" > <template slot-scope="scopeChild"> <el-button type="text" :disabled="scope.row.id > 0" @click="delCondition(scope.$index,scope.row.conditionListForm.conditionListDia,scopeChild.row,scopeChild.$index)">刪除</el-button> </template> </el-table-column> </el-table> </el-form> ConditionListRules: { conditionRelation: [ { required: true, message: '條件組合不能為空', trigger: 'change' } ], medalConditionClassname: [ { required: true, message: '條件分組不能為空', trigger: 'change' } ], conditionId: [ { required: true, message: '條件不能為空', trigger: 'change' } ], conditionOperation: [ { required: true, message: '運算關系不能為空', trigger: 'change' } ], conditionValue: [ { required: true, message: '值不能為空', trigger: 'change' } ], conditionTrueValueList: [ { required: true, message: '值不能為空', trigger: 'change' } ] },
主要就是prop的問題,prop要對應到數據。
以上就是“vue動態添加表單validateField驗證功能如何實現”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。