后续继续整理和参考
https://www.jianshu.com/p/ffbd55a2c866
https://www.jianshu.com/p/ef97dbf4fcfe
https://www.jianshu.com/p/5f9300437038
https://blog.csdn.net/bluefish_flying/article/details/81909136
因为在type="number"这里就给禁止了,但是这种写法允许为空,而我们要求为必填
<FormItem label="班级:" prop="301">
<Input v-model="parameter[301]" type="number">Input>
FormItem>
ruleValidate: {
"301": [
{
required: true,
message: "这是必填项",
trigger: "blur"
},
{
type:"number",
message:"请填入数字",
trigger:"blur"
}
]
}
type=number
ruleValidate: {
"301": [
{
required: true,
message: "这是必填项",
trigger: "blur"
}
]
}
type
和require
写入一个里面也不行<FormItem label="班级:" prop="301">
<Input v-model="parameter[301]" type="number">Input>
FormItem>
ruleValidate: {
"301": [
{
required: true,
message: "必须填写数字",
trigger: "blur",
type:"number"
}
]
}
依旧无法校验是否为数字,只能在input上面加入type=number
{ type: 'number', message: '请输入排序', trigger: 'blur' }
<FormItem label="班级:" prop="301">
<Input v-model="parameter[301]" type="number">Input>
FormItem>
ruleValidate: {
"301": [
{
required: true,
message: "这是必填项",
// trigger: "blur"
},
{
validator: validateNum,
}
]
}
validateNum为自定义校验规则,直接放在data里面就可以
const validateNum = (rule, value, callback) => {
console.log("rule");
console.log(rule);
console.log("value");
console.log(value);
console.log("callback");
console.log(callback);
if (!Number.isInteger(+value)) {
callback(new Error("请输入数字"));
} else {
callback();
}
};
这里有个问题,如果在require
里面加入trigger: "blur"
,会导致第一次进来以后,有值的时候也会报错,所以我把他去掉,直接在提交的时候在校验