首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UI5:为空/空白值验证整个表单的必需字段和可见域

UI5:为空/空白值验证整个表单的必需字段和可见域
EN

Stack Overflow用户
提问于 2020-07-15 17:28:21
回答 1查看 726关注 0票数 0

onPress of submit按钮,我想验证所有SimpleForms的字段(ComboBox、Input、DatePicker等等)。它们是

需要&

  • visible

查看它们是空还是空("")。如果目标字段(必需和可见的)为空/空,则将该控件的状态设置为" error“并显示错误消息。如果没有目标字段为空/空,则弹出“成功”对话框。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-15 17:31:14

此方法是自动化的,因此在将来,以后添加的任何字段都将自动检查,而不需要手动添加控制器代码。

控制器代码

代码语言:javascript
复制
requiredAndVisible: function(oControl) {
    if (typeof oControl.getRequired === "function") { //certain ctrls like toolbars dont have getRequired as a method, so we want to skim those out, else itll throw an error later in the next check
      if (oControl.getRequired() === true && oControl.getVisible() === true) {
        return oControl;
      }
    }
  },

  onSubmit: function() {
    var valid = true,
      oView = this.getView(),
      aFormInitial = oView.byId("formInitial").getContent(), // get all the controls of SimpleForm1
      aFormConfig = oView.byId("formConfiguration").getContent(), // get all controls of SimpleForm2
      aControls = aFormInitial.concat(aFormConfig), // combine the 2arrays together into 1
      aFilteredControls = aControls.filter(this.requiredAndVisible); // check each element if it required & visible using the 1st function. return only the controls that are both req'd & visible

    aFilteredControls.forEach(function(oControl) { // in resultant array, check each element if...  
      if (!oControl.getValue() || oControl.getValue().length < 1) { // its value is null or blank
        oControl.setValueState("Error");
        valid = false; // set valid to false if it is
      } else {
        oControl.setValueState("None");
      }
    });

    if (valid === false) {
      // **replace this code with w/e error handling code u want**
      oView.byId("errorMsgStrip").setVisible(true);
    } else if (valid === true) {
      // **replace this code with whatever success handling code u want**
      var oDialogConfirm = new sap.ui.xmlfragment("dialogID", "dialog.address.here", this);
      oDialogConfirm.open();
    }
  },

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62920568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档