forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_script.js.php
262 lines (228 loc) · 11.9 KB
/
validation_script.js.php
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
<?php
/**
* library/validation/validation_script.js
*
* Validation functions that work with the validate.js library
*
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* http://www.gnu.org/licenses/licenses.html#GPL .
*
* @package OpenEMR
* @license http://www.gnu.org/licenses/licenses.html#GPL GNU GPL V3+
* @author Sharon Cohen <[email protected]>
* @author Amiel Elboim <[email protected]>
* @link http://www.open-emr.org
*/
require_once($GLOBALS['srcdir'] . "/validation/LBF_Validation.php");
/*LBF form take the valude from the global $GLOBALS['new_validate'];*/
/*Other pages depend if the page in the lists options (page validation)is active and exists)*/
if ($use_validate_js) {
?>
<script src="<?php echo $GLOBALS['assets_static_relative'] ?>/moment/moment.js"></script>
<script src="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_modified.js"></script>
<script src="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_extend.js"></script>
<?php
}
?>
<script>
<?php /*Added 2 parameters to the already existing submitme form*/
/*new validate: Use the new validation library (comes from globals)*/
/*e: event*/
/*form id: used to get the validation rules*/?>
var g_date_format='<?php echo $GLOBALS["date_display_format"];?>';
function submitme(new_validate,e,form_id, constraints) {
top.restoreSession();
//Use the old validation script if no parameter sent (backward compatibility)
//if we want to use the "old" validate function (set in globals) the validate function that will be called is the one that
// was on code up to today (the validate library was not loaded ( look at the top of this file)
if (new_validate !== 1) {
var f = document.forms[0];
if (validate(f)) {
somethingChanged = false;
f.submit();
} else { //If there was an error prevent the form submit in order to display them
e.preventDefault();
}
} else { //If the new validation library is used :
//Variables used for the validation library and validation mechanism
/*Get the constraint from the DB-> LBF forms accordinf the form_id*/
if(constraints==undefined || constraints=='') {
<?php $constraints = LBF_Validation::generate_validate_constraints($form_id ?? '');?>
constraints = <?php echo $constraints;?>;
}
var valid = true ;
//We use a common error for all the errors because of the multilanguage capability of openemr
var form = document.querySelector("form#"+form_id);
//gets all the "elements" in the form and sends them to the validate library
//for more information @see https://validatejs.org/
var elements = validate.collectFormValues(form);
var element, new_key;
//before catch all values - clear filed that in display none, this will enable to fail on this fields.
for(var key in elements){
//catch th element with the name because the id of select-multiple contain '[]'
// and jquery throws error in those situation
element = $('[name="'+ key + '"]');
if(!$(element).is('select[multiple]')) {
if($(element).parent().prop('style') != undefined && ($(element).parent().prop('style').visibility == 'hidden'|| element.parent().parent().css('display')== 'none')){
$(element).val("");
}
}
}
//get the input value after romoving hide fields
elements = validate.collectFormValues(form);
//custom validate for multiple select(failed validate.js)
//the validate js cannot handle the LBF multiple select fields
for(var key in elements){
element = $('[name="'+ key + '"]');
if($(element).is('select[multiple]')) {
new_key = key.substring(0, key.length - 2);
if(validate.isObject(constraints[new_key])) {
//check if select multiple does empty (empty or unassigned)
if(constraints[new_key].presence && (elements[key].length == 0 || elements[key][0] == null )) {
appendError(element, new_key);
e.preventDefault();
valid = false;
}
}
//remove multi select key to prevent errors
delete elements[key];
delete constraints[new_key];
}
}
//error conatins an list of the elements and their errors
//set false full message because the name of the input not can be translated
var errors = validate(elements, constraints, {fullMessages: false});
if (typeof errors !== 'undefined') {
//prevent default if trigger is submit button
if(typeof (e) !== 'undefined') {
e.preventDefault();
}
showErrors(form, errors);
valid = false;
}else{
somethingChanged = false;
}
//In case there were errors they are displayed with this functionn
function showErrors(form, errors) {
for (var key in errors) {
element = $('[name="'+ key + '"]');
if (errors.hasOwnProperty(key)) {
appendError(element, key, errors[key][0])
}
}
}
/**
* append 'span' with error message
*/
function appendError(input, id, message){
//append 'span' tag for error massages if not exist
if($("#error_" + id).length == 0) {
//If have another element after the input
if($(input).next().length > 0) {
$(input).next().after("<span id='error_" + id +"' class='error-message' '></span>");
} else {
$(input).after("<span id='error_" + id +"' class='error-message'></span>");
}
}
//show error message
//Validate.js enables to overwrite the error messages by adding 'message' to constraints json. if you want to use your custom message instead
//default message you need to add boolean property to the constraints json - 'custom_messages':true.
var error_msg = (typeof constraints.custom_messages !== 'undefined' && constraints.custom_messages) ? message : getErrorMessage(message);
var title= $(input).attr('title');
//if it's long title remove it from error message (this could destroy the UI)
if(title == undefined || title.length > 20) { title = "" }
$("#error_" + id).text(title +' '+error_msg);
$(input).addClass('error-border');
//mark the tub
var parent_div = $(input).parents('div.tab');
if($(parent_div).is('div')) {
var div_id = $(parent_div).attr('id');
var type_tab = div_id.substr(4);
$('a#header_tab_'+type_tab).css('color', 'var(--danger)');
}
//open tab for new patient form
parent_div = $(input).parents('div.section');
if(parent_div !== undefined) {
$(parent_div).css('display' , 'block');
div_id = $(parent_div).attr('id');
if(div_id !== undefined) {
div_id = div_id.substr(-1);
var input_checkbox = document.getElementById('form_cb_' + div_id);
input_checkbox.checked = true;
}
}
//bind hide function on focus/select again
$(input).on('click focus select', function(){
hideErrors(this, id);
});
//for datepicker button
if($(input).next().is('img')){
$(input).next().click(function(){
hideErrors($(this).prev(), id);
});
}
}
/*
* hide error message
* @param element
**/
function hideErrors(input, id){
$(input).removeClass('error-border');
$("#error_" + id).text('');
var parent_div = $(input).parents('div.tab');
if($(parent_div).is('div')) {
var div_id = $(parent_div).attr('id');
var type_tab = div_id.substr(4);
$('a#header_tab_'+type_tab).css('color', 'var(--black)');
}
}
/*
* Check if exist translation for current error message else return default message.
* In addition you can adding custom error message to the constraints json according validate.js instructions and add the translation here
* @param message
**/
function getErrorMessage(message){
//enable to translate error message
//todo - adding all the translations string from validate.js
// console.log(message);
switch (message){
case 'Patient Name Required':
return <?php echo xlj('Patient Name Required');?>;
case 'An end date later than the start date is required for repeated events!':
return <?php echo xlj('An end date later than the start date is required for repeated events!');?>;
case 'Required field missing: Please enter the User Name':
return <?php echo xlj('Required field missing: Please enter the User Name');?>;
case 'Please enter the password':
return <?php echo xlj('Please enter the password'); ?>;
case 'Required field missing: Please enter the First name':
return <?php echo xlj('Required field missing: Please enter the First name');?>;
case 'Required field missing: Please enter the Last name':
return <?php echo xlj('Required field missing: Please enter the Last name');?>;
case 'Please choose a patient':
return <?php echo xlj('Please choose a patient');?>;
case 'Must be future date':
return <?php echo xlj('Must be future date');?>;
case 'Recipient required unless status is Done':
return <?php echo xlj('Recipient required unless status is Done');?>;
default:
return <?php echo xlj('is not valid');?>;
}
}
//the result of validation
return valid;
}
}
//enable submit button until load submitme function
if(document.getElementById('submit_btn') != null) {
document.getElementById('submit_btn').disabled = false;
}
</script>