forked from bsy/react-native-gifted-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GiftedFormManager.js
331 lines (282 loc) · 8.58 KB
/
GiftedFormManager.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
var validatorjs = require('validator');
function doValidateOne(k = '', value = undefined, validators = {}) {
var isValid = null;
var validate = validators.validate || [];
var result = [];
for (var i = 0; i < validate.length; i++) {
if (!validate[i].validator || validate[i].validator === 'undefined') { continue; }
var args = validate[i].arguments;
args = !Array.isArray(args) ? [ args ] : args;
var clonedArgs = args.slice(0);
validate[i].message = validate[i].message || '';
var message = validate[i].message.replace('{PATH}', "'"+k+"'");
var title = validators.title;
if (title) {
message = message.replace('{TITLE}', title);
}
message = message.replace(/{ARGS\[(\d+)\]}/g, function (replace, argIndex) {
var val = args[argIndex];
return val !== undefined ? val : '';
});
if (typeof validate[i].validator === 'function') {
clonedArgs.unshift(value);
isValid = validate[i].validator.apply(null, clonedArgs);
// handle custom validators
result.push({
validator: 'Custom',
isValid,
message,
value,
title: title || k,
});
} else {
if (typeof validatorjs[validate[i].validator] === 'undefined') {
console.warn('GiftedForm Error: Validator is not correct for: '+k);
continue;
}
clonedArgs.unshift(typeof value === 'undefined' || value === null ? '' : value.toString());
if (validate[i].validator === 'isLength') {
if (typeof clonedArgs[0] === 'string') {
clonedArgs[0] = clonedArgs[0].trim();
}
}
// Validator ONLY accepts string arguments.
if (clonedArgs[0] === null || clonedArgs[0] === undefined) {
clonedArgs[0] = '';
} else {
clonedArgs[0] = String(clonedArgs[0]);
}
isValid = validatorjs[validate[i].validator].apply(null, clonedArgs);
result.push({
validator: validate[i].validator,
isValid,
message,
value: clonedArgs[0],
title: title || k,
}); }
}
return result;
}
function doParseResult(result = [], name = '') {
var isValid = true;
var message = '';
for (var i = 0; i < result.length; i++) {
if (result[i].isValid === false) {
isValid = false;
message = result[i].message;
break;
}
}
return {
name,
isValid,
message,
};
}
// the select widgets values need to be formated because even when the values are 'False' they are stored
// formatValues return only selected values of select widgets
function formatValues(values) {
var formatted = {};
for (var key in values) {
if (values.hasOwnProperty(key)) {
if (typeof values[key] === 'boolean') {
var position = key.indexOf('{');
if (position !== -1) {
if (values[key] === true) {
// Each options of SelectWidget are stored as boolean and grouped using '{' and '}'
// eg:
// gender{male} = true
// gender{female} = false
var newKey = key.substr(0, position);
var newValue = key.substr(position);
newValue = newValue.replace('{', '');
newValue = newValue.replace('}', '');
if (!Array.isArray(formatted[newKey])) {
formatted[newKey] = [];
}
formatted[newKey].push(newValue);
}
} else {
formatted[key] = values[key];
}
} else {
if (typeof values[key] === 'string') {
formatted[key] = values[key].trim();
} else {
formatted[key] = values[key];
}
}
}
}
return formatted;
}
class Manager {
stores = {};
// ===================
// = ACTIONS SECTION =
// ===================
updateValue(formName, name, value) {
this.handleUpdateValue({
name,
formName,
value
});
}
updateValueIfNotSet(formName, name, value) {
this.handleUpdateValueIfNotSet({
name,
formName,
value
});
}
setValidators(formName, name, validators = {}) {
this.handleSetValidators({
name,
formName,
validators,
});
}
getValidators(formName, name) {
var state = this.stores;
if (typeof state[formName] !== 'undefined') {
if (typeof state[formName].validators === 'object') {
if (typeof state[formName].validators[name] !== 'undefined') {
return state[formName].validators[name];
}
}
}
return {};
}
// reset only the values + validators + etc.
// if you reset the validators, you need to re-mount the form
reset(formName) {
this.handleReset(formName);
}
// reset only the values
// useful if the GiftedForm is not unmounted
resetValues(formName) {
this.handleResetValues(formName);
}
clearSelect(formName, name) {
this.handleClearSelect({
name,
formName,
});
}
validateAndParseOne(k = '', value = undefined, validators = {}) {
return doParseResult(doValidateOne(k, value, validators), k);
}
validate(formName) {
var formInfo = this.stores[formName] || {};
var validators = formInfo.validators || {};
var values = null;
if (typeof this.stores[formName] !== 'undefined') {
values = formatValues(this.stores[formName].values);
} else {
values = {};
}
var results = {};
for (let k in validators) {
if (validators.hasOwnProperty(k)) {
results[k] = doValidateOne(k, values[k], validators[k]);
}
}
for (let k in results) {
if (results.hasOwnProperty(k)) {
for (let i = 0; i < results[k].length; i++) {
if (results[k][i].isValid === true) {
} else if (results[k][i].isValid === false) {
return {isValid: false, results: results};
}
}
}
}
return {isValid: true, results};
}
getValues(formName) {
if (typeof this.stores[formName] !== 'undefined') {
return formatValues(this.stores[formName].values);
}
return {};
}
getValue(formName, name) {
if (typeof this.stores[formName] !== 'undefined') {
if (typeof this.stores[formName].values === 'object') {
var res = this.stores[formName].values;
var formatted = formatValues(res);
if (typeof formatted[name] !== 'undefined') {
return formatted[name];
}
}
}
return null;
}
// =================
// = STORE SECTION =
// =================
initForm(formName) {
if (typeof this.stores[formName] === 'undefined') {
this.stores[formName] = {values: {}, validators: {}};
}
}
handleSetValidators(obj) {
this.initForm(obj.formName);
this.stores[obj.formName].validators[obj.name] = {
validate: obj.validators.validate || [],
title: obj.validators.title || '',
};
}
handleUpdateValue(obj) {
this.initForm(obj.formName);
this.stores[obj.formName].values[obj.name] = obj.value;
}
handleReset(formName) {
this.initForm(formName);
this.stores[formName] = {values: {}, validators: {}};
}
handleResetValues(formName) {
this.initForm(formName);
if (typeof this.stores[formName] === 'undefined') {
this.stores[formName] = {};
}
this.stores[formName].values = {};
}
handleClearSelect(obj) {
this.initForm(obj.formName);
for (var key in this.stores[obj.formName].values) {
if (this.stores[obj.formName].values.hasOwnProperty(key)) {
if (key.indexOf(obj.name) === 0) {
// console.log('CLEARING '+key);
delete this.stores[obj.formName].values[key];
}
}
}
}
handleUpdateValueIfNotSet(obj) {
this.initForm(obj.formName);
if (typeof this.stores[obj.formName].values[obj.name] === 'undefined') {
this.stores[obj.formName].values[obj.name] = obj.value;
}
}
getValidationErrors(validated, notValidMessage = '{TITLE} Invalid', requiredMessage = '{TITLE} Required') {
var errors = [];
if (validated.isValid === false) {
for (var k in validated.results) {
if (validated.results.hasOwnProperty(k)) {
for (var j in validated.results[k]) {
if (validated.results[k].hasOwnProperty(j)) {
if (validated.results[k][j].isValid === false) {
let defaultMessage = !!validated.results[k][j].value ? notValidMessage : requiredMessage;
errors.push(validated.results[k][j].message || defaultMessage.replace('{TITLE}', validated.results[k][j].title));
// displaying only 1 error per widget
break;
}
}
}
}
}
}
return errors;
}
}
module.exports = new Manager();