背景:

在onReady初始化规则

onReady() {
this.$refs.uForm.setRules(this.rules);
},

同时:ref,model,rules,props都要配置好。

报错

当input框限定type为number,digit类型有初始值不做修改动作,直接提交会报错,验证失败:报空。

验证规则

data():{

return {

model: {type: '年假', days: '0', reason: '-', hours: 0,},rules: {type: [{required: true,message: '请输入请假类型',trigger: ['change', 'blur'],}],days: [{required: true,message: '请输入请假类型',trigger: ['change', 'blur'],}],hours: [{required: true,message: '请输入小时',trigger: ['change', 'blur'],}],reason: [{required: true,message: '请输入租赁洗涤价',trigger: ['change', 'blur']}]// {min: 0,max:500, message: '不能小于0', trigger: ['change', 'blur'],}],},

}}

解决:

方法1:hours:数字的字段加上数字正则匹配规则,限制只能0-999的数字,且只能有2个小数。

hours: [{required: true,message: '请输入小时',pattern:/^(([1-9]\d{0,3})|0)(\.\d{0,2})" />确定export default {data() {return {show: false,model: {type: '年假', days: '0', reason: '-', hours: 1,},rules: {type: [{required: true,message: '请输入请假类型',trigger: ['change', 'blur'],}],days: [{required: true,message: '请输入请假类型',trigger: ['change', 'blur'],}],hours: [{required: true,message: '请输入请假类型',pattern:/^(([1-9]\d{0,3})|0)(\.\d{0,2})?$/,trigger: ['change', 'blur'],}],reason: [{required: true,message: '请输入原由',trigger: ['change', 'blur']}]// {min: 0,max:500, message: '不能小于0', trigger: ['change', 'blur'],}],},list: [{name: '发起申请'}, {name: '查看数据',}],m2mSimflowList: [],m2mOrderFlowList: [],current: 0,status: 'loadmore',iconType: 'circle',isDot: false,loadText: {loadmore: '点击加载更多',loading: '正在加载...',nomore: '没有更多了'},}},onReady() {this.$refs.uForm.setRules(this.rules)},created() {},methods: {submitForm() {this.$refs.uForm.validate(valid => {if (valid) {this.$u.toast("验证通过")} else {this.$u.toast('验证失败')}})},change(index) {this.current = index;},navTo(url) {uni.navigateTo({url: url});}}}