forked from qualiture/ui5-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.js
More file actions
167 lines (149 loc) · 7.65 KB
/
Validator.js
File metadata and controls
167 lines (149 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*global sap */
sap.ui.define([
"sap/ui/core/message/Message",
"sap/ui/core/MessageType",
"sap/ui/core/ValueState"
], function (Message, MessageType, ValueState) {
"use strict";
/**
* @name nl.qualiture.plunk.demo.utils.Validator
*
* @class
* @classdesc Validator class.<br/>
*
* @version Oktober 2015
* @author Robin van het Hof
*/
var Validator = function () {
this._isValid = true;
this._isValidationPerformed = false;
};
/**
* Returns true _only_ when the form validation has been performed, and no validation errors were found
* @memberof nl.qualiture.plunk.demo.utils.Validator
*
* @returns {boolean}
*/
Validator.prototype.isValid = function () {
return this._isValidationPerformed && this._isValid;
};
/**
* Recursively validates the given oControl and any aggregations (i.e. child controls) it may have
* @memberof nl.qualiture.plunk.demo.utils.Validator
*
* @param {(sap.ui.core.Control|sap.ui.layout.form.FormContainer|sap.ui.layout.form.FormElement)} oControl - The control or element to be validated.
* @return {boolean} whether the oControl is valid or not.
*/
Validator.prototype.validate = function (oControl) {
this._isValid = true;
sap.ui.getCore().getMessageManager().removeAllMessages();
this._validate(oControl);
return this.isValid();
};
/**
* Recursively validates the given oControl and any aggregations (i.e. child controls) it may have
* @memberof nl.qualiture.plunk.demo.utils.Validator
*
* @param {(sap.ui.core.Control|sap.ui.layout.form.FormContainer|sap.ui.layout.form.FormElement)} oControl - The control or element to be validated.
*/
Validator.prototype._validate = function (oControl) {
var aPossibleAggregations = ["items", "content", "form", "formContainers", "formElements", "fields", "sections", "subSections", "_grid", "cells", "_page"],
aControlAggregation = null,
oControlBinding = null,
aValidateProperties = ["value", "selectedKey", "text"], // yes, I want to validate Select and Text controls too
isValidatedControl = false,
oExternalValue, oInternalValue,
i, j, editable;
// only validate controls and elements which have a 'visible' property
// and are visible controls (invisible controls make no sense checking)
if (( oControl instanceof sap.ui.core.Control
|| oControl instanceof sap.ui.layout.form.FormContainer
|| oControl instanceof sap.ui.layout.form.FormElement
) && oControl.getVisible() ) {
// check control for any properties worth validating
for (i = 0; i < aValidateProperties.length; i += 1) {
if (oControl.getBinding(aValidateProperties[i])
// check if a data type exists (which may have validation constraints)
&& oControl.getBinding(aValidateProperties[i]).getType()
) {
try { editable = oControl.getProperty("editable"); }
catch (ex) { editable = true; }
if(editable) {
try { // try validating the bound value
oControlBinding = oControl.getBinding(aValidateProperties[i]);
oExternalValue = oControl.getProperty(aValidateProperties[i]);
oInternalValue = oControlBinding.getType().parseValue(oExternalValue, oControlBinding.sInternalType);
oControlBinding.getType().validateValue(oInternalValue);
}
catch (ex) { // catch any validation errors
this._isValid = false;
oControl.setValueState(ValueState.Error);
oControlBinding = oControl.getBinding(aValidateProperties[i]);
sap.ui.getCore().getMessageManager().addMessages(
new Message({
message : ex.message,
type : MessageType.Error,
target : ( oControlBinding.getContext() ? oControlBinding.getContext().getPath() + "/" : "") +
oControlBinding.getPath(),
processor: oControl.getBinding(aValidateProperties[i]).getModel()
})
);
}
isValidatedControl = true;
}
} else if (oControl.getRequired
&& oControl.getRequired() === true ) {
try {
oControlBinding = oControl.getBinding(aValidateProperties[i]);
oExternalValue = oControl.getProperty(aValidateProperties[i]);
if (!oExternalValue || oExternalValue==="") {
this._isValid = false;
var oMessage = "Please fill this mandatory field!";
oControl.setValueState(ValueState.Error, oMessage);
sap.ui.getCore().getMessageManager().addMessages(
new Message({
message: oMessage,
type: MessageType.Error,
target : ( oControlBinding.getContext() ? oControlBinding.getContext().getPath() + "/" : "") +
oControlBinding.getPath(),
processor: oControl.getBinding(aValidateProperties[i]).getModel()
})
);
} else if (oControl.getAggregation("picker")
&& oControl.getProperty("selectedKey").length === 0 ) { // might be a select
this._isValid = false;
//TODO: i18n this
oControl.setValueState(ValueState.Error, "Please choose an entry!");
} else {
oControl.setValueState(ValueState.None);
}
} catch (ex) {
// Validation failed
}
} else {
//oControl.setValueState(ValueState.None);
}
}
// if the control could not be validated, it may have aggregations
if (!isValidatedControl) {
for (i = 0; i < aPossibleAggregations.length; i += 1) {
aControlAggregation = oControl.getAggregation(aPossibleAggregations[i]);
if (aControlAggregation) {
// generally, aggregations are of type Array
if (aControlAggregation instanceof Array) {
for (j = 0; j < aControlAggregation.length; j += 1) {
this._validate(aControlAggregation[j]);
}
}
// ...however, with sap.ui.layout.form.Form, it is a single object *sigh*
else {
this._validate(aControlAggregation);
}
}
}
}
}
this._isValidationPerformed = true;
};
return Validator;
});