How to set up form validation using the dynamic addition or subtraction of form items in iviewui?
dynamically increase or decrease list items in this column, : rules=" {required: true, message: "Item" + item.index + "can not be empty", trigger:" blur"} "
can be validated normally. Modify the settings required: false
, type: "number"
, max: 200
on this validation rule, but how to set multiple validation rules on an input in a dynamic form?
if you want to verify that the input number is 0 to 100, it can be negative, and the decimal place is reserved. How to verify the dynamic increase or decrease of table items?
how to customize like a fixed form < Form ref= "info": model= "baseInfo": rules= "infoValidate": label-width= "200" class=" authentication-form ">"
like this : rules= "infoValidate"
?
first of all, thank you for your answers.
Custom validation of dynamic addition and decrease of form items has been tried, but it has not been successful. Maybe I wrote something wrong.
later, I carefully examined the document and found that pattern , verification rule rules
you can directly set regular pattern:/ ^ ((\ d\ d (.\ d)?) | 100) $/
, for example:
<FormItem :label="':'"
:prop="'items.' + index + '.score'+item.index"
:rules="{pattern:/^((\d\d(.\d)?)|100)$/, message: '0~1001', trigger: 'blur'}">
<Input type="text" v-model="item['score'+ item.index]" placeholder=""></Input>
</FormItem>
suggest solving the problem from the perspective of calculating attributes
For
this unconventional verification rule, it is recommended to write Custom Verification
.
<template>
<Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80">
<FormItem label="Password" prop="passwd">
<Input type="password" v-model="formCustom.passwd"></Input>
</FormItem>
<FormItem label="Confirm" prop="passwdCheck">
<Input type="password" v-model="formCustom.passwdCheck"></Input>
</FormItem>
<FormItem label="Age" prop="age">
<Input type="text" v-model="formCustom.age" number></Input>
</FormItem>
<FormItem>
<Button type="primary" @click="handleSubmit('formCustom')">Submit</Button>
<Button @click="handleReset('formCustom')" style="margin-left: 8px">Reset</Button>
</FormItem>
</Form>
</template>
<script>
export default {
data () {
const validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('Please enter your password'));
} else {
if (this.formCustom.passwdCheck !== '') {
//
this.$refs.formCustom.validateField('passwdCheck');
}
callback();
}
};
const validatePassCheck = (rule, value, callback) => {
if (value === '') {
callback(new Error('Please enter your password again'));
} else if (value !== this.formCustom.passwd) {
callback(new Error('The two input passwords do not match!'));
} else {
callback();
}
};
const validateAge = (rule, value, callback) => {
if (!value) {
return callback(new Error('Age cannot be empty'));
}
//
setTimeout(() => {
if (!Number.isInteger(value)) {
callback(new Error('Please enter a numeric value'));
} else {
if (value < 18) {
callback(new Error('Must be over 18 years of age'));
} else {
callback();
}
}
}, 1000);
};
return {
formCustom: {
passwd: '',
passwdCheck: '',
age: ''
},
ruleCustom: {
passwd: [
{ validator: validatePass, trigger: 'blur' }
],
passwdCheck: [
{ validator: validatePassCheck, trigger: 'blur' }
],
age: [
{ validator: validateAge, trigger: 'blur' }
]
}
}
},
methods: {
handleSubmit (name) {
this.$refs[name].validate((valid) => {
if (valid) {
this.$Message.success('Success!');
} else {
this.$Message.error('Fail!');
}
})
},
handleReset (name) {
this.$refs[name].resetFields();
}
}
}
</script>
rules can you put an array? Of course, there is a certain method, which is : rules= "{validator: validateInput, trigger: 'blur'}"
and then declare a validateInput method in data. For more information, please refer to iview's custom verification.