vue.js - Vue.js - Element UI - 获取表单验证的状态
问题描述
重现步骤
重复链接是这样的:https ://stackblitz.com/edit/typescript-vuejs-element-3kko7a
password input 2
username input 11
Change username to 2// should be able to submit, but not
预期: formvalid = true
在此处验证代码:
checkForm() {
// console.log('validate runs');
// @ts-ignore
const fields = this.$refs.ruleForm.fields;
if (fields.find((f) => f.validateState === 'validating')) {
setTimeout(() => {
this.checkForm();
}, 100);
}
this.formValid = fields.reduce((acc, f) => {
const valid = (f.isRequired && f.validateState === 'success');
const notErroring = (!f.isRequired && f.validateState !== 'error');
return acc && (valid || notErroring);
}, true);
console.log('valid:', this.$data.formValid);
}
解决方案
基本上你看ruleForm
- 所以每次ruleForm
更改你都会触发checkform
- 但是你的验证器会在blur
你设置后触发,ruleForm
所以你首先测试然后变为有效。将其更改为吸气剂:
get formValid(){
const fields = this.$refs.ruleForm.fields || [];
if (fields.find((f) => f.validateState === 'validating')) {
setTimeout(() => {
this.checkForm();
}, 100);
}
return fields.reduce((acc, f) => {
const valid = (f.isRequired && f.validateState === 'success');
const notErroring = (!f.isRequired && f.validateState !== 'error');
return acc && (valid || notErroring);
}, fields.length > 0);
}
推荐阅读
- excel - 将单元格中输入的值添加到同一列中的另一个单元格
- r - 将偏移变量合并到栅格中以预测泊松回归的最佳方法
- javascript - 将值映射到数组对象中的键
- java - 如果 amazonSqsAsync 无效,则阻止加载 SqsListener 和 AwsConfiguration
- c++ - 如何为全局类型设置静态计数器?
- c# - 如何查找(分组的)键的一部分是否在列表中多次出现?
- java - 在 Telosys 中使用 Dsl 模型生成带有链接的实体的问题
- url - 自动查找默认的索引文件在边缘浏览器中不起作用
- c# - 从 Razor 页面中 Index.chtml.cs 中的操作方法加载的 Ajax Partial 中的依赖注入
- python - networkx合并图形内存问题