3 * library/validation/validation_script.js
5 * Validation functions that work with the validate.js library
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see
17 * http://www.gnu.org/licenses/licenses.html#GPL .
20 * @license http://www.gnu.org/licenses/licenses.html#GPL GNU GPL V3+
21 * @author Sharon Cohen <sharonco@matrix.co.il>
22 * @author Amiel Elboim <amielel@matrix.co.il>
23 * @link http://www.open-emr.org
27 /*If the validation (new) that uses the validate.js library was set on "on" in globals include the following libraries*/
28 if($GLOBALS['new_validate']) {
30 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative'] ?>/moment-2-13-0/moment.js"></script
>
31 <script type
="text/javascript" src
="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_modified.js"></script
>
32 <script type
="text/javascript" src
="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_extend.js"></script
>
37 <script language
='JavaScript'>
38 <?php
/*Added 2 parameters to the already existing submitme form*/
39 /*new validate: Use the new validation library (comes from globals)*/
41 /*form id: used to get the validation rules*/?
>
43 function submitme(new_validate
,e
,form_id
) {
45 //Use the old validation script if no parameter sent (backward compatibility)
46 //if we want to use the "old" validate function (set in globals) the validate function that will be called is the one that
47 // was on code up to today (the validat library was not loaded ( look at the top of this file)
48 if (new_validate
!== 1) {
49 var f
= document
.forms
[0];
51 somethingChanged
= false;
54 } else { //If there was an error prevent the form submit in order to display them
57 } else { //If the new validation library is used :
59 /*Get the constraint from the DB-> LBF forms accordinf the form_id*/
60 $constraints = LBF_Validation
::generate_validate_constraints($form_id);
62 //Variables used for the validation library and validation mechanism
64 var constraints
= <?php
echo $constraints;?
>;
65 //We use a common error for all the errors because of the multilanguage capability of openemr
66 //TODO: implement a traslation mechanism of the errors that the library returns
67 var error_msg
='<?php echo xl('is not valid
');?>';
68 var form
= document
.querySelector("form#"+form_id
);
70 //gets all the "elements" in the form and sends them to the validate library
71 //for more information @see https://validatejs.org/
72 var elements
= validate
.collectFormValues(form
);
74 //custom validate for multiple select(failed validate.js)
75 //the validate js cannot handle the LBF multiple select fields
78 for(var key in elements
){
80 element
= $
('[name="'+ key +
'"]');
82 if($
(element
).is('select[multiple]')) {
84 new_key
= key
.substring(0, key
.length
- 2);
85 if(validate
.isObject(constraints
[new_key
])) {
86 //check if select multiple does empty (empty or unassigned)
87 if(constraints
[new_key
].presence
&& (elements
[key
].length
== 0 || elements
[key
][0] == null )) {
89 appendError(element
, new_key
);
94 //remove multi select key to prevent errors
96 delete constraints
[new_key
];
100 //error conatins an list of the elements and their errors
101 var errors
= validate(elements
, constraints
);
102 if (typeof errors
!== 'undefined') {
103 //prevent default if trigger is submit button
104 if(typeof (e
) !== 'undefined') {
107 showErrors(form
, errors
);
110 somethingChanged
= false;
113 //In case there were errors they are displayed with this functionn
114 function showErrors(form
, errors
) {
116 for (var key in errors
) {
117 element
= $
('[name="'+ key +
'"]');
118 if (errors
.hasOwnProperty(key
)) {
120 appendError(element
, key
)
125 * append 'span' with error message
127 function appendError(input
, id
){
129 //append 'span' tag for error massages if not exist
130 if($
("#error_" + id
).length
== 0) {
131 //If have another element after the input
132 if($
(input
).next().length
> 0) {
134 $
(input
).next().after("<span id='error_" + id +
"' class='error-message' '></span>");
137 $
(input
).after("<span id='error_" + id +
"' class='error-message'></span>");
142 var title
= $
(input
).attr('title');
143 if(title
== undefined
) { title
= "" }
144 $
("#error_" + id
).text(title +
' '+error_msg
);
146 $
(input
).addClass('error-border');
149 var parent_div
= $
(input
).parents('div.tab');
150 if($
(parent_div
).is('div')) {
151 var div_id
= $
(parent_div
).attr('id');
152 var type_tab
= div_id
.substr(4);
153 $
('a#header_tab_'+type_tab
).css('color', 'red');
156 //open tab for new patient form
157 parent_div
= $
(input
).parents('div.section');
158 if(parent_div
!== undefined
) {
159 $
(parent_div
).css('display' , 'block');
160 div_id
= $
(parent_div
).attr('id');
161 if(div_id
!== undefined
) {
162 div_id
= div_id
.substr(-1);
163 var input_checkbox
= document
.getElementById('form_cb_' + div_id
);
164 input_checkbox
.checked
= true;
169 //bind hide function on focus/select again
170 $
(input
).on('click focus select', function(){
171 hideErrors(this
, id
);
173 //for datepicker button
174 if($
(input
).next().is('img')){
175 $
(input
).next().click(function(){
177 hideErrors($
(this
).prev(), id
);
186 function hideErrors(input
, id
){
187 $
(input
).removeClass('error-border');
188 $
("#error_" + id
).text('');
190 var parent_div
= $
(input
).parents('div.tab');
191 if($
(parent_div
).is('div')) {
192 var div_id
= $
(parent_div
).attr('id');
193 var type_tab
= div_id
.substr(4);
194 $
('a#header_tab_'+type_tab
).css('color', 'black');