1

I have a datepicker that was created with the following code snippet:

return new sap.m.DatePicker(sId, {
    dateValue: `{${sPath}}`,
    valueFormat: "dd-MM-yyyy",
    displayFormat: "dd-MM-yyyy"
});

Typing wrong weird stuff into the field:
enter image description here
It does not recognize the invalid format.

But when I tried to write in this example, it does recognize.
enter image description here

What am I doing wrong?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
softshipper
  • 32,463
  • 51
  • 192
  • 400
  • In which format is the value behind the `sPath` in model stored? Is it a JS-date object? Is it in string? With which model are you working? JSONModel? Or ODataModel? If it's the latter model, let me know if this helps https://stackoverflow.com/a/47551731/5846045 – Boghyon Hoffmann Oct 12 '18 at 11:29
  • The value of the `sPath` is `CharInput>/ZPM_TEST_6`. It is a JSON object it has the type null at initial. – softshipper Oct 12 '18 at 11:31
  • The value should be the type of `Date`. That because, I set it to `null` on the initialization point. – softshipper Oct 12 '18 at 11:36

2 Answers2

1
const oDatePicker = new DatePicker(sId).bindValue({
  path: sPath,
  type: new DateType({ // "sap/ui/model/type/Date"
    pattern: "dd-MM-yyyy",
  })
});
const messageManager = sap.ui.getCore().getMessageManager();
messageManager.registerObject(oDatePicker, true);
return oDataPicker;
  1. If working with data binding, you'll need to bind the value property instead of dateValue.

    API reference: sap.m.DatePicker

    • Use the value property if you want to bind the DatePicker to a model using the sap.ui.model.type.Date.
    • Use the dateValue property if the date is already provided as a JavaScript Date object or you want to work with a JavaScript Date object. (...) Although possible to bind it, the recommendation is not to do it. When binding is needed, use value property instead.
  2. And finally, register the control to the MessageManager or enable handleValidation. UI5 will then take care of displaying the error message if the input could not be parsed or violates given constraints.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
0

https://sapui5.hana.ondemand.com/#/sample/sap.m.sample.DatePicker/code/Group.controller.js

handleChange: function (oEvent) {
        var oText = this.byId("T1");
        var oDP = oEvent.oSource;
        var sValue = oEvent.getParameter("value");
        var bValid = oEvent.getParameter("valid");
        this._iEvent++;
        oText.setText("Change - Event " + this._iEvent + ": DatePicker " + oDP.getId() + ":" + sValue);

        if (bValid) {
            oDP.setValueState(sap.ui.core.ValueState.None);
        } else {
            oDP.setValueState(sap.ui.core.ValueState.Error);
        }
    }

this is the change handler used in the sample you have to implement the error handling by yourself