Minor fixes in client side validation, take 2.
[openemr.git] / library / validation / validation_script.js.php
blob13ade1fea4f435524765e1ed92b57465056d2581
1 <?php require_once($GLOBALS['srcdir'] . "/validation/LBF_Validation.php");?>
2 <?php
3 /**
4 * library/validation/validation_script.js
6 * Validation functions that work with the validate.js library
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * http://www.gnu.org/licenses/licenses.html#GPL .
20 * @package OpenEMR
21 * @license http://www.gnu.org/licenses/licenses.html#GPL GNU GPL V3+
22 * @author Sharon Cohen <sharonco@matrix.co.il>
23 * @author Amiel Elboim <amielel@matrix.co.il>
24 * @link http://www.open-emr.org
28 /*If the validation (new) that uses the validate.js library was set on "on" in globals include the following libraries*/
29 if($GLOBALS['new_validate']) {
31 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative'] ?>/moment-2-13-0/moment.js"></script>
32 <script type="text/javascript" src="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_modified.js"></script>
33 <script type="text/javascript" src="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_extend.js"></script>
34 <?php
38 <script language='JavaScript'>
39 <?php /*Added 2 parameters to the already existing submitme form*/
40 /*new validate: Use the new validation library (comes from globals)*/
41 /*e: event*/
42 /*form id: used to get the validation rules*/?>
46 function submitme(new_validate,e,form_id, constraints) {
48 //Use the old validation script if no parameter sent (backward compatibility)
49 //if we want to use the "old" validate function (set in globals) the validate function that will be called is the one that
50 // was on code up to today (the validate library was not loaded ( look at the top of this file)
51 if (new_validate !== 1) {
52 var f = document.forms[0];
53 if (validate(f)) {
54 somethingChanged = false;
55 top.restoreSession();
56 f.submit();
57 } else { //If there was an error prevent the form submit in order to display them
58 e.preventDefault();
60 } else { //If the new validation library is used :
62 //Variables used for the validation library and validation mechanism
63 /*Get the constraint from the DB-> LBF forms accordinf the form_id*/
64 if(constraints==undefined || constraints=='') {
65 <?php $constraints = LBF_Validation::generate_validate_constraints($form_id);?>
66 constraints = <?php echo $constraints;?>;
68 var valid = true ;
70 //We use a common error for all the errors because of the multilanguage capability of openemr
71 //TODO: implement a traslation mechanism of the errors that the library returns
72 var error_msg ='<?php echo xl('is not valid');?>';
73 var form = document.querySelector("form#"+form_id);
74 //gets all the "elements" in the form and sends them to the validate library
75 //for more information @see https://validatejs.org/
76 var elements = validate.collectFormValues(form);
77 var element, new_key;
79 //before catch all values - clear filed that in display none, this will enable to fail on this fields.
80 for(var key in elements){
81 //catch th element with the name because the id of select-multiple contain '[]'
82 // and jquery throws error in those situation
83 element = $('[name="'+ key + '"]');
84 if(!$(element).is('select[multiple]')) {
86 if($(element).parent().prop('style') != undefined && $(element).parent().prop('style').visibility == 'hidden'){
87 console.log(element);
88 $(element).val("");
93 //get the input value after romoving hide fields
94 elements = validate.collectFormValues(form);
95 //custom validate for multiple select(failed validate.js)
96 //the validate js cannot handle the LBF multiple select fields
97 for(var key in elements){
99 element = $('[name="'+ key + '"]');
101 if($(element).is('select[multiple]')) {
103 new_key = key.substring(0, key.length - 2);
104 if(validate.isObject(constraints[new_key])) {
105 //check if select multiple does empty (empty or unassigned)
106 if(constraints[new_key].presence && (elements[key].length == 0 || elements[key][0] == null )) {
108 appendError(element, new_key);
109 e.preventDefault();
110 valid = false;
113 //remove multi select key to prevent errors
114 delete elements[key];
115 delete constraints[new_key];
119 //error conatins an list of the elements and their errors
120 var errors = validate(elements, constraints);
121 if (typeof errors !== 'undefined') {
122 //prevent default if trigger is submit button
123 if(typeof (e) !== 'undefined') {
124 e.preventDefault();
126 showErrors(form, errors);
127 valid = false;
128 }else{
129 somethingChanged = false;
132 //In case there were errors they are displayed with this functionn
133 function showErrors(form, errors) {
135 for (var key in errors) {
136 element = $('[name="'+ key + '"]');
137 if (errors.hasOwnProperty(key)) {
139 appendError(element, key)
144 * append 'span' with error message
146 function appendError(input, id){
148 //append 'span' tag for error massages if not exist
149 if($("#error_" + id).length == 0) {
150 //If have another element after the input
151 if($(input).next().length > 0) {
153 $(input).next().after("<span id='error_" + id +"' class='error-message' '></span>");
155 } else {
156 $(input).after("<span id='error_" + id +"' class='error-message'></span>");
160 //show error message
161 var title= $(input).attr('title');
162 if(title == undefined) { title = "" }
163 $("#error_" + id).text(title +' '+error_msg);
165 $(input).addClass('error-border');
167 //mark the tub
168 var parent_div = $(input).parents('div.tab');
169 if($(parent_div).is('div')) {
170 var div_id = $(parent_div).attr('id');
171 var type_tab = div_id.substr(4);
172 $('a#header_tab_'+type_tab).css('color', 'red');
175 //open tab for new patient form
176 parent_div = $(input).parents('div.section');
177 if(parent_div !== undefined) {
178 $(parent_div).css('display' , 'block');
179 div_id = $(parent_div).attr('id');
180 if(div_id !== undefined) {
181 div_id = div_id.substr(-1);
182 var input_checkbox = document.getElementById('form_cb_' + div_id);
183 input_checkbox.checked = true;
188 //bind hide function on focus/select again
189 $(input).on('click focus select', function(){
190 hideErrors(this, id);
192 //for datepicker button
193 if($(input).next().is('img')){
194 $(input).next().click(function(){
196 hideErrors($(this).prev(), id);
202 * hide error message
203 * @param element
204 * */
205 function hideErrors(input, id){
206 $(input).removeClass('error-border');
207 $("#error_" + id).text('');
209 var parent_div = $(input).parents('div.tab');
210 if($(parent_div).is('div')) {
211 var div_id = $(parent_div).attr('id');
212 var type_tab = div_id.substr(4);
213 $('a#header_tab_'+type_tab).css('color', 'black');
216 return valid;
219 //enable submit button until load submitme function
220 if(document.getElementById('submit_btn') != null){
221 document.getElementById('submit_btn').disabled = false;
223 </script>