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

107 lines
2.1 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 :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',
{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',
{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>
2018-06-24 13:18:45 +00:00
<a-form-item
2019-02-01 09:23:00 +00:00
:label-col="formTailLayout.labelCol"
:wrapper-col="formTailLayout.wrapperCol"
2018-06-24 13:18:45 +00:00
>
2018-06-23 09:17:45 +00:00
<a-checkbox
:checked="checkNick"
@change="handleChange"
>
Nickname is required
</a-checkbox>
</a-form-item>
2018-06-24 13:18:45 +00:00
<a-form-item
2019-02-01 09:23:00 +00:00
:label-col="formTailLayout.labelCol"
:wrapper-col="formTailLayout.wrapperCol"
2018-06-24 13:18:45 +00:00
>
2019-02-01 09:23:00 +00:00
<a-button
type="primary"
@click="check"
>
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 {
2018-05-06 10:32:40 +00:00
data () {
return {
checkNick: false,
2018-06-23 09:17:45 +00:00
formItemLayout,
formTailLayout,
form: this.$form.createForm(this),
2019-01-12 03:33:27 +00:00
};
2018-05-06 10:32:40 +00:00
},
methods: {
check () {
this.form.validateFields(
(err) => {
if (!err) {
2019-01-12 03:33:27 +00:00
console.info('success');
2018-05-06 10:32:40 +00:00
}
},
2019-01-12 03:33:27 +00:00
);
2018-05-06 10:32:40 +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>
2018-05-08 03:20:07 +00:00
2018-05-06 10:32:40 +00:00
2018-06-23 09:17:45 +00:00