ant-design-vue/components/form/demo/dynamic-rule.vue

85 lines
1.8 KiB
Vue
Raw Normal View History

2018-05-06 10:32:40 +00:00
<cn>
#### 动态校验规则
根据不同情况执行不同的校验规则
</cn>
<us>
#### Dynamic Rules
Perform different check rules according to different situations.
</us>
2018-05-08 03:20:07 +00:00
2018-06-23 09:17:45 +00:00
<template>
<a-form :autoFormCreate="(form)=>{this.form = form}">
<a-form-item
:formItemLayout="formItemLayout"
label='Name'
fieldDecoratorId="username"
:fieldDecoratorOptions="{rules: [{ required: true, message: 'Please input your name' }]}"
>
<a-input placeholder='Please input your name' />
</a-form-item>
<a-form-item
:formItemLayout="formItemLayout"
label='Nickname'
fieldDecoratorId="nickname"
:fieldDecoratorOptions="{rules: [{ required: checkNick, message: 'Please input your nickname' }]}"
>
<a-input placeholder='Please input your nickname' />
</a-form-item>
<a-form-item :formTailLayout="formTailLayout">
<a-checkbox
:checked="checkNick"
@change="handleChange"
>
Nickname is required
</a-checkbox>
</a-form-item>
<a-form-item :formTailLayout="formTailLayout">
<a-button type='primary' @click="check">Check</a-button>
</a-form-item>
</a-form>
</template>
2018-05-06 10:32:40 +00:00
2018-06-23 09:17:45 +00:00
<script>
2018-05-06 10:32:40 +00:00
const formItemLayout = {
labelCol: { span: 4 },
wrapperCol: { span: 8 },
}
const formTailLayout = {
labelCol: { span: 4 },
wrapperCol: { span: 8, offset: 4 },
}
2018-06-23 09:17:45 +00:00
export default {
2018-05-06 10:32:40 +00:00
data () {
return {
checkNick: false,
2018-06-23 09:17:45 +00:00
formItemLayout,
formTailLayout,
2018-05-06 10:32:40 +00:00
}
},
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>
2018-05-08 03:20:07 +00:00
2018-05-06 10:32:40 +00:00
2018-06-23 09:17:45 +00:00