There are no secrets to success. It is the result of preparation, hard work, and learning from failure.

Friday 17 June 2016

Combo Box Validation

Click here and follow my SAP UI5/FIORI snippets and information Page
I have done combo-box validation here.if you enter value manually then it will check with all items,if match would not find then it will show error with red line.Go through it and please let me know in case of any issue/doubt.

Paste below lines in Controller.
//Write in onInit()
var json = sap.ui.model.json.JSONModel();
json.loadData("Model/Data.json", true, false);
var page = this.byId("Page_Id");
var simpleform = new sap.ui.layout.form.SimpleForm({
content : [ sap.m.FlexBox({
alignItems : "Center",
justifyContent : "Center",
items : [ new sap.m.ComboBox("comboId", {
change : [ this.handleEvent, this ],
placeholder : "Select name",
items : {
path : "/Record",
sorter : name,
template : new sap.ui.core.Item({
text : "{name}"
}),
}
}) ]

}) ]
})
var comboId=sap.ui.getCore().byId("comboId");
comboId.setModel(json);
var getBind=comboId.getBinding("items");
var sorter=new sap.ui.model.Sorter("name",false,false);
getBind.sort(sorter);
page.addContent(simpleform);

// Validation Part
// Change event for ComboBox...
handleEvent : function() {
var combo_Id = sap.ui.getCore().byId("comboId");
var allItem = combo_Id.getItems();
var arr = [];
var value = combo_Id.getValue().trim();
for (var i = 0; i < allItem.length; i++) {
arr.push(allItem[i].getText())}
if (arr.indexOf(value) < 0) {
combo_Id.setValueState("Error")
combo_Id.setValue();
} else {
combo_Id.setValueState("None")
} }

Create json file and Paste below lines in created file.

{"Record":[{"name":"ritesh joshi"},
{"name":"raj singh"},
{"name":"patlu"},
{"name":"daya"},
{"name":"dharma raju"},
{"name":"chuni"},
{"name":"nikhil kumar"},
{"name":"piku"},
{"name":"prashant kumar"}
]}

Note:- You have to give page id in view part.

Result


2 comments:

  1. how to make it dynamically with out using id of element

    ReplyDelete
    Replies
    1. Make simpleform variable like this.simpleform and get the content of form by getContent() method then you will get array.Find index of array.Now you can access comboBox dynamically without assigning id.
      Thanks for your comment....

      Delete