在 "sap.ui.model.SimpleType.extend" 中设置 TextField 值状态
Setting TextField Value State in "sap.ui.model.SimpleType.extend"
我正在尝试使用正则表达式为我的 table 中的列添加时间验证。首先我添加了一列,请找到下面的代码:
oTableFileContents.addColumn(
new sap.ui.table.Column("Time")
.setTemplate(
new sap.ui.commons.TextField({
editable:true,
valueState : sap.ui.core.ValueState.Error,
tooltip:""
})
.bindProperty("value","Time",new sap.myproject.Time())
)
.setLabel(
new sap.ui.commons.Label({
text : "Time"
})
)
);
在这里,默认情况下,我将每个 TextField 的 ValueState 设置为 "Error",以便每个空白条目都处于错误状态。在分配模型时,我必须将每个有效条目设置为 "Success"。为此,我将我的扩展 class 定义为:
sap.ui.model.SimpleType.extend("sap.myproject.Time", {
formatValue: function(oValue) {
var re = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
if(oValue!=null){
if(re.test(oValue)) {
// this.setEditable(false); //ERROR
//this.setValueState(sap.ui.core.ValueState.Success); //ERROR
alert("Success");
}
}
return oValue;
},
parseValue: function(oValue) {
return oValue;
},
validateValue: function(oValue) {}
});
但是在上面的代码中,我无法将 TextField 的状态设置为 "Success"。可能是我无法获得对我的 TextField 的引用。谁能帮我设置这个 Valuestate?任何 help/suggestion 将不胜感激。谢谢,库纳尔。
请不要,永远不要尝试从您的格式化程序中引用 UI 控件,永远! :-)
只需抛出一个 sap.ui.model.FormatException
,框架会为您完成剩下的工作:
// ...snip...
formatValue: function(oValue) {
var re = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
if(oValue!=null){
if(!re.test(oValue)) {
throw new sap.ui.model.FormatException("Value " + oValue + " is not in a valid Date format");
}
}
//optional, if you don't want empty/null dates
else {
throw new sap.ui.model.FormatException("Date cannot be null or empty");
}
return oValue;
},
// ...snip...
编辑:为了获得格式或验证错误的视觉线索,您应该在控制器的 onInit
事件处理程序中附加格式和验证事件处理程序:
sap.ui.getCore().attachFormatError(function (oEvent) {
oEvent.getParameter("element").setValueState(sap.ui.core.ValueState.Error);
});
sap.ui.getCore().attachValidationError(function (oEvent) {
oEvent.getParameter("element").setValueState(sap.ui.core.ValueState.Error);
});
sap.ui.getCore().attachValidationSuccess(function (oEvent) {
oEvent.getParameter("element").setValueState(sap.ui.core.ValueState.None);
});
我正在尝试使用正则表达式为我的 table 中的列添加时间验证。首先我添加了一列,请找到下面的代码:
oTableFileContents.addColumn(
new sap.ui.table.Column("Time")
.setTemplate(
new sap.ui.commons.TextField({
editable:true,
valueState : sap.ui.core.ValueState.Error,
tooltip:""
})
.bindProperty("value","Time",new sap.myproject.Time())
)
.setLabel(
new sap.ui.commons.Label({
text : "Time"
})
)
);
在这里,默认情况下,我将每个 TextField 的 ValueState 设置为 "Error",以便每个空白条目都处于错误状态。在分配模型时,我必须将每个有效条目设置为 "Success"。为此,我将我的扩展 class 定义为:
sap.ui.model.SimpleType.extend("sap.myproject.Time", {
formatValue: function(oValue) {
var re = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
if(oValue!=null){
if(re.test(oValue)) {
// this.setEditable(false); //ERROR
//this.setValueState(sap.ui.core.ValueState.Success); //ERROR
alert("Success");
}
}
return oValue;
},
parseValue: function(oValue) {
return oValue;
},
validateValue: function(oValue) {}
});
但是在上面的代码中,我无法将 TextField 的状态设置为 "Success"。可能是我无法获得对我的 TextField 的引用。谁能帮我设置这个 Valuestate?任何 help/suggestion 将不胜感激。谢谢,库纳尔。
请不要,永远不要尝试从您的格式化程序中引用 UI 控件,永远! :-)
只需抛出一个 sap.ui.model.FormatException
,框架会为您完成剩下的工作:
// ...snip...
formatValue: function(oValue) {
var re = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
if(oValue!=null){
if(!re.test(oValue)) {
throw new sap.ui.model.FormatException("Value " + oValue + " is not in a valid Date format");
}
}
//optional, if you don't want empty/null dates
else {
throw new sap.ui.model.FormatException("Date cannot be null or empty");
}
return oValue;
},
// ...snip...
编辑:为了获得格式或验证错误的视觉线索,您应该在控制器的 onInit
事件处理程序中附加格式和验证事件处理程序:
sap.ui.getCore().attachFormatError(function (oEvent) {
oEvent.getParameter("element").setValueState(sap.ui.core.ValueState.Error);
});
sap.ui.getCore().attachValidationError(function (oEvent) {
oEvent.getParameter("element").setValueState(sap.ui.core.ValueState.Error);
});
sap.ui.getCore().attachValidationSuccess(function (oEvent) {
oEvent.getParameter("element").setValueState(sap.ui.core.ValueState.None);
});