sapui5中必填字段验证器

Mun*_*ish 2 javascript jquery sapui5

我有四个带有标签名称,年龄,城市和电话号码的文本字段.如果它留空,我必须验证它.它应该提醒我填写.如何在sapui5中使用必填字段验证器验证文本字段?

her*_*ock 8

您可以简单地编写一个获取文本字段并检查其值的函数.
这看起来像这样:

function validateTextFieldValues() {

    // this function gets the first textfield
    var nameTextField = sap.ui.getCore().byId("idOfTheTextFieldContaingTheName");
    //this function checks its value, you can insert more checks on the value
    if(nameTextField.getValue() === "") {
        alert("Please enter a name.");
    }

    // ...
    // the same for the other fields
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在按钮单击时绑定该功能,例如在创建按钮时:

new sap.ui.commons.Button({
    // your buttonconfiguration
    click: function(){
        validateTextFieldValues();
    }
});
Run Code Online (Sandbox Code Playgroud)



另外,它TextField有一个名为的属性valueState.
与其事件相关,liveChange有机会在键入时验证输入:

new sap.ui.commons.TextField({
    // your configuration
    liveChange: function(){
        if(this.getValue() === "")
            this.setValueState(sap.ui.core.ValueState.Error);
        else
            this.setValueState(sap.ui.core.ValueState.Success);
    }
});
Run Code Online (Sandbox Code Playgroud)

(https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.core.ValueState.html)