element form 校验数组每一项_温瞳_前端开发者

1.校验的数据结构如下:

 

 2.html结构

<el-dialog
                :title="title"
                :visible.sync="visable"
                width="40%"
                :before-close="cancel">
            <el-form label-width="80px" :model="formData" :rules="formDataRules" ref="formData" >
                <el-form-item label="Id" prop="id" v-if="formData.id">
                    {{formData.id}}
                </el-form-item>

                // begin ---------------------
                <div v-for="(item, index) in formData.test" :key="index">
                    <el-form-item label="test1" :prop="`test[${index}].test1`" :rules="{ required: true, message: '请输入test1', trigger: 'blur' }">
                        <el-input type="text" v-model="item.test1"></el-input>
                    </el-form-item>
                    <el-form-item label="test2" :prop="`test[${index}].test2`" :rules="{ required: true, message: '请输入test2', trigger: 'blur' }">
                        <el-input type="text" v-model="item.test2"></el-input>
                    </el-form-item>
                </div>
         // end ---------------------

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save('formData')">确 定</el-button>
            </div>
        </el-dialog>
<el-dialog
                :title="title"
                :visible.sync="visable"
                width="40%"
                :before-close="cancel">
            <el-form label-width="80px" :model="formData" :rules="formDataRules" ref="formData" >
                <el-form-item label="Id" prop="id" v-if="formData.id">
                    {{formData.id}}
                </el-form-item>

                // begin ---------------------
                <div v-for="(item, index) in formData.test" :key="index">
                    <el-form-item label="test1" :prop="`test[${index}].test1`" :rules="{ required: true, message: '请输入test1', trigger: 'blur' }">
                        <el-input type="text" v-model="item.test1"></el-input>
                    </el-form-item>
                    <el-form-item label="test2" :prop="`test[${index}].test2`" :rules="{ required: true, message: '请输入test2', trigger: 'blur' }">
                        <el-input type="text" v-model="item.test2"></el-input>
                    </el-form-item>
                </div>
         // end ---------------------

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save('formData')">确 定</el-button>
            </div>
        </el-dialog>

dialog
:title
title
:visible.sync
visable
width
40%
:before
cancel80pxformDataformDataRulesrefformDataIdidifformData.id
{{formData.id}}
// begin ———————for(item, index) in formData.testindextest1`test[${index}].test1`{ required: true, message: ‘请输入test1’, trigger: ‘blur’ }textitem.test1test2`test[${index}].test2`{ required: true, message: ‘请输入test2’, trigger: ‘blur’ }textitem.test2// end ———————footerclassdialog-footercancelprimarysave(‘formData’)

3.提交

save(formName: string) {
            const el: any = this.$refs[formName] ;
            el.validate((valid: any) => {
                if (valid) {
                    // todo 校验成功
                } else {
                    return false;
                }
            });
        }
save(formName: string) {
            const el: any = this.$refs[formName] ;
            el.validate((valid: any) => {
                if (valid) {
                    // todo 校验成功
                } else {
                    return false;
                }
            });
        }

string) {
constthis.$refs[formName] ;
el.validate((valid: any)
{
if (valid) {
// todo 校验成功else {
returnfalse;
}
});
}

4.效果

 

» 本文来自:前端开发者 » 《element form 校验数组每一项_温瞳_前端开发者》
» 本文链接地址:https://www.rokub.com/73602.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!