Sapui5 如何在SAP UI5中验证表单中的必填字段?
我正在尝试创建一个表单,其中包含一些必填字段,需要在表单提交时进行验证Sapui5 如何在SAP UI5中验证表单中的必填字段?,sapui5,Sapui5,我正在尝试创建一个表单,其中包含一些必填字段,需要在表单提交时进行验证 有谁能建议我在SAP UI5中实现这一点的最佳方法吗?必填字段的数量更多,因此我不想按ID分别检查所有字段。您可以在两种情况下执行此操作。输入值时,或提交问题中的表单时 CheckRequired: function(oEvent) { var aInputs = [this.getView().byId(oEvent.getSource().getId())]; var
有谁能建议我在SAP UI5中实现这一点的最佳方法吗?必填字段的数量更多,因此我不想按ID分别检查所有字段。您可以在两种情况下执行此操作。输入值时,或提交问题中的表单时
CheckRequired: function(oEvent) {
var aInputs = [this.getView().byId(oEvent.getSource().getId())];
var sError = false;
jQuery.each(aInputs, function(i, input) {
if (!input.getValue() || input.getValue().length < 1) {
input.setValueState("Error");
input.focus();
sError = true;
} else {
input.setValueState("None");
}
});
return sError;
},
CheckRequired:功能(oEvent){
var aInputs=[this.getView().byId(oEvent.getSource().getId())];
var-sError=false;
每个(输入,函数(i,输入){
如果(!input.getValue()| | input.getValue().length<1){
input.setValueState(“错误”);
input.focus();
说谎者=真实;
}否则{
input.setValueState(“无”);
}
});
回禀;
},
此函数将与onLiveChange属性一起使用。它检查控件是否至少填充了一个字符
如果您想在按submit时检查所有内容。您可以在表单中使用如下函数:
_onSubmitCheck: function() {
var oForm = this.getView().byId("form").getContent();
var sError = false;
oForm.forEach(function(Field) {
if (typeof Field.getValue === "function") {
if (!Field.getValue() || Field.getValue().length < 1) {
Field.setValueState("Error");
sError = true;
}
else {
Field.setValueState("None");
}
}
});
return sError;
},
\u onSubmitCheck:function(){
var of orm=this.getView().byId(“form”).getContent();
var-sError=false;
forEach格式(函数(字段){
if(type of Field.getValue==“函数”){
如果(!Field.getValue()| | Field.getValue().length<1){
字段。setValueState(“错误”);
说谎者=真实;
}
否则{
字段。setValueState(“无”);
}
}
});
回禀;
},
它将在表单控件上循环,以检查getValue()方法是否作为控件的一部分存在。如果返回yes,它将检查其值是否至少为1个字符。您可以在两种情况下执行此操作。输入值时,或提交问题中的表单时
CheckRequired: function(oEvent) {
var aInputs = [this.getView().byId(oEvent.getSource().getId())];
var sError = false;
jQuery.each(aInputs, function(i, input) {
if (!input.getValue() || input.getValue().length < 1) {
input.setValueState("Error");
input.focus();
sError = true;
} else {
input.setValueState("None");
}
});
return sError;
},
CheckRequired:功能(oEvent){
var aInputs=[this.getView().byId(oEvent.getSource().getId())];
var-sError=false;
每个(输入,函数(i,输入){
如果(!input.getValue()| | input.getValue().length<1){
input.setValueState(“错误”);
input.focus();
说谎者=真实;
}否则{
input.setValueState(“无”);
}
});
回禀;
},
此函数将与onLiveChange属性一起使用。它检查控件是否至少填充了一个字符
如果您想在按submit时检查所有内容。您可以在表单中使用如下函数:
_onSubmitCheck: function() {
var oForm = this.getView().byId("form").getContent();
var sError = false;
oForm.forEach(function(Field) {
if (typeof Field.getValue === "function") {
if (!Field.getValue() || Field.getValue().length < 1) {
Field.setValueState("Error");
sError = true;
}
else {
Field.setValueState("None");
}
}
});
return sError;
},
\u onSubmitCheck:function(){
var of orm=this.getView().byId(“form”).getContent();
var-sError=false;
forEach格式(函数(字段){
if(type of Field.getValue==“函数”){
如果(!Field.getValue()| | Field.getValue().length<1){
字段。setValueState(“错误”);
说谎者=真实;
}
否则{
字段。setValueState(“无”);
}
}
});
回禀;
},
它将在表单控件上循环,以检查getValue()方法是否作为控件的一部分存在。如果返回yes,它将检查它是否有至少1个字符的值。有两种方法 加
“sap.ui5”:{
...
“扶手电梯”:没错,有两种方式
加
“sap.ui5”:{
...
“handleValidation”:true,
我是按照老派的方式进行的。输入字段确实获得了required=true
属性,然后我循环使用此属性找到的所有控件:
// store view ID to compare with control IDs later
var viewId = this.getView().getId();
jQuery('input[required=required]').each(function () {
// control has wrapper with no id, therefore we need to remove the "-inner" end
var oControl = sap.ui.getCore().byId(this.id.replace(/-inner/g,''));
// CAUTION: as OpenUI5 keeps all loaded views in DOM, ensure that the controls found belong to the current view
if (oControl.getId().startsWith(viewId) && (oControl instanceof sap.m.Input || oControl instanceof sap.m.DatePicker)) {
var val = oControl.getValue();
if (!val) {
oControl.setValueState(sap.ui.core.ValueState.Error);
oControl.openValueStateMessage();
bError = true;
return false;
} else {
oControl.setValueState(sap.ui.core.ValueState.None);
oControl.closeValueStateMessage();
}
}
});
嗯,
Anton我是用老派的方式来做的。输入字段确实获得了required=true
属性,然后我循环使用此属性找到的所有控件:
// store view ID to compare with control IDs later
var viewId = this.getView().getId();
jQuery('input[required=required]').each(function () {
// control has wrapper with no id, therefore we need to remove the "-inner" end
var oControl = sap.ui.getCore().byId(this.id.replace(/-inner/g,''));
// CAUTION: as OpenUI5 keeps all loaded views in DOM, ensure that the controls found belong to the current view
if (oControl.getId().startsWith(viewId) && (oControl instanceof sap.m.Input || oControl instanceof sap.m.DatePicker)) {
var val = oControl.getValue();
if (!val) {
oControl.setValueState(sap.ui.core.ValueState.Error);
oControl.openValueStateMessage();
bError = true;
return false;
} else {
oControl.setValueState(sap.ui.core.ValueState.None);
oControl.closeValueStateMessage();
}
}
});
嗯,
安东