antdeign vue 动态添加输入框并带校验

<a-card style="margin-top: 24px;" :title="$t('activity.activityRule')" class="rulecard" v-if="activityData.activityType == 4">
					<a-radio-group v-model="ruleType" @change="changeRule">
						<a-radio :value="1">{{$t('activity.ladderReduce')}}</a-radio>
						<a-radio :value="2" style="margin-left: 20px">{{$t('activity.everyReduce')}}</a-radio>
					</a-radio-group>
					<a-form-model  :model="dynamicValidateForm" style="margin-top:15px" ref="dynamicValidateRef">
							<template v-if="ruleType == 2">
								<span  style="line-height:40px">{{$t('activity.everyfull')}}</span>
								<a-form-model-item  prop="domain2.value1"
								>
								<a-input-number :min="0" class="numberStyle" v-model="dynamicValidateForm.domain2.value1"/>
								</a-form-model-item>
								<span  style="line-height:40px">{{$t('activity.reduce')}}</span>
								<a-form-model-item  prop="domain2.value2" key="only"
								:rules="{
									required: true,
									message:$t('activity.notEmpty'),
									trigger: 'blur',
								}"
								>
								<a-input-number  class="numberStyle" v-model="dynamicValidateForm.domain2.value2"/>
								</a-form-model-item>
							</template>
							<template v-else>
								<template v-for="(domain, index) in dynamicValidateForm.domains" >
									<span :key="domain.index" style="line-height:40px">{{$t('activity.full')}}</span>
									<a-form-model-item :key="domain.index" :prop="'domains.' + index + '.value1'"
									:rules="{
										required: true,
										validator:checkRule1,
										trigger: 'blur',
									}"
									>
									<a-input-number :min="index == 0 ? 0 : 1" class="numberStyle" v-model="domain.value1"/>
									</a-form-model-item>
									<span :key="domain.index" style="line-height:40px">{{$t('activity.reduce')}}</span>
									<a-form-model-item :key="domain.index" :prop="'domains.' + index + '.value2'"
									:rules="{
										required: true,
										validator:checkRule2,
										trigger: 'blur',
									}"
									>
									<a-input-number class="numberStyle" v-model="domain.value2"/>
									</a-form-model-item>
									<a-icon :key="domain.index"
									v-if="dynamicValidateForm.domains.length > 1"
									type="minus-circle-o"
									class="dynamic-delete-button"
									@click="removeDomain(domain)"
									/>
									<br :key="domain.index" >
								</template>
								<a-button icon="plus" class="addBtd" style="width:380px" @click="addRule">{{$t('activity.newFullreduce')}}</a-button>
							</template>
					</a-form-model>	
					
			</a-card>

主要的坑就是 页面不显示rule校验结果,是因为要在a-form-model-item和span标签外面套个template。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要对ant design vue输入框进行验证,可以使用内置的表单验证规则和自定义验证规则。 首先,在模板中使用`<a-form-item>`包装输入框,并指定`name`属性和`rules`属性。`name`属性用于唯一标识该表单项,`rules`属性用于定义验证规则。 ```html <template> <a-form :form="form"> <a-form-item label="用户名" name="username" :rules="usernameRules"> <a-input v-model="username" /> </a-form-item> </a-form> </template> ``` 接下来,定义验证规则。可以使用内置的验证规则,如`required`、`min`、`max`等,也可以自定义验证函数。 ```javascript <script> export default { data() { return { form: this.$form.createForm(this), username: '' }; }, computed: { usernameRules() { return [ { required: true, message: '请输入用户名' }, { min: 3, max: 10, message: '用户名长度在3到10之间' }, { validator: this.checkUsername } ]; } }, methods: { checkUsername(rule, value, callback) { if (!/^[a-zA-Z0-9]+$/.test(value)) { callback(new Error('用户名只能包含字母和数字')); } else { callback(); } } } }; </script> ``` 在上面的例子中,`usernameRules`是一个计算属性,它返回一个数组,包含了三个验证规则。第一个规则要求该输入框不能为空,第二个规则要求该输入框的长度在3到10之间,第三个规则是自定义的验证函数,要求用户名只能包含字母和数字。 最后,需要在提交表单时执行验证。可以使用`this.form.validateFields`方法来执行验证,如果验证通过,则执行提交操作。 ```javascript <script> export default { data() { return { form: this.$form.createForm(this), username: '' }; }, methods: { submitForm() { this.form.validateFields((errors, values) => { if (errors) { console.log('表单验证不通过', errors); } else { console.log('表单验证通过', values); } }); } } }; </script> ``` 这样,我们就可以对ant design vue输入框进行验证了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值