gpt4 book ai didi

javascript - 如何向输入字段添加验证?绑定(bind) JSON 模型不起作用

转载 作者:行者123 更新时间:2023-12-03 07:52:33 24 4
gpt4 key购买 nike

我尝试使用 Samples frpm Demo kit Input - Checked 学习 SAPUI5。我收到一条错误消息: oInput.getBinding 不是函数
我有一个简单的输入字段 xml:

<Label text="Name" required="false" width="60%" visible="true"/>
<Input id="nameInput" type="Text" enabled="true" visible="true" valueHelpOnly="false" required="true" width="60%" valueStateText="Name must not be empty." maxLength="0" value="{previewModel>/name}" change= "onChange"/>
和我的 Controller :
    _validateInput: function(oInput) {
var oView = this.getView().byId("nameInput");
oView.setModel(this.getView().getModel("previewModel"));
var oBinding = oInput.getBinding("value");
var sValueState = "None";
var bValidationError = false;

try {
oBinding.getType().validateValue(oInput.getValue());
} catch (oException) {
sValueState = "Error";
bValidationError = true;
}

oInput.setValueState(sValueState);

return bValidationError;
},

/**
* Event handler for the continue button
*/
onContinue : function () {
// collect input controls
var that = this;
var oView = this.getView();
var aInputs =oView.byId("nameInput");
var bValidationError = false;

// check that inputs are not empty
// this does not happen during data binding as this is only triggered by changes
jQuery.each(aInputs, function (i, oInput) {
bValidationError = that._validateInput(oInput) || bValidationError;
});

// output result
if (!bValidationError) {
MessageToast.show("The input is validated. You could now continue to the next screen");
} else {
MessageBox.alert("A validation error has occured. Complete your input first");
}
},

// onChange update valueState of input
onChange: function(oEvent) {
var oInput = oEvent.getSource();
this._validateInput(oInput);
},
有人可以向我解释如何设置模型吗?

最佳答案

您的模型很好并且正确绑定(bind)。
您的代码中的问题在这里,在 onContinue功能

         jQuery.each(aInputs, function (i, oInput) {
bValidationError = that._validateInput(oInput) || bValidationError;
});
aInput不是数组,因此您的代码不会在数组元素上进行迭代。
为了快速解决这个问题,您可以像这样在声明周围加上括号:
        var aInputs = [
oView.byId("nameInput")
];

此外,您可以删除 _validateInput 的前两行方法,因为它们没用...

关于javascript - 如何向输入字段添加验证?绑定(bind) JSON 模型不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59824627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com