【问题标题】:UI5: Validate Whole Form's Required and Visible Fields for Null/Blank ValuesUI5:验证整个表单的空值/空白值的必填和可见字段
【发布时间】:2020-11-05 06:55:05
【问题描述】:

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

  1. 必填&
  2. 可见

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

【问题讨论】:

    标签: forms validation input combobox sapui5


    【解决方案1】:

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

    控制器代码

    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();
        }
      },

    【讨论】:

    • 我认为您立即回答了自己的问题(就像某种教程一样)很棒,但我认为这段代码可以进行大量优化。
    • 总是欢迎优化。我很想看看它如何变成更好的东西,我们的社区可以使用
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2022-11-22
    相关资源
    最近更新 更多