-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathvalidation.js
More file actions
125 lines (113 loc) 路 5.11 KB
/
Copy pathvalidation.js
File metadata and controls
125 lines (113 loc) 路 5.11 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
/**
* DataFormsJS [app.validation] Extension.
*
* If loaded then this file is used with:
* DataFormsJS\js\pages\entryForm.js
* DataFormsJS\js\plugins\listEditor.js
*
* If you would like to use your own validation with one of the above files
* then simply copy and modify this file for your project.
*/
/* Validates with both [jshint] and [eslint] */
/* global app */
/* jshint strict: true */
/* eslint-env browser */
/* eslint quotes: ["error", "single", { "avoidEscape": true }] */
/* eslint strict: ["error", "function"] */
/* eslint spaced-comment: ["error", "always"] */
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
(function() {
'use strict';
var validation = {
text: {
requiredField: '[{field}] is a required field.',
typeNumberInt: '[{field}] needs to be entered as a integer.',
typeNumber: '[{field}] needs to be entered as a number.',
row: 'Row',
},
validateInput: function(element, fieldName, rowIndex, listLabel) {
// Make sure an valid element was passed
var elementType = element.nodeName;
if (!(elementType === 'INPUT' || elementType === 'SELECT' || elementType === 'TEXTAREA')) {
throw new TypeError('Error invalid function call at [DataFormsJS.validation.validateInput]');
}
// Get the Form field value
// If a input/select has a [data-value] attribute defined
// then use that instead of the standard input value.
// This is usefull for plugins such as an inputmask or custom
// code that defines a different value to save than to show.
var value = element.getAttribute('data-value');
if (value === null) {
if (element.type === 'checkbox') {
value = element.checked;
} else {
value = element.value;
}
}
// HTML [required] attribute
var errorText = null;
if (element.required && String(value).trim() === '') {
errorText = validation.text.requiredField;
}
// If [date-type] is specified then validate the type.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
if (value !== '') {
var numValue = Number(value);
var dataType = element.getAttribute('data-type');
if (dataType === 'int') {
if (!(isFinite(numValue) && Math.floor(numValue) === numValue)) {
errorText = validation.text.typeNumberInt;
}
} else if (dataType === 'float') {
if (!isFinite(numValue)) {
errorText = validation.text.typeNumber;
}
}
}
// Update Error text with field name
if (errorText) {
// Get field name to display to the user.
// Get the related <label> element if one exists.
var fieldLabel = null;
if (element.id !== '') {
fieldLabel = document.querySelector('label[for="' + element.id + '"]');
if (fieldLabel !== null) {
fieldLabel = fieldLabel.textContent;
}
}
if (fieldLabel === null) {
fieldLabel = element.getAttribute('data-label');
}
if (fieldLabel === null) {
// [fieldName] is supplied as an optional parameter to the function.
// It is used by [entryForm.js] for validation, however <label> is
// the priority as it is what the user would read and is standard HTML.
fieldLabel = (typeof fieldName === 'string' ? fieldName : null);
}
if (fieldLabel === null) {
fieldLabel = (element.name ? element.name : element.id);
}
if (fieldLabel === '') {
fieldLabel = 'Form Field';
}
// Row index and Table Name can be passed to build messages when there are multiple records in a list
if (rowIndex) {
fieldLabel = validation.text.row + ' ' + String(rowIndex) + ', ' + fieldLabel;
}
if (listLabel) {
fieldLabel = listLabel + ', ' + fieldLabel;
}
// Update Error Text with field name. [undefined] could happen if a value
// such as [validation.text.requiredField] were overwritten by other code.
if (errorText === undefined) {
errorText = 'Error with field [' + fieldLabel + '].';
} else {
errorText = errorText.replace(/{field}/g, fieldLabel);
}
}
// Returns null or an error message
return errorText;
},
};
app.validation = validation;
})();