如何在输入字段中添加验证?

Kod*_*_12 0 validation sapui5

刚学习SAP UI5,我有一个简单的输入字段,如下所示:

app.xml中

<Input
    placeholder="Please enter 10 digit number"
    value=""
/Input>
Run Code Online (Sandbox Code Playgroud)

我试图了解如何在输入字段上添加验证,执行以下操作:

- 接受10位数或更多位数

到目前为止,据我所知,这可以通过以下方式设置:

<Input value=”{

    path : ‘/userName’,
    type : ‘sap.ui.model.type.Integer’,
    constraints : {
        minLength : 10
    }
}
Run Code Online (Sandbox Code Playgroud)

我感到困惑的地方:

输入字段用作搜索栏,因此我不需要引入数据绑定(搜索栏不应该有初始值),我必须在其中配置路径.有人可以向我解释如何在不设置"路径"的情况下添加验证吗?

小智 5

如果您查看Input的文档,您将看到该控件具有事件liveChange.此外,您还可以使用验证器向用户提供即时反馈.

我们来看一个例子 -

//View
<Input liveChange="onLiveChange" />

//Controller
onLiveChange:function(oEvent){
    var newValue = oEvent.getParameter("newValue");
            this.setValueState(sap.ui.core.ValueState.Success); //you can also use None, or just remove this line

            if(/* your error condition */)
                this.setValueState(sap.ui.core.ValueState.Error);
            } 

}
Run Code Online (Sandbox Code Playgroud)

当"您的条件"为真时,整个输入将改变其边界.

无论如何,如果要使用模型,可以尝试创建本地json模型,并尝试使用约束将其绑定到输入.但在这种情况下,我会选择第一个解决方案.