Extend the JS validation to support dd/mm/YYYY format (#1418)
[openemr.git] / library / validation / validation_script.js.php
blob353316107d7700b93c6234483696f764ec8ac0c4
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*/?>
45 var g_date_format='<?php echo $GLOBALS["date_display_format"];?>';
47 function submitme(new_validate,e,form_id, constraints) {
49 top.restoreSession();
51 //Use the old validation script if no parameter sent (backward compatibility)
52 //if we want to use the "old" validate function (set in globals) the validate function that will be called is the one that
53 // was on code up to today (the validate library was not loaded ( look at the top of this file)
54 if (new_validate !== 1) {
55 var f = document.forms[0];
56 if (validate(f)) {
57 somethingChanged = false;
58 f.submit();
59 } else { //If there was an error prevent the form submit in order to display them
60 e.preventDefault();
62 } else { //If the new validation library is used :
64 //Variables used for the validation library and validation mechanism
65 /*Get the constraint from the DB-> LBF forms accordinf the form_id*/
66 if(constraints==undefined || constraints=='') {
67 <?php $constraints = LBF_Validation::generate_validate_constraints($form_id);?>
68 constraints = <?php echo $constraints;?>;
70 var valid = true ;
72 //We use a common error for all the errors because of the multilanguage capability of openemr
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'|| element.parent().parent().css('display')== 'none')){
87 $(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 //set false full message because the name of the input not can be translated
121 var errors = validate(elements, constraints, {fullMessages: false});
122 if (typeof errors !== 'undefined') {
123 //prevent default if trigger is submit button
124 if(typeof (e) !== 'undefined') {
125 e.preventDefault();
127 showErrors(form, errors);
128 valid = false;
129 }else{
130 somethingChanged = false;
133 //In case there were errors they are displayed with this functionn
134 function showErrors(form, errors) {
136 for (var key in errors) {
137 element = $('[name="'+ key + '"]');
138 if (errors.hasOwnProperty(key)) {
139 appendError(element, key, errors[key][0])
144 * append 'span' with error message
146 function appendError(input, id, message){
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 error_msg = getErrorMessage(message);
163 var title= $(input).attr('title');
164 //if it's long title remove it from error message (this could destroy the UI)
165 if(title == undefined || title.length > 20) { title = "" }
166 $("#error_" + id).text(title +' '+error_msg);
168 $(input).addClass('error-border');
170 //mark the tub
171 var parent_div = $(input).parents('div.tab');
172 if($(parent_div).is('div')) {
173 var div_id = $(parent_div).attr('id');
174 var type_tab = div_id.substr(4);
175 $('a#header_tab_'+type_tab).css('color', 'red');
178 //open tab for new patient form
179 parent_div = $(input).parents('div.section');
180 if(parent_div !== undefined) {
181 $(parent_div).css('display' , 'block');
182 div_id = $(parent_div).attr('id');
183 if(div_id !== undefined) {
184 div_id = div_id.substr(-1);
185 var input_checkbox = document.getElementById('form_cb_' + div_id);
186 input_checkbox.checked = true;
191 //bind hide function on focus/select again
192 $(input).on('click focus select', function(){
193 hideErrors(this, id);
195 //for datepicker button
196 if($(input).next().is('img')){
197 $(input).next().click(function(){
199 hideErrors($(this).prev(), id);
205 * hide error message
206 * @param element
208 function hideErrors(input, id){
209 $(input).removeClass('error-border');
210 $("#error_" + id).text('');
212 var parent_div = $(input).parents('div.tab');
213 if($(parent_div).is('div')) {
214 var div_id = $(parent_div).attr('id');
215 var type_tab = div_id.substr(4);
216 $('a#header_tab_'+type_tab).css('color', 'black');
220 * Check if exist translation for current error message else return default message.
221 * In addition you can adding custom error message to the constraints json according validate.js instructions and add the translation here
222 * @param message
224 function getErrorMessage(message){
225 //enable to translate error message
226 //todo - adding all the translations string from validate.js
227 // console.log(message);
228 switch (message){
229 case 'Patient Name Required':
230 return '<?php echo xla('Patient Name Required');?>';
231 case 'An end date later than the start date is required for repeated events!':
232 return '<?php echo xla('An end date later than the start date is required for repeated events!');?>';
233 case 'Required field missing: Please enter the User Name':
234 return '<?php echo xla('Required field missing: Please enter the User Name', 'e');?>';
235 case 'Please enter the password':
236 return '<?php echo xla('Please enter the password'); ?>';
237 case 'Required field missing: Please enter the First name':
238 return '<?php echo xla('Required field missing: Please enter the First name');?>';
239 case 'Required field missing: Please enter the Last name':
240 return '<?php echo xla('Required field missing: Please enter the Last name');?>';
241 default:
242 return '<?php echo xla('is not valid');?>';
245 //the result of validation
246 return valid;
249 //enable submit button until load submitme function
250 if(document.getElementById('submit_btn') != null){
251 document.getElementById('submit_btn').disabled = false;
253 </script>