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

102 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
2018-06-24 13:18:45 +00:00
:labelCol="formItemLayout.labelCol"
:wrapperCol="formItemLayout.wrapperCol"
2018-06-23 09:17:45 +00:00
label='Name'
>
<a-input
v-decorator="[
'username',
{rules: [{ required: true, message: 'Please input your name' }]}
]"
placeholder='Please input your name'
/>
2018-06-23 09:17:45 +00:00
</a-form-item>
<a-form-item
2018-06-24 13:18:45 +00:00
:labelCol="formItemLayout.labelCol"
:wrapperCol="formItemLayout.wrapperCol"
2018-06-23 09:17:45 +00:00
label='Nickname'
>
<a-input
v-decorator="[
'nickname',
{rules: [{ required: checkNick, message: 'Please input your nickname' }]}
]"
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
:labelCol="formTailLayout.labelCol"
:wrapperCol="formTailLayout.wrapperCol"
>
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
:labelCol="formTailLayout.labelCol"
:wrapperCol="formTailLayout.wrapperCol"
>
2018-06-23 09:17:45 +00:00
<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,
form: this.$form.createForm(this),
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