1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| export default { data() { return { formConfig: [ { type: 'text', name: 'username', label: '用户名', required: true, placeholder: '请输入用户名' }, { type: 'email', name: 'email', label: '邮箱', required: true, placeholder: '请输入邮箱' }, { type: 'password', name: 'password', label: '密码', required: true, placeholder: '请输入密码' }, { type: 'select', name: 'role', label: '角色', options: [ { value: 'admin', label: '管理员' }, { value: 'user', label: '普通用户' } ] }, { type: 'checkbox', name: 'interests', label: '兴趣爱好', options: [ { value: 'reading', label: '阅读' }, { value: 'music', label: '音乐' }, { value: 'sports', label: '运动' } ] } ], formData: {}, formErrors: {} }; }, methods: { updateFormData(field, value) { this.$set(this.formData, field, value); this.validateField(field); }, validateField(field) { const config = this.formConfig.find(item => item.name === field); const value = this.formData[field]; if (config && config.required && (!value || value.length === 0)) { this.$set(this.formErrors, field, `${config.label}是必填项`); } else { this.$delete(this.formErrors, field); } }, submitForm() { this.formConfig.forEach(config => { this.validateField(config.name); }); if (Object.keys(this.formErrors).length === 0) { console.log('表单提交:', this.formData); } } }, template: ` <div class="dynamic-form"> <h2>动态表单</h2> <form @submit.prevent="submitForm"> <div v-for="config in formConfig" :key="config.name" class="form-group"> <label>{{ config.label }}</label> <!-- 文本输入框 --> <input v-if="config.type === 'text' || config.type === 'email' || config.type === 'password'" :type="config.type" :name="config.name" :placeholder="config.placeholder" :value="formData[config.name]" @input="updateFormData(config.name, $event.target.value)" :class="{ error: formErrors[config.name] }" /> <!-- 下拉选择框 --> <select v-else-if="config.type === 'select'" :name="config.name" :value="formData[config.name]" @change="updateFormData(config.name, $event.target.value)" :class="{ error: formErrors[config.name] }" > <option value="">请选择</option> <option v-for="option in config.options" :key="option.value" :value="option.value" > {{ option.label }} </option> </select> <!-- 复选框 --> <div v-else-if="config.type === 'checkbox'" class="checkbox-group"> <label v-for="option in config.options" :key="option.value" class="checkbox-item" > <input type="checkbox" :value="option.value" :checked="formData[config.name] && formData[config.name].includes(option.value)" @change="updateCheckbox(config.name, option.value, $event.target.checked)" /> {{ option.label }} </label> </div> <div v-if="formErrors[config.name]" class="error-message"> {{ formErrors[config.name] }} </div> </div> <button type="submit" :disabled="Object.keys(formErrors).length > 0"> 提交 </button> </form> </div> `, methods: { updateCheckbox(field, value, checked) { if (!this.formData[field]) { this.$set(this.formData, field, []); } const array = this.formData[field]; if (checked) { if (!array.includes(value)) { array.push(value); } } else { const index = array.indexOf(value); if (index > -1) { array.splice(index, 1); } } this.validateField(field); } } };
|