element-ui时间组件的校验规则,时间不能为空且开始时间不能大于结束时间
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
免费下载资源
·
组件结构里的内容
// An highlighted block
<el-form :model="addForm" :rules="rules" ref="addForm" label-width="150px" class="demo-ruleForm">
<el-form-item label="开始时间:" prop="startTime">
<el-date-picker
v-model="addForm.startTime"
type="datetime"
:picker-options="pickerOptions"
value-format="yyyy-MM-dd HH:mm:ss"
placeholder="开始时间">
</el-date-picker>
</el-form-item>
<el-form-item label="结束时间:" prop="endTime">
<el-date-picker
v-model="addForm.endTime"
value-format="yyyy-MM-dd HH:mm:ss"
type="datetime"
:picker-options="pickerOptions"
placeholder="结束时间"
align="right">
</el-date-picker>
</el-form-item>
</el-form>
手写校验
// 时间的校验
const validateStartTime = (rule, value, callback) => {
if (value === undefined) {
callback(new Error('开始时间不能为空'))
} else {
if (new Date(this.addForm.startTime).getTime() <= new Date().getTime()) {
callback()
} else {
callback()
}
}
}
const validateEndTime = (rule, value, callback) => {
if (value === undefined) {
callback(new Error('结束时间不能为空'))
} else {
if (
new Date(this.addForm.startTime).getTime() >=
new Date(this.addForm.endTime).getTime()
) {
callback(new Error('截止时间必须大于开始时间!'))
} else {
callback()
}
}
}
from表单的校验
// An highlighted block
rules:{
startTime: [
{required: true, validator: validateStartTime, trigger: 'blur' },
],
endTime: [
{ required: true, validator: validateEndTime, trigger: 'blur' },
],
}
GitHub 加速计划 / eleme / element
10
1
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:5 个月前 )
c345bb45
9 个月前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 9 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)