cancel
Showing results for 
Search instead for 
Did you mean: 

Validation of timepickers on submit button click

Former Member
0 Kudos

Hello all,

I have a problem with the validation of timepicker fields. If i fill the fields and click on the submit button, the fields are displayed as empty fields. (see screenshot and code below)

Does anyone have an idea, what the problem could be?

Many thanks in advance!

Fahri

This is my view:

<mvc:View controllerName="validation.controller.View" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" xmlns:m="sap.m">

  <m:App>

  <m:pages>

  <m:Page title="{i18n>title}">

  <m:VBox>

  <m:Label text="Start" required="true"/>

  <m:TimePicker value="" valueStateText="test" id="TP1" valueFormat="HH:mm" displayFormat="HH:mm" width="20%" placeholder="Enter time..."/>

  <m:Label text="End" required="true"/>

  <m:TimePicker value="" valueStateText="test" id="TP2" valueFormat="HH:mm" displayFormat="HH:mm" width="20%" placeholder="Enter time..."/>

  <m:Button id="save" text="submit" type="Accept" width="20%" press="saveButton" class="save"/>

  </m:VBox>

  </m:Page>

  </m:pages>

  </m:App>

</mvc:View>

This is my Controller:

sap.ui.define([

  "sap/ui/core/mvc/Controller", "sap/m/MessageToast", "sap/m/MessageBox", "jquery.sap.global"

], function(Controller, MessageToast, MessageBox, jQuery) {

  "use strict";

  return Controller.extend("validation.controller.View", {

  onInit: function() {

  sap.ui.getCore().attachValidationError(function(evt) {

  var control = evt.getParameter("element");

  if (control && control.setValueState) {

  control.setValueState("Error");

  }

  });

  sap.ui.getCore().attachValidationSuccess(function(evt) {

  var control = evt.getParameter("element");

  if (control && control.setValueState) {

  control.setValueState("None");

  }

  });

  },

  saveButton: function(evt) {

  var view = this.getView();

  var inputs = [

  view.byId("TP1"),

  view.byId("TP2")

  ];

  //Check that inputs are not empty

  jQuery.each(inputs, function(i, input) {

  if (!input.getValue()) {

  input.setValueState("Error");

  }

  });

  //check states of inputs

  var canContinue = true;

  jQuery.each(inputs, function(i, input) {

  if ("Error" === input.getValueState()) {

  canContinue = false;

  return false;

  }

  });

  if (canContinue) {

  MessageToast.show("The input is correct. You could now continue to the next screen.");

  } else {

  jQuery.sap.require("sap.m.MessageBox");

  MessageBox.alert("Complete your input first.");

  }

  }

  });

});

Accepted Solutions (1)

Accepted Solutions (1)

former_member227918
Active Contributor
0 Kudos

Hi Fahri,

no need to write attachValidationError in init function, that is not triggering at all and even not required in your scenario, and in each loop just add one line to validate the input to true.


check below link:

https://jsfiddle.net/Akhilesh_U/u40je6qg/

Regards,

Akhilesh

Answers (1)

Answers (1)

Former Member
0 Kudos

Please bind text value a model.

  1. <TimePicker
  2. id="TP1"
  3. value="{a>/Time1}"
  4. valueFormat="HH:mm"
  5. displayFormat="HH:mm"
  6. change="handleChange"
  7. placeholder="Enter meeting start time"/>