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

87 lines
2.0 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-06-23 09:17:45 +00:00
<template>
<a-form :form="form">
2018-06-23 09:17:45 +00:00
<a-form-item
2019-02-01 09:23:00 +00:00
:label-col="formItemLayout.labelCol"
:wrapper-col="formItemLayout.wrapperCol"
label="Name"
2018-06-23 09:17:45 +00:00
>
<a-input
v-decorator="[
'username',
2019-09-28 12:45:07 +00:00
{ rules: [{ required: true, message: 'Please input your name' }] },
]"
2019-02-01 09:23:00 +00:00
placeholder="Please input your name"
/>
2018-06-23 09:17:45 +00:00
</a-form-item>
<a-form-item
2019-02-01 09:23:00 +00:00
:label-col="formItemLayout.labelCol"
:wrapper-col="formItemLayout.wrapperCol"
label="Nickname"
2018-06-23 09:17:45 +00:00
>
<a-input
v-decorator="[
'nickname',
2019-09-28 12:45:07 +00:00
{ rules: [{ required: checkNick, message: 'Please input your nickname' }] },
]"
2019-02-01 09:23:00 +00:00
placeholder="Please input your nickname"
/>
2018-06-23 09:17:45 +00:00
</a-form-item>
2019-09-28 12:45:07 +00:00
<a-form-item :label-col="formTailLayout.labelCol" :wrapper-col="formTailLayout.wrapperCol">
<a-checkbox :checked="checkNick" @change="handleChange">
2018-06-23 09:17:45 +00:00
Nickname is required
</a-checkbox>
</a-form-item>
2019-09-28 12:45:07 +00:00
<a-form-item :label-col="formTailLayout.labelCol" :wrapper-col="formTailLayout.wrapperCol">
<a-button type="primary" @click="check">
2019-02-01 09:23:00 +00:00
Check
</a-button>
2018-06-23 09:17:45 +00:00
</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 },
2019-01-12 03:33:27 +00:00
};
2018-05-06 10:32:40 +00:00
const formTailLayout = {
labelCol: { span: 4 },
wrapperCol: { span: 8, offset: 4 },
2019-01-12 03:33:27 +00:00
};
2018-06-23 09:17:45 +00:00
export default {
2019-09-28 12:45:07 +00:00
data() {
2018-05-06 10:32:40 +00:00
return {
checkNick: false,
2018-06-23 09:17:45 +00:00
formItemLayout,
formTailLayout,
2019-04-20 06:19:13 +00:00
form: this.$form.createForm(this, { name: 'dynamic_rule' }),
2019-01-12 03:33:27 +00:00
};
2018-05-06 10:32:40 +00:00
},
methods: {
2019-09-28 12:45:07 +00:00
check() {
this.form.validateFields(err => {
if (!err) {
console.info('success');
}
});
2018-05-06 10:32:40 +00:00
},
2019-09-28 12:45:07 +00:00
handleChange(e) {
2019-01-12 03:33:27 +00:00
this.checkNick = e.target.checked;
2018-05-06 10:32:40 +00:00
this.$nextTick(() => {
2019-01-12 03:33:27 +00:00
this.form.validateFields(['nickname'], { force: true });
});
2018-05-06 10:32:40 +00:00
},
},
2019-01-12 03:33:27 +00:00
};
2018-05-06 10:32:40 +00:00
</script>