minor fix in hover for prior commit
[openemr.git] / library / validation / validation_script.js.php
blob8bf26537a9381dea4f82ecf73aad75d3587cb6a7
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 /*LBF form take the valude from the global $GLOBALS['new_validate'];*/
29 /*Other pages depend if the page in the lists options (page validation)is active and exists)*/
30 if($use_validate_js){
32 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative'] ?>/moment-2-13-0/moment.js"></script>
33 <script type="text/javascript" src="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_modified.js"></script>
34 <script type="text/javascript" src="<?php echo $GLOBALS['rootdir'] ?>/../library/js/vendors/validate/validate_extend.js"></script>
35 <?php
39 <script language='JavaScript'>
40 <?php /*Added 2 parameters to the already existing submitme form*/
41 /*new validate: Use the new validation library (comes from globals)*/
42 /*e: event*/
43 /*form id: used to get the validation rules*/?>
47 function submitme(new_validate,e,form_id, constraints) {
49 //Use the old validation script if no parameter sent (backward compatibility)
50 //if we want to use the "old" validate function (set in globals) the validate function that will be called is the one that
51 // was on code up to today (the validate library was not loaded ( look at the top of this file)
52 if (new_validate !== 1) {
53 var f = document.forms[0];
54 if (validate(f)) {
55 somethingChanged = false;
56 top.restoreSession();
57 f.submit();
58 } else { //If there was an error prevent the form submit in order to display them
59 e.preventDefault();
61 } else { //If the new validation library is used :
63 //Variables used for the validation library and validation mechanism
64 /*Get the constraint from the DB-> LBF forms accordinf the form_id*/
65 if(constraints==undefined || constraints=='') {
66 <?php $constraints = LBF_Validation::generate_validate_constraints($form_id);?>
67 constraints = <?php echo $constraints;?>;
69 var valid = true ;
71 //We use a common error for all the errors because of the multilanguage capability of openemr
72 var form = document.querySelector("form#"+form_id);
73 //gets all the "elements" in the form and sends them to the validate library
74 //for more information @see https://validatejs.org/
75 var elements = validate.collectFormValues(form);
76 var element, new_key;
78 //before catch all values - clear filed that in display none, this will enable to fail on this fields.
79 for(var key in elements){
80 //catch th element with the name because the id of select-multiple contain '[]'
81 // and jquery throws error in those situation
82 element = $('[name="'+ key + '"]');
83 if(!$(element).is('select[multiple]')) {
85 if($(element).parent().prop('style') != undefined && ($(element).parent().prop('style').visibility == 'hidden'|| element.parent().parent().css('display')== 'none')){
86 $(element).val("");
92 //get the input value after romoving hide fields
93 elements = validate.collectFormValues(form);
94 //custom validate for multiple select(failed validate.js)
95 //the validate js cannot handle the LBF multiple select fields
96 for(var key in elements){
98 element = $('[name="'+ key + '"]');
100 if($(element).is('select[multiple]')) {
102 new_key = key.substring(0, key.length - 2);
103 if(validate.isObject(constraints[new_key])) {
104 //check if select multiple does empty (empty or unassigned)
105 if(constraints[new_key].presence && (elements[key].length == 0 || elements[key][0] == null )) {
107 appendError(element, new_key);
108 e.preventDefault();
109 valid = false;
112 //remove multi select key to prevent errors
113 delete elements[key];
114 delete constraints[new_key];
118 //error conatins an list of the elements and their errors
119 //set false full message because the name of the input not can be translated
120 var errors = validate(elements, constraints, {fullMessages: false});
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)) {
138 appendError(element, key, errors[key][0])
143 * append 'span' with error message
145 function appendError(input, id, message){
147 //append 'span' tag for error massages if not exist
148 if($("#error_" + id).length == 0) {
149 //If have another element after the input
150 if($(input).next().length > 0) {
152 $(input).next().after("<span id='error_" + id +"' class='error-message' '></span>");
154 } else {
155 $(input).after("<span id='error_" + id +"' class='error-message'></span>");
159 //show error message
160 var error_msg = getErrorMessage(message);
162 var title= $(input).attr('title');
163 //if it's long title remove it from error message (this could destroy the UI)
164 if(title == undefined || title.length > 20) { title = "" }
165 $("#error_" + id).text(title +' '+error_msg);
167 $(input).addClass('error-border');
169 //mark the tub
170 var parent_div = $(input).parents('div.tab');
171 if($(parent_div).is('div')) {
172 var div_id = $(parent_div).attr('id');
173 var type_tab = div_id.substr(4);
174 $('a#header_tab_'+type_tab).css('color', 'red');
177 //open tab for new patient form
178 parent_div = $(input).parents('div.section');
179 if(parent_div !== undefined) {
180 $(parent_div).css('display' , 'block');
181 div_id = $(parent_div).attr('id');
182 if(div_id !== undefined) {
183 div_id = div_id.substr(-1);
184 var input_checkbox = document.getElementById('form_cb_' + div_id);
185 input_checkbox.checked = true;
190 //bind hide function on focus/select again
191 $(input).on('click focus select', function(){
192 hideErrors(this, id);
194 //for datepicker button
195 if($(input).next().is('img')){
196 $(input).next().click(function(){
198 hideErrors($(this).prev(), id);
204 * hide error message
205 * @param element
207 function hideErrors(input, id){
208 $(input).removeClass('error-border');
209 $("#error_" + id).text('');
211 var parent_div = $(input).parents('div.tab');
212 if($(parent_div).is('div')) {
213 var div_id = $(parent_div).attr('id');
214 var type_tab = div_id.substr(4);
215 $('a#header_tab_'+type_tab).css('color', 'black');
219 * Check if exist translation for current error message else return default message.
220 * In addition you can adding custom error message to the constraints json according validate.js instructions and add the translation here
221 * @param message
223 function getErrorMessage(message){
224 //enable to translate error message
225 //todo - adding all the translations string from validate.js
226 // console.log(message);
227 switch (message){
228 case 'Patient Name Required':
229 return '<?php echo xla('Patient Name Required');?>';
230 case 'An end date later than the start date is required for repeated events!':
231 return '<?php echo xla('An end date later than the start date is required for repeated events!');?>';
232 case 'Required field missing: Please enter the User Name':
233 return '<?php echo xla('Required field missing: Please enter the User Name','e');?>';
234 case 'Please enter the password':
235 return '<?php echo xla('Please enter the password'); ?>';
236 case 'Required field missing: Please enter the First name':
237 return '<?php echo xla('Required field missing: Please enter the First name');?>';
238 case 'Required field missing: Please enter the Last name':
239 return '<?php echo xla('Required field missing: Please enter the Last name');?>';
240 default:
241 return '<?php echo xla('is not valid');?>';
244 //the result of validation
245 return valid;
248 //enable submit button until load submitme function
249 if(document.getElementById('submit_btn') != null){
250 document.getElementById('submit_btn').disabled = false;
252 </script>