Skip to Content
author's profile photo Former Member
Former Member

Validation of timepickers on submit button click

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.");

}

}

});

});

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Sep 30, 2016 at 08:29 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 28, 2016 at 10:57 AM

    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"/>
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.