当前位置: 首页 > news >正文

自定义组件触发饿了么表单校验

饿了么的表单控件,如果存在自定义组件更改了值,例如在el-from中存在原生input组件很有可能没法触发表单校验,下拉框或者弹框组件仍然是报红边框。
这是因为饿了么的输入框或者下拉框更改值的时候会自动触发表单校验,但是封装过后的组件无法触发校验表单校验。那么此时可以手动触发饿了么的表单校验。

源码分析

在packages/form/src/form-item.vue中,可以找到addValidateEvents方法,该方法是用来给el-form-item的子组件绑定校验事件的,如下:

addValidateEvents() {
  const rules = this.getRules();
  if (rules.length || this.required !== undefined) {
    this.$on('el.form.blur', this.onFieldBlur);
    this.$on('el.form.change', this.onFieldChange);
  }
}

在packages/input/src/input.vue中,可以找到el-input发送el.form.blur和el.form.change事件的代码,这里只贴出el.form.change的代码:

watch: {
  value(val) {
    this.$nextTick(this.resizeTextarea);
    if (this.validateEvent) {
      this.dispatch('ElFormItem', 'el.form.change', [val]);
    }
  }
}

这里用了dispatch方法,该方法的代码在src/mixins/emitter.js中:

dispatch(componentName, eventName, params) {
  var parent = this.$parent || this.$root;
  var name = parent.$options.componentName;
  while (parent && (!name || name !== componentName)) {
    parent = parent.$parent;
    if (parent) {
      name = parent.$options.componentName;
    }
  }
  if (parent) {
    parent.$emit.apply(parent, [eventName].concat(params));
  }
}

由此可以看出,要触发el-form的校验,需要el-form-item中的子组件去发布el.form.change或el.form.blur等事件,由el-form-item监听该事件,触发表单校验。

解决方案

方法一:在父页面中直接调用表单的校验方法validateField:

watch: {
  'passwordForm.newPassword': function() {
    this.$refs.passwordForm.validateField('newPassword')
  }
}

方法二:在父页面中发布组件的el.form.change等事件:

<input ref="input" @blur="handleBlur">
<script>
export default {
  methods: {
    handleBlur (val) {
      this.$refs.input.$emit('el.form.blur', val)
    }
  }
}
</script>

方法三:在子组件中发布el.form.change等事件,此时无需在父页面中做任何处理,其中dispatch方法直接将上面所说的emitter.js中的代码拷贝过来即可:

export default {
  methods: {
    dispatch(componentName, eventName, params) {
      // ... 从emitter.js中拷贝过来的代码
    },
    handleInput (e) {
      this.$emit('input', e.target.value)
      this.dispatch('ElFormItem', 'el.form.change', [e.target.value])
    }
  }
}

以下是我某项目的解决方法,使用的方法2,即找到el-form-item然后触发el.form.change事件。

 methods: {
	confirm() {
	      this.$emit("confirm", result);
	      this.$nextTick(() => {
	        parent && parent.$emit("el.form.blur", result); // 重点!触发表单校验 el.form.change, el.form.blur
       		parent && parent.$emit("el.form.change", result); // 重点!触发表单校验
      });
    },
   },
 // 找到el-form-item元素的代码
 mounted() {
   let parent = this.$parent;
   while (parent) { 
	   if (parent.$options.name === "ElFormItem") {
	     this.parent = parent;
	     break;
	   }
	   parent = parent.$parent;
   }
},
    

原文博客:非el组件/自定义组件触发el-form的校验
个人博客: 自定义组件触发饿了么表单校验


文章转载自:
http://bellarmine.isnyv.cn
http://asafetida.isnyv.cn
http://benlate.isnyv.cn
http://caseload.isnyv.cn
http://chloronaphthalene.isnyv.cn
http://americandom.isnyv.cn
http://chromizing.isnyv.cn
http://bank.isnyv.cn
http://chimere.isnyv.cn
http://alcor.isnyv.cn
http://cacodemon.isnyv.cn
http://chipped.isnyv.cn
http://chilli.isnyv.cn
http://amethystine.isnyv.cn
http://buster.isnyv.cn
http://barkhausen.isnyv.cn
http://charter.isnyv.cn
http://barbate.isnyv.cn
http://anaerobiosis.isnyv.cn
http://antecede.isnyv.cn
http://butcherly.isnyv.cn
http://carbonate.isnyv.cn
http://anchor.isnyv.cn
http://antiphrasis.isnyv.cn
http://bieberite.isnyv.cn
http://canaster.isnyv.cn
http://azide.isnyv.cn
http://carrack.isnyv.cn
http://bereave.isnyv.cn
http://attorn.isnyv.cn
http://www.dtcms.com/a/112772.html

相关文章:

  • LaTeX、KaTeX、Markdown 的用法
  • 15.2linux设备树下的platform驱动编写(程序)_csdn
  • 与 AI 共舞:解锁自我提升的无限可能
  • 如何通过优化HMI设计大幅提升产品竞争力?
  • 配置网络编辑器
  • 【Rust学习】Rust环境搭建和Rust基础语法
  • Jetpack Compose 自定义组件完全指南
  • python基础-13-处理excel电子表格
  • 叁仟数智指路机器人的智能导航精度如何?
  • 【爬虫案例】采集 Instagram 平台数据几种方式(python脚本可直接运行)
  • 你用的是Bing吗?!
  • 【AI论文】GPT-ImgEval:一个用于诊断GPT4o在图像生成方面的综合基准
  • 论文写作篇#8:双栏的格式里怎么插入横跨两栏的图片和表格
  • Kafka 概念
  • Johnson算法——两阶段流水线调度的最优解法
  • k8s安装cri驱动创建storageclass动态类
  • Deep Reinforcement Learning for Robotics翻译解读2
  • 关于apple ios苹果mdm监管锁的漏洞与修复
  • web forms可视化开发显示的网页是用ExpressionWebEditorFrame控件,是IE内核还是简单的HTML解析?如何让他加载CSS和JS?
  • 如何一天背300到500个单词
  • 赚钱模拟器-百宝库v0.1.1
  • 精品可编辑PPT | 基于湖仓一体构建数据中台架构大数据湖数据仓库一体化中台解决方案
  • ffmpeg音频分析
  • 机器学习(1)—线性回归
  • 【Pandasai】理解SmartDataframe 类:对dataframe添加自然语言处理能力
  • 从爬虫到可视化:Python分析豆瓣Top250电影数据
  • 不在 qtdesigner中提升,进行主题程序设计
  • FreeRTOS 启动过程中 SVC 和 PendSV 的工作流程​
  • 新能源汽车电子电气架构设计中的功能安全
  • DHCP Snooping理论笔记(超详细)