iview 动态校验表单- 多个输入框分别校验

本文展示了如何在使用iview时,通过调整模板结构,为多个动态生成的Input输入框分别设置独立的校验规则,确保每个输入框在提交时能进行单独的验证,从而提高表单数据的准确性和完整性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
    <Form ref="formDynamic" :model="formDynamic" :label-width="80" style="width: 300px">
        <FormItem
                v-for="(item, index) in formDynamic.items"
                v-if="item.status"
                :key="index"
                :label="'Item ' + item.index"
                :prop="'items.' + index + '.value'"
                :rules="{required: true, message: 'Item ' + item.index +' can not be empty', trigger: 'blur'}">
            <Row>
                <Col span="18">
                    <Input type="text" v-model="item.value" placeholder="Enter something..."></Input>
                </Col>
                <Col span="4" offset="1">
                    <Button @click="handleRemove(index)">Delete</Button>
                </Col>
            </Row>
        </FormItem>
        <FormItem>
            <Row>
                <Col span="12">
                    <Button type="dashed" long @click="handleAdd" icon="md-add">Add item</Button>
                </Col>
            </Row>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formDynamic')">Submit</Button>
            <Button @click="handleReset('formDynamic')" style="margin-left: 8px">Reset</Button>
        </FormItem>
    </Form>
</template>

iview自带用循环来动态校验输入框,但循环多个input会同时加上校验规则。

可以改为这样

<template>
    <Form ref="formDynamic" :model="formDynamic" :label-width="80" style="width: 300px">
        <FormItem
                v-for="(item, index) in formDynamic.items"
                v-if="item.status"
                :key="index"
                :label="'Item ' + item.index">
            <Row>
                <Col span="18">
                  <FormItem
                    :prop="'items.' + index + '.value'"
                    :rules="{required: true, message: 'Item1 不能为空', trigger: 'blur'}">
                    <Input type="text" v-model="item.value" placeholder="Enter something..."></Input>
                  </FormItem>
                </Col>
                <Col span="18" style="margin-top:20px">
                    <FormItem
                    :prop="'items.' + index + '.value1'"
                    :rules="{required: true, message: 'Item2 也不能为空', trigger: 'blur'}">
                    <Input type="text" v-model="item.value1" placeholder="Enter something..."></Input>
                  </FormItem>
                </Col>
                <Col span="18" style="margin-top:20px">
                    <Input type="text" v-model="item.value2" placeholder="Enter something..."></Input>
                </Col>
                <Col span="4" offset="1">
                    <Button @click="handleRemove(index)">Delete</Button>
                </Col>
            </Row>
        </FormItem>
        <FormItem>
            <Row>
                <Col span="12">
                    <Button type="dashed" long @click="handleAdd" icon="md-add">Add item</Button>
                </Col>
            </Row>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formDynamic')">Submit</Button>
            <Button @click="handleReset('formDynamic')" style="margin-left: 8px">Reset</Button>
        </FormItem>
    </Form>
</template>

将 :prop="  "
     :rules="  "

提取出来,单独添加到不同的输入框

 

以到达分别动态校验效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值