1、表单验证介绍
下面来讲解iviewui表单验证的实现,下面上示例代码:
<template>
<Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
<FormItem prop="user"><!--prop属性不能少-->
<Input type="text" v-model="formInline.user" placeholder="Username">
<Icon type="ios-person-outline" slot="prepend"></Icon>
</Input>
<--formInline.user关联的变量值-->
</FormItem>
<FormItem prop="password">
<Input type="password" v-model="formInline.password" placeholder="Password">
<Icon type="ios-lock-outline" slot="prepend"></Icon>
</Input>
</FormItem>
<FormItem>
<Button type="primary" @click="handleSubmit('formInline')">Signin</Button>
</FormItem>
</Form>
</template>
<script>
export default {
data () {
return {
formInline: {
user: '',
password: ''
},
ruleInline: {
user: [
{ required: true, message: 'Please fill in the user name', trigger: 'blur' }
],
password: [
{ required: true, message: 'Please fill in the password.', trigger: 'blur' },
{ type: 'string', min: 6, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
]
}
}
},
methods: {
handleSubmit(name) {
this.$refs[name].validate((valid) => {
if (valid) {
this.$Message.success('Success!');
} else {
this.$Message.error('Fail!');
}
})
}
}
}
</script>
错误演示效果如下:
表单项的props
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
prop | 对应表单域 model 里的字段 | String | - |
label | 标签文本 | String | - |
label-width | 表单域标签的的宽度 | Number | - |
label-for | 指定原生的 label 标签的 for 属性,配合控件的 element-id 属性,可以点击 label 时聚焦控件。 |
String | - |
required | 是否必填,如不设置,则会根据校验规则自动生成 | Boolean | - |
rules | 表单验证规则 | Object | Array | - |
error | 表单域验证错误信息, 设置该值会使表单验证状态变为error,并显示该错误信息 | String | - |
show-message | 是否显示校验错误信息 | Boolean | true |
表单的props
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
model | 表单数据对象 | Object | - |
rules | 表单验证规则,具体配置查看 async |