1

In the current project, I have a SimpleForm on a view. Binding a JSONModel on this view, the validateValue function is working fine on my input field. The constraints on the input field are defined as follows:

<Input value="{
  path: '/carrId',
  type: 'sap.ui.model.type.String',
  constraints: {
    // ... 
  }
}"/>

Changing the model to an ODataModel doesn't fire the validateValue and the field is not marked with red if the constraints are not matched.

The view is registered at the message manager.

Do I have to implement the validation by implementing a change event or where is the mistake?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Christian
  • 13
  • 5
  • Just to let you know, I updated my answer below which might explain the actual reason why it didn't work when you changed from JSONModel to an ODataModel. – Boghyon Hoffmann Aug 29 '20 at 11:13

1 Answers1

2
  1. Replace sap.ui.model.type.String with the type corresponding to the EDM type of your carrId. E.g. with sap.ui.model.odata.type.String if it has the Type="Edm.String" in metadata. Note that the constraints settings are also different.

  2. In order to actually allow transferring the user input from the UI back to the model, and thus triggering parseValue and validateValue automatically, the binding mode needs to be TwoWay (The default binding mode of v2.ODataModel is OneWay*).

    • Open manifest.json

    • Set /sap.ui5/models/<modelName>/settings/defaultBindingMode to "TwoWay":

      {
        "dataSource": "MyV2ODataSource",
        "settings": {
          "defaultBindingMode": "TwoWay",
          "preliminaryContext": true
        },
        "preload": true
      }

* See Binding Modes: One-time Binding, One-way Binding, and Two-way Binding

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • A bit late but I'm experiencing the same issue: parseValue and validateValue are not called when I change from a JSONModel variable (validated against type sap.ui.model.type.Float) to an oDataModel (validated against type sap.ui.model.odata.Decimal). I ended up firing both the functions on the change event of the input but I'm not happy with this solution since I feel I'm missing something... Should I open a different thread? I can't find any topic related.. – Cmdd Aug 28 '20 at 13:11
  • @Cmdd I guess it's because you've assigned an invalid type. It should be `'sap.ui.model.odata.type.Decimal'` (In your case, the part `.type` is missing). UI5 only recently started logging errors for such typos: https://github.com/SAP/openui5/issues/2501. I.e. if you change the bootstrap CDN to `https://openui5nightly.hana.ondemand.com/resources/sap-ui-core.js`, you should see the corresponding error in the console. – Boghyon Hoffmann Aug 28 '20 at 13:24
  • I'm sorry, I misspelled the type in my question but in the code I use the right 'sap.ui.model.odata.type.Decimal' type :-( – Cmdd Aug 28 '20 at 13:33
  • @Cmdd https://jsbin.com/wujoduk/edit?js,output works (If value > 999 is typed, the valueState becomes red). If that doesn't help, I think a new question with an [mcve](https://stackoverflow.com/help/minimal-reproducible-example) would be very helpful so that other contributors can also take a look. – Boghyon Hoffmann Aug 28 '20 at 14:04
  • Many many many thanks for the jsBin but maybe I was unclear: your example works also for me when I bind a value from a JSONmodel as you did. The problem occurs only when I bind a value from an oData model, let's say the unnamed model, with data coming from tha backend! I'm really considering to open a new question but I'm a bit in a hurry right now :-) – Cmdd Aug 28 '20 at 14:15
  • 1
    @Cmdd Did you set the `defaultBindingMode` to `"TwoWay"`? In `JSONModel`, it's already two-way by default but `v2.ODataModel` one is `"OneWay"`. This might explain why `parseValue` and `validateValue` are not called when switching to ODataModel. – Boghyon Hoffmann Aug 28 '20 at 14:22
  • There it is! What a newbie error... I think I took too literally "summer brain vacation". Many thanks, again! – Cmdd Aug 28 '20 at 14:31
  • @Cmdd No, it's not a newbie error. I should've come to that conclusion earlier. Thank you too! This is an important note to add to my answer. – Boghyon Hoffmann Aug 28 '20 at 14:33