<el-form-item label="事故形态" pop="xingtai"> <el-select v-model="form.xingtai" @change="chaxingtai(form.xingtai)" placeholder="请选择事故形态" size="small" > <el-option label="碰撞" value="碰撞"></el-option> <el-option label="刮蹭" value="刮蹭"></el-option> <el-option label="碾压" value="碾压"></el-option> <el-option label="翻车" value="翻车"></el-option> <el-option label="坠车" value="坠车"></el-option> <el-option label="失火" value="失火"></el-option> <el-option label="撞固定物" value="撞固定物"></el-option> <el-option label="撞静止车辆" value="撞静止车辆"></el-option> <el-option label="其他" value="其他" ></el-option> </el-select> <div v-if="xingtaiqita" style="width: 200px"> <el-input type="textarea" v-model="form.select_1" size="small" clearable > ></el-input > </div> </el-form-item>以上代码写出表单验证
时间: 2024-02-14 18:15:13 浏览: 136
form表单的验证
你可以使用element-ui提供的表单验证来验证该字段是否为空。示例代码如下:
```
<template>
<el-form :model="form" :rules="rules" ref="form">
<el-form-item label="事故形态" prop="xingtai" pop="xingtai">
<el-select
v-model="form.xingtai"
@change="chaxingtai(form.xingtai)"
placeholder="请选择事故形态"
size="small"
>
<el-option label="碰撞" value="碰撞"></el-option>
<el-option label="刮蹭" value="刮蹭"></el-option>
<el-option label="碾压" value="碾压"></el-option>
<el-option label="翻车" value="翻车"></el-option>
<el-option label="坠车" value="坠车"></el-option>
<el-option label="失火" value="失火"></el-option>
<el-option label="撞固定物" value="撞固定物"></el-option>
<el-option label="撞静止车辆" value="撞静止车辆"></el-option>
<el-option label="其他" value="其他"></el-option>
</el-select>
<div v-if="xingtaiqita" style="width: 200px">
<el-input
type="textarea"
v-model="form.select_1"
size="small"
clearable
></el-input>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">提交</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
form: {
xingtai: '',
select_1: ''
},
xingtaiqita: false,
rules: {
xingtai: [
{
required: true,
message: '请选择事故形态',
trigger: 'change'
}
]
}
}
},
methods: {
chaxingtai(value) {
if (value === '其他') {
this.xingtaiqita = true
} else {
this.xingtaiqita = false
}
},
submitForm() {
this.$refs.form.validate(valid => {
if (valid) {
// 表单验证通过,提交表单
console.log('submit')
} else {
// 表单验证失败
console.log('error submit')
return false
}
})
}
}
}
</script>
```
在上面的代码中,我们给`el-form-item`组件设置了`prop`属性为`xingtai`,这是为了在验证时使用。在`rules`中,我们设置了`xingtai`字段必填,如果该字段为空,则会提示用户选择事故形态。在`submitForm`方法中,我们调用了`this.$refs.form.validate()`方法来进行表单验证,如果验证成功,则可以提交表单,否则会提示用户错误信息。
阅读全文