如何在 sapui5 上验证表单(不要取空值)
how to validate form(do not take null value) on sapui5
我的表格
var oSimpleForm = new sap.ui.layout.form.SimpleForm({
maxContainerCols: 2,
content:[
new sap.ui.core.Title({text:"Employee"}),
new sap.ui.commons.Label({text:"Emp No",required:true}),
new sap.ui.commons.TextField({value:"",}),
new sap.ui.commons.Label({text:"First Name"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"Last Name"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"Street"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"Country"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"City"}),
new sap.ui.commons.TextField({value:"",required: true})
]
});
oCreateDialog.addContent(oSimpleForm);
oCreateDialog.addButton(
new sap.ui.commons.Button({
text: "Submit",
press: function() {
//validate input data not null
}
})
);
这是一个简单的表格,需要一些输入 data.My 问题是 -- 单击提交按钮时,我如何验证输入数据或在提交时没有数据字段应为空...
谢谢
function() {
// get the content (as array)
var content = oSimpleForm.getContent();
// check every single control
for (var i in content) {
var control = content[i];
// check control only if it has the function getValue
// a rather primitive way to filter the TextFields
if(control.getValue) {
// check the value on empty text
if(control.getValue() === "") {
// do whatever you want to show the user he has to provide more input
alert("empty value found");
}
}
}
}
只提醒一次:
function() {
var content = oSimpleForm.getContent();
// assume all controls are valid, if one is invalid this flag is set to false
var allValid = true;
for (var i in content) {
var control = content[i];
// in case one control was invalid the variable allValid has been set to false
if(allValid && control.getValue && control.getValue() === "") {
allValid = false;
// stop the for-loop
break;
}
}
if(!allValid) {
alert("empty value found");
}
}
我的表格
var oSimpleForm = new sap.ui.layout.form.SimpleForm({
maxContainerCols: 2,
content:[
new sap.ui.core.Title({text:"Employee"}),
new sap.ui.commons.Label({text:"Emp No",required:true}),
new sap.ui.commons.TextField({value:"",}),
new sap.ui.commons.Label({text:"First Name"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"Last Name"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"Street"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"Country"}),
new sap.ui.commons.TextField({value:"",required: true}),
new sap.ui.commons.Label({text:"City"}),
new sap.ui.commons.TextField({value:"",required: true})
]
});
oCreateDialog.addContent(oSimpleForm);
oCreateDialog.addButton(
new sap.ui.commons.Button({
text: "Submit",
press: function() {
//validate input data not null
}
})
);
这是一个简单的表格,需要一些输入 data.My 问题是 -- 单击提交按钮时,我如何验证输入数据或在提交时没有数据字段应为空...
谢谢
function() {
// get the content (as array)
var content = oSimpleForm.getContent();
// check every single control
for (var i in content) {
var control = content[i];
// check control only if it has the function getValue
// a rather primitive way to filter the TextFields
if(control.getValue) {
// check the value on empty text
if(control.getValue() === "") {
// do whatever you want to show the user he has to provide more input
alert("empty value found");
}
}
}
}
只提醒一次:
function() {
var content = oSimpleForm.getContent();
// assume all controls are valid, if one is invalid this flag is set to false
var allValid = true;
for (var i in content) {
var control = content[i];
// in case one control was invalid the variable allValid has been set to false
if(allValid && control.getValue && control.getValue() === "") {
allValid = false;
// stop the for-loop
break;
}
}
if(!allValid) {
alert("empty value found");
}
}