<el-form :model="formData" :rules="rules" :inline="true" ref="subimtForm" label-width="100px"> <el-form-item label="开始时间" required prop="startTime" style="width: 100%"> <el-date-picker v-model="formData.startTime" type="datetime" format="YYYY/MM/DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始日期" style="width: 100%" /> </el-form-item> <el-form-item label="结束日期" required prop="endTime" style="width: 100%"> <el-date-picker v-model="formData.endTime" type="datetime" format="YYYY/MM/DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束日期" style="width: 100%" /> </el-form-item> </el-form> <script setup> import {ref,reactive,watch} from 'vue'; let subimtForm = ref(); let formData = ref({}); // 表单规则 const rules = reactive({ startTime: [{ required: true, validator: (rule, value, callback)=> { if(!value){ callback(new Error("请选择开始时间")); }else{ if(formData.value.endTime){ subimtForm.value.validateField('endTime'); } callback(); } }, trigger: 'change', }], endTime: [{ required: true, validator: (rule, value, callback)=>{ if(!value){ callback(new Error("请选择结束时间")); }else{ if(!formData.value.startTime){ callback(new Error("请选择开始时间!")) }else if(Date.parse(formData.value.startTime) >= Date.parse(value)){ callback(new Error("结束时间必须大于开始时间!")) }else{ callback(); } } }, trigger: 'change', }] });</script>
文章来源地址https://www.toymoban.com/news/detail-760459.html
文章来源:https://www.toymoban.com/news/detail-760459.html
到了这里,关于vue3 +element-ui 实现时间选择器并校验开始时间小于结束时间的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!