今天想寫一個(gè)表單校驗(yàn)用戶有沒有填寫內(nèi)容
官方文檔是這樣寫的
<template>
<i-form v-ref:form-validate :model="formValidate" :rules="ruleValidate" :label-width="80">
<Form-item label="姓名" prop="name">
<i-input :value.sync="formValidate.name" placeholder="請(qǐng)輸入姓名"></i-input>
</Form-item>
<Form-item label="郵箱" prop="mail">
<i-input :value.sync="formValidate.mail" placeholder="請(qǐng)輸入郵箱"></i-input>
</Form-item>
<Form-item label="城市" prop="city">
<i-select :model.sync="formValidate.city" placeholder="請(qǐng)選擇所在地">
<i-option value="beijing">北京市</i-option>
<i-option value="shanghai">上海市</i-option>
<i-option value="shenzhen">深圳市</i-option>
</i-select>
</Form-item>
<Form-item label="選擇日期">
<Row>
<i-col span="11">
<Form-item prop="date">
<Date-picker type="date" placeholder="選擇日期" :value.sync="formValidate.date"></Date-picker>
</Form-item>
</i-col>
<i-col span="2" style="text-align: center">-</i-col>
<i-col span="11">
<Form-item prop="time">
<Time-picker type="time" placeholder="選擇時(shí)間" :value.sync="formValidate.time"></Time-picker>
</Form-item>
</i-col>
</Row>
</Form-item>
<Form-item label="性別" prop="gender">
<Radio-group :model.sync="formValidate.gender">
<Radio value="male">男</Radio>
<Radio value="female">女</Radio>
</Radio-group>
</Form-item>
<Form-item label="愛好" prop="interest">
<Checkbox-group :model.sync="formValidate.interest">
<Checkbox value="吃飯"></Checkbox>
<Checkbox value="睡覺"></Checkbox>
<Checkbox value="跑步"></Checkbox>
<Checkbox value="看電影"></Checkbox>
</Checkbox-group>
</Form-item>
<Form-item label="介紹" prop="desc">
<i-input :value.sync="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="請(qǐng)輸入..."></i-input>
</Form-item>
<Form-item>
<i-button type="primary" @click="handleSubmit('formValidate')">提交</i-button>
<i-button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</i-button>
</Form-item>
</i-form></template><script>
export default {
data () { return {
formValidate: {
name: '',
mail: '',
city: '',
gender: '',
interest: [],
date: '',
time: '',
desc: ''
},
ruleValidate: {
name: [
{ required: true, message: '姓名不能為空', trigger: 'blur' }
],
mail: [
{ required: true, message: '郵箱不能為空', trigger: 'blur' },
{ type: 'email', message: '郵箱格式不正確', trigger: 'blur' }
],
city: [
{ required: true, message: '請(qǐng)選擇城市', trigger: 'change' }
],
gender: [
{ required: true, message: '請(qǐng)選擇性別', trigger: 'change' }
],
interest: [
{ required: true, type: 'array', min: 1, message: '至少選擇一個(gè)愛好', trigger: 'change' },
{ type: 'array', max: 2, message: '最多選擇兩個(gè)愛好', trigger: 'change' }
],
date: [
{ required: true, type: 'date', message: '請(qǐng)選擇日期', trigger: 'change' }
],
time: [
{ required: true, type: 'date', message: '請(qǐng)選擇時(shí)間', trigger: 'change' }
],
desc: [
{ required: true, message: '請(qǐng)輸入個(gè)人介紹', trigger: 'blur' },
{ type: 'string', min: 20, message: '介紹不能少于20字', trigger: 'blur' }
]
}
}
},
methods: {
handleSubmit (name) { this.$refs[name].validate((valid) => { if (valid) { this.$Message.success('提交成功!');
} else { this.$Message.error('表單驗(yàn)證失敗!');
}
})
},
handleReset (name) { this.$refs[name].resetFields();
}
}
}</script>
全部復(fù)制下來會(huì)發(fā)現(xiàn)根本沒有用
但是好在這個(gè)東西他開源
于是我去github上看了下源代碼
發(fā)現(xiàn)作者在源代碼中是這樣寫的
我也照葫蘆畫瓢 結(jié)果成功了
貼上代碼
<template>
<div>
<Row>
<Form ref="formValidate1" :model="formValidate" :rules="ruleValidate" :label-width="80">
<FormItem label="Name" prop="name">
<i-input v-model="formValidate.name" placeholder="Enter your name"></i-input>
</FormItem>
</Form>
</Row>
</div>
</template>
<script>
export default {
data() {
return {
formValidate: {
name: ''
},
ruleValidate: {
name: [{
required: true,
message: 'The name cannot be empty',
trigger: 'blur'
}]
}
}
}
}
</script>
<style scoped>
</style>