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

问题描述 投票:1回答:2

我尝试通过Samples frpm演示工具包输入来学习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"/>

和我的控制器:

_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);
    },

有人可以向我解释如何设置模型吗?

javascript validation error-handling model sapui5
2个回答
0
投票

通常,我们是在加载视图后设置模型,而不是在更改值时设置模型。例如,如果您想使用名称“ previewModel”设置JSONModel


0
投票

您的模型很好,并且已正确绑定。您的代码中的问题在这里,在onContinue函数中

© www.soinside.com 2019 - 2024. All rights reserved.