SAP / openui5

OpenUI5 lets you build enterprise-ready web applications, responsive to all devices, running on almost any browser of your choice.
http://openui5.org
Apache License 2.0
2.95k stars 1.23k forks source link

Controller Code for Sample Input Checked does not work #3693

Closed simonarangelova closed 1 year ago

simonarangelova commented 1 year ago

OpenUI5 version: 1.110

Browser/version (+device/version):

Any other tested browsers/devices(OK/FAIL):

URL (minimal example if possible): https://ui5.sap.com/#/entity/sap.m.Input/sample/sap.m.sample.InputChecked

User/password (if required and possible - do not post any confidential information here):

Steps to reproduce the problem: The sample for Input - Checked (https://ui5.sap.com/#/entity/sap.m.Input/sample/sap.m.sample.InputChecked) does not work.

In the Controller code https://ui5.sap.com/#/entity/sap.m.Input/sample/sap.m.sample.InputChecked/code/C.controller.js the function _validateInput needs to be corrected.

What is the expected result?

_validateInput: function (oInput) {
    var sValueState = "None";
    var bValidationError = false;
    var oBindingInfo = oInput.getBindingInfo("value");

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

    oInput.setValueState(sValueState);

    return bValidationError;
},

What happens instead?

_validateInput: function (oInput) {
    var sValueState = "None";
    var bValidationError = false;
    var oBinding = oInput.getBinding("value");

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

    oInput.setValueState(sValueState);

    return bValidationError;
},

Any other information? (attach screenshot if possible) This issue is moved from SAP-docs repository sebastianesch commented (https://github.com/SAP-docs/sapui5/issues/37#issue-1563903050)

boghyon commented 1 year ago

Hi @sebastianesch ,

the function _validateInput needs to be corrected

Could you please elaborate on why exactly oInput.getBinding("value") needs to be changed to oInput.getBindingInfo("value")? The sample seems to be working fine.

Also, ManagedObject#getBindingInfo is a protected API.

sebastianesch commented 1 year ago

Hi @boghyon,

sorry, I did not notice that getBindingInfo is protected. When I try the example with getBinding the return value is undefined:

image

getBindingInfo returns the Binding information defined in the view.

Kind regards, Sebastian

sebastianesch commented 1 year ago

Hi @boghyon,

I debugged the UI5 sample and can confirm that it works. In our case the Binding Info object was missing the binding property and as getBinding uses return oInfo && oInfo.binding;, it would not return the binding:

image

The root cause for the issue was that our binding definition in the view contained path : '{/name}', instead of path : '/name',. Removing {} fixed the issue and the example works.

<Input
            id="nameInput"
            class="sapUiSmallMarginBottom"
            placeholder="Enter name"
            valueStateText="Name must not be empty. Maximum 10 characters."
            value="{
                path : '{/name}',  <!-- {} breaks binding info -->
                type : 'sap.ui.model.type.String',
                constraints : {
                    minLength: 1,
                    maxLength: 10
                }
            }"
            change= ".onNameChange" />

Sorry for the inconvenience and thanks for the help.

Kind regards, Sebastian

boghyon commented 1 year ago

@sebastianesch No prob, thanks for the replies! You might want to subscribe to https://github.com/SAP/ui5-language-assistant/issues/82 and https://github.com/SAP/ui5-language-assistant/issues/563 which, if the requested feature(s) were ever implemented, could detect invalid values such as path: '{/name}'.