You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/form/demo/dynamic-rule.vue

102 lines
2.1 KiB

<cn>
#### 动态校验规则
根据不同情况执行不同的校验规则
</cn>
<us>
#### Dynamic Rules
Perform different check rules according to different situations.
</us>
<template>
<a-form :form="form">
<a-form-item
:labelCol="formItemLayout.labelCol"
:wrapperCol="formItemLayout.wrapperCol"
label='Name'
>
<a-input
v-decorator="[
'username',
{rules: [{ required: true, message: 'Please input your name' }]}
]"
placeholder='Please input your name'
/>
</a-form-item>
<a-form-item
:labelCol="formItemLayout.labelCol"
:wrapperCol="formItemLayout.wrapperCol"
label='Nickname'
>
<a-input
v-decorator="[
'nickname',
{rules: [{ required: checkNick, message: 'Please input your nickname' }]}
]"
placeholder='Please input your nickname'
/>
</a-form-item>
<a-form-item
:labelCol="formTailLayout.labelCol"
:wrapperCol="formTailLayout.wrapperCol"
>
<a-checkbox
:checked="checkNick"
@change="handleChange"
>
Nickname is required
</a-checkbox>
</a-form-item>
<a-form-item
:labelCol="formTailLayout.labelCol"
:wrapperCol="formTailLayout.wrapperCol"
>
<a-button type='primary' @click="check">Check</a-button>
</a-form-item>
</a-form>
</template>
<script>
const formItemLayout = {
labelCol: { span: 4 },
wrapperCol: { span: 8 },
}
const formTailLayout = {
labelCol: { span: 4 },
wrapperCol: { span: 8, offset: 4 },
}
export default {
data () {
return {
checkNick: false,
formItemLayout,
formTailLayout,
form: this.$form.createForm(this),
}
},
methods: {
check () {
this.form.validateFields(
(err) => {
if (!err) {
console.info('success')
}
},
)
},
handleChange (e) {
this.checkNick = e.target.checked
this.$nextTick(() => {
this.form.validateFields(['nickname'], { force: true })
})
},
},
}
</script>