ant-design-vue/components/form/demo/test.vue

81 lines
2.2 KiB
Vue
Raw Normal View History

2018-06-22 13:35:14 +00:00
<template>
2018-06-23 09:17:45 +00:00
<a-form layout='inline' @submit="handleSubmit" :autoFormCreate="(form)=>{this.form = form}">
<template v-if="form">
2018-06-22 13:35:14 +00:00
<a-form-item
2018-06-23 09:17:45 +00:00
:validateStatus="userNameError() ? 'error' : ''"
:help="userNameError() || ''"
fieldDecoratorId="userName"
:fieldDecoratorOptions="{rules: [{ required: true, message: 'Please input your username!' }]}"
2018-06-22 13:35:14 +00:00
>
2018-06-23 09:17:45 +00:00
<a-input placeholder='Username'>
<a-icon slot="prefix" type='user' style="color:rgba(0,0,0,.25)"/>
</a-input>
2018-06-22 13:35:14 +00:00
</a-form-item>
<a-form-item
2018-06-23 09:17:45 +00:00
:validateStatus="passwordError() ? 'error' : ''"
:help="passwordError() || ''"
fieldDecoratorId="password"
:fieldDecoratorOptions="{rules: [{ required: true, message: 'Please input your Password!' }]}"
2018-06-22 13:35:14 +00:00
>
2018-06-23 09:17:45 +00:00
<a-input type='password' placeholder='Password'>
<a-icon slot="prefix" type='lock' style="color:rgba(0,0,0,.25)"/>
</a-input>
2018-06-22 13:35:14 +00:00
</a-form-item>
2018-06-23 09:17:45 +00:00
<a-form-item>
<a-button
type='primary'
htmlType='submit'
:disabled="hasErrors(form.getFieldsError())"
>
Log in
2018-06-22 13:35:14 +00:00
</a-button>
</a-form-item>
2018-06-23 09:17:45 +00:00
</template>
</a-form>
2018-06-22 13:35:14 +00:00
</template>
<script>
2018-06-23 09:17:45 +00:00
function hasErrors (fieldsError) {
return Object.keys(fieldsError).some(field => fieldsError[field])
}
2018-06-22 13:35:14 +00:00
export default {
data () {
return {
2018-06-23 09:17:45 +00:00
hasErrors,
form: null,
2018-06-22 13:35:14 +00:00
}
},
2018-06-23 09:17:45 +00:00
mounted () {
},
watch: {
form (val) {
this.$nextTick(() => {
// To disabled submit button at the beginning.
this.form.validateFields()
})
},
},
2018-06-22 13:35:14 +00:00
methods: {
2018-06-23 09:17:45 +00:00
// Only show error after a field is touched.
userNameError () {
const { getFieldError, isFieldTouched } = this.form
return isFieldTouched('userName') && getFieldError('userName')
},
// Only show error after a field is touched.
passwordError () {
const { getFieldError, isFieldTouched } = this.form
return isFieldTouched('password') && getFieldError('password')
},
handleSubmit (e) {
2018-06-22 13:35:14 +00:00
e.preventDefault()
this.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values)
}
})
},
},
}
</script>