on 09-28-2016 10:17 AM
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.");
}
}
});
});
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please bind text value a model.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.