Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required field validator in sapui5

I have four text fields with labels name, age, city and phone number.I have to validate it if it left empty. it should alert me to fill. How to validate a text field with required field validator in sapui5?

like image 370
Jag Avatar asked Feb 03 '26 05:02

Jag


1 Answers

You can simply write a function that get the textfields and checks their value.
This could look like this:

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
}

Then you can bind the function on the button-click, for example when creating the button:

new sap.ui.commons.Button({
    // your buttonconfiguration
    click: function(){
        validateTextFieldValues();
    }
});



Additionally the TextField has an attribute called valueState.
In connection with its event liveChange there is the opportunity to validate the input while typing:

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

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

like image 53
herrlock Avatar answered Feb 04 '26 19:02

herrlock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!