Framework  3.9
validation.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
41 require_once realpath(dirname(__FILE__))."/data_item.inc";
42 
49 {
50  var $field;
51  var $title;
52 
54  {
55  $this->field = $field;
56  $this->title = $title;
57  }
58 
59  function writeClient()
60  {
61  }
62 
63  function validate()
64  {
65  }
66 }
67 
76 {
77  var $empty = "";
78  var $checkDisplayNone = true;
79 
81  {
82  $this->empty = $empty;
84  $this->checkDisplayNone = $checkDisplayNone;
85  }
86 
87  function writeClient()
88  {
89  if ($this->readOnly) return "";
90 
91  if($this->checkDisplayNone)
92  $condition = "form[\"{$this->field}\"] != null && form[\"{$this->field}\"].style.display != \"none\" && ";
93 
94  $script = <<<ENDSCRIPT
95 
96  if ({$condition}form["{$this->field}"].value == "{$this->empty}")
97  {
98  alert("{$this->title} is a required field. Please supply a value.");
99  return false;
100  }
101 ENDSCRIPT;
102 
103  return $script;
104  }
105 
106  function validate()
107  {
108  global $_POST;
109 
110  if ($this->readOnly) return "";
111 
112  if (!isset($_POST[$this->field]) || $_POST[$this->field] === $this->empty)
113  {
114  return "{$this->title} is a required field.";
115  }
116  else
117  {
118  return "";
119  }
120  }
121 }
122 
123 
124 
132 {
133  var $empty = "";
134 
136  {
137  $this->empty = $empty;
139  }
140 
141  function writeClient()
142  {
143  if ($this->readOnly) return "";
144 
145  $script = <<<ENDSCRIPT
146 
147  var div = document.id("{$this->field}_table");
148  var valid = false;
149 
150  div.getElements("input").each(function(box)
151  {
152  if (box.checked == true)
153  valid = true;
154  });
155 
156  if(valid == false)
157  {
158  alert("{$this->title} is a required field. Please supply a value.");
159  return false;
160  }
161 ENDSCRIPT;
162 
163  return $script;
164  }
165 
166  function validate()
167  {
168  global $_POST;
169 
170  if ($this->readOnly) return "";
171 
172  if (!isset($_POST[$this->field]))
173  {
174  return "{$this->title} is a required field.";
175  }
176  else
177  {
178  return "";
179  }
180  }
181 }
182 
183 
184 /*
185  * When we must not set any of the buttons by default
186  * and the field is required, we need this validator
187  * instead of "RequiredValidator"
188  *
189  * JDG 1/14/2011
190  */
192 {
193  var $empty = "";
194 
196  {
197  $this->empty = $empty;
199  }
200 
201  function writeClient()
202  {
203  if ($this->readOnly) return "";
204 
205  $script = <<<ENDSCRIPT
206 
207  var valid = false;
208 
209  var radios = form["{$this->field}"];
210 
211  for(i=0; i < radios.length; i++)
212  {
213  if (radios[i].checked == true)
214  {
215  valid = true;
216  }
217  }
218 
219  if(valid == false)
220  {
221  alert("{$this->title} is a required field. Please supply a value.");
222  return false;
223  }
224 ENDSCRIPT;
225 
226  return $script;
227  }
228 
229 }
230 
231 
232 
234 {
235  var $message;
236 
238  {
240  $this->message = $message ? $message : "{$title} is a required field. Please browse for a file using the 'Browse' button";
241 
242  trace("RequiredValidator($field,$title,$message)", 3);
243  }
244 
245  function writeClient()
246  {
247  if ($this->readOnly) return "";
248 
249  $msg = jsSafe($this->message, true);
250  $script = <<<ENDSCRIPT
251 
252  if (form["{$this->field}"].style.display != "none" && form["{$this->field}"].value == "{$this->empty}")
253  {
254  alert("{$msg}");
255  return false;
256  }
257 ENDSCRIPT;
258 
259  return $script;
260  }
261 
262  function validate()
263  {
264 
265  if ($this->readOnly) return "";
266 
267  if (!isset($_FILES[$this->field]))
268  {
269  return $this->message;
270  }
271  else
272  {
273  return "";
274  }
275  }
276 }
277 
279 {
280  var $message;
282 
283  function RequiredIfCheckedValidator($field, $checkbox, $title, $checkboxTitle, $message = "")
284  {
286  $this->checkbox = $checkbox;
287 
288  $this->message = $message ? $message : "{$title} is a required field. Please supply a value or uncheck {$checkboxTitle}.";
289 
290  trace("RequiredIfCheckedValidator($field,$title,$checkboxTitle, $message)", 4);
291  }
292 
293  function writeClient()
294  {
295  if ($this->readOnly) return "";
296 
297  $msg = jsSafe($this->message, true);
298  $script = <<<ENDSCRIPT
299 
300  if (form["{$this->field}"].style.display != "none" && form["{$this->checkbox}"].checked && form["{$this->field}"].value == "{$this->empty}")
301  {
302  alert("{$msg}");
303  return false;
304  }
305 ENDSCRIPT;
306 
307  return $script;
308  }
309 
310  function validate()
311  {
312 
313  if ($this->readOnly) return "";
314 
315  if (!isset($_POST[$this->field]) && isset($_POST[$this->checkbox]))
316  {
317  return $this->message;
318  }
319  else
320  {
321  return "";
322  }
323  }
324 
325 }
326 
327 
338 {
340  {
341  $this->AbstractValidator($field, $title);
342  }
343 
344  function writeClient()
345  {
346  if ($this->readOnly) return "";
347 
348  $script .= <<<ENDSCRIPT
349  var d = form["{$this->field}"].value.split(/\D+/);
350 
351  if (form["{$this->field}"].value != "" &&
352  (!form["{$this->field}"].value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/) ||
353  (form["{$this->field}"].value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/) &&
354  (parseInt(d[0], 10) == 0 || parseInt(d[1], 10) == 0 || parseInt(d[2], 10) == 0))))
355  {
356  alert("{$this->title} must be in the format MM/DD/YYYY");
357  return false;
358  }
359 
360 ENDSCRIPT;
361 
362  return $script;
363  }
364 
365  function validate()
366  {
367  global $_POST;
368 
369  if ($this->readOnly) return "";
370 
371  list($dd,$mm,$yyyy) = explode('/', $_POST[$this->field]);
372 
373  if ($_POST[$this->field] != "" AND !preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $_POST[$this->field]) ||
374  (preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $_POST[$this->field]) &&
375  (intval($dd) == 0 || intval($mm) == 0 || intval($yyyy) == 0)))
376  {
377  $result = "{$this->title} must be in the format MM/DD/YYYY";
378  }
379 
380  return $result;
381  }
382 }
383 
390 {
391  var $min = 0;
392  var $message;
393 
395  {
397  $this->min = $min;
398  $this->message = $message;
399 
400  }
401 
402  function validate()
403  {
404  $mm = checkNumeric($_POST["{$this->field}_mm"]);
405  $dd = checkNumeric($_POST["{$this->field}_dd"]);
406  $yy = checkNumeric($_POST["{$this->field}_yy"]);
407 
408  if (!$mm && !$dd && !$yy)
409  {
410  return "";
411  }
412 
413  if (!$mm || !$dd || !$yy)
414  {
415  return "Incomplete date of birth - $mm/$dd/$yy";
416  }
417 
418  $dob = new DateTime("$mm/$dd/$yy");
419  $today = new DateTime();
420 
421  $age = $dob->diff($today);
422  if ($age->y < $this->min)
423  {
424  return $this->message;
425  }
426 
427  return "";
428  }
429 }
430 
437 {
438  var $max = 0;
439  var $message;
440 
442  {
444  $this->max = $max;
445  $this->message = $message;
446 
447  }
448 
449  function validate()
450  {
451  $mm = checkNumeric($_POST["{$this->field}_mm"]);
452  $dd = checkNumeric($_POST["{$this->field}_dd"]);
453  $yy = checkNumeric($_POST["{$this->field}_yy"]);
454 
455  if (!$mm && !$dd && !$yy)
456  {
457  return "";
458  }
459 
460  if (!$mm || !$dd || !$yy)
461  {
462  return "Incomplete date of birth - $mm/$dd/$yy";
463  }
464 
465  $dob = new DateTime("$mm/$dd/$yy");
466  $today = new DateTime();
467 
468  $age = $dob->diff($today);
469  if ($age->y > $this->max)
470  {
471  return $this->message;
472  }
473 
474  return "";
475  }
476 }
477 
496 {
497  function DateRangeValidator($field, $title, $min, $max, $message = null)
498  {
499  $this->min = $min;
500  $this->max = $max;
501 
502  $fields = array();
503  if($this->min && preg_match("|^(\\d{4})-(\\d\\d)-(\\d\\d)$|", $this->min, $fields))
504  {
505  $this->min = $fields[2]."/".$fields[3]."/".$fields[1];
506  }
507 
508  if($this->max && preg_match("|^(\\d{4})-(\\d\\d)-(\\d\\d)$|", $this->max, $fields))
509  {
510  $this->max = $fields[2]."/".$fields[3]."/".$fields[1];
511  }
512 
513  $this->message = $message;
514 
515  trace("DateRangeValidator field $field min {$this->min} max {$this->max}", 3);
516  if (!$message)
517  {
518  $labelMin = ($this->min && preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $this->min)) ?
519  $this->min : prettify($this->min);
520 
521  $labelMax = ($this->max && preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $this->max)) ?
522  $this->max : prettify($this->max);
523 
524  if($this->min && $this->max)
525  {
526  $this->message = "$title must be in the range {$labelMin} to {$labelMax}.";
527  }
528  else if($this->min)
529  {
530  $this->message = "$title must be greater than or equal to the {$labelMin}.";
531  }
532  else if($this->max)
533  {
534  $this->message = "$title must be less than or equal to the {$labelMax}.";
535  }
536  }
537 
539  }
540 
546  function writeClient()
547  {
548  if ($this->readOnly) return "";
549 
550  $min = "'{$this->min}'";
551  $max = "'{$this->max}'";
552 
553  $script .= <<<ENDSCRIPT
554 
555  var min = $min;
556  var max = $max;
557  var minDate = "";
558  var maxDate = "";
559  var fieldDate = "";
560 
561 
562  if(form["{$this->field}"].value != "" && form["{$this->field}"].value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))
563  {
564  form["{$this->field}"].value = Date.parse(form["{$this->field}"].value).format('%x');
565  fieldDate = new Date(form["{$this->field}"].value);
566  }
567 
568  if(max && max.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))
569  {
570  maxDate = new Date(max);
571  }
572  else if(max && form["{$this->max}"].value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))
573  {
574  form["{$this->max}"].value = Date.parse(form["{$this->max}"].value).format('%x');
575  maxDate = new Date(form["{$this->max}"].value);
576  }
577 
578  if(min && min.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))
579  {
580  minDate = new Date(min);
581  }
582  else if(min && form["{$this->min}"].value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))
583  {
584  form["{$this->min}"].value = Date.parse(form["{$this->min}"].value).format('%x');
585  minDate = new Date(form["{$this->min}"].value);
586  }
587 
588  if(fieldDate &&
589  (minDate && fieldDate.valueOf() < minDate.valueOf()) ||
590  (maxDate && fieldDate.valueOf() > maxDate.valueOf()) )
591  {
592  alert("{$this->message}");
593  return false;
594  }
595 
596 ENDSCRIPT;
597 
598  return $script;
599  }
600 
601  function validate()
602  {
603  global $_POST;
604 
605  if ($this->readOnly) return "";
606 
607  $min = $this->min;
608  $max = $this->max;
609  $minDate = "";
610  $maxDate = "";
611 
612  if($min && !preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $min))
613  {
614  $min = $_POST[$this->min];
615  }
616 
617  if(!preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $max))
618  {
619  $max = $_POST[$this->max];
620  }
621 
622  if($min && preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $min))
623  {
624  $minDate = new DateTime($min."00:00:00");
625  }
626 
627  if($max && preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $max))
628  {
629  $maxDate = new DateTime($max."00:00:00");
630  }
631 
632  if($_POST[$this->field] != "" AND preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $_POST[$this->field]))
633  {
634  $fieldDate = new DateTime($_POST[$this->field]);
635  }
636 
637  if ($_POST[$this->field] != "" AND (!preg_match("|^\\d{1,2}\/\\d{1,2}\/\\d{4}$|", $_POST[$this->field]) ||
638  (($minDate && $fieldDate && $fieldDate < $minDate) || ($maxDate && $fieldDate && $fieldDate > $maxDate))))
639  {
640  $result = $this->message;
641  }
642 
643  return $result;
644  }
645 }
646 
647 
658 {
659  var $message;
660 
662  {
663  $this->AbstractValidator($field, $title);
664  }
665 
666  function writeClient()
667  {
668  if ($this->readOnly) return "";
669 
670 $script = <<<ENDSCRIPT
671  var hourObj = form["{$this->field}_hh"];
672  var minObj = form["{$this->field}_mm"];
673 
674  if (hourObj.value != "" && (hourObj.value < 1 || hourObj.value > 12))
675  {
676  alert("{$this->title} Hours must be in the range 1 to 12");
677  return false;
678  }
679 
680  if (minObj.value != "" && (minObj.value < 0 || minObj.value > 59))
681  {
682  alert("{$this->title} Minutes must be in the range 0 to 59");
683  return false;
684  }
685 
686 
687 ENDSCRIPT;
688 
689  return $script;
690  }
691 }
692 
693 
702 {
703  var $empty = "";
704 
706  {
707  $this->empty = $empty;
709  }
710 
711  function writeClient()
712  {
713  if ($this->readOnly) return "";
714 
715  $script = <<<ENDSCRIPT
716  if ((
717  form["{$this->field}_hh"] != "" && form["{$this->field}_hh"].style.display != "none" &&
718  form["{$this->field}_hh"].value == "{$this->empty}") ||
719  (form["{$this->field}_mm"] != "" && form["{$this->field}_mm"].style.display != "none" &&
720  form["{$this->field}_mm"].value == "{$this->empty}")
721  )
722  {
723  alert("{$this->title} is a required field. Please supply a value.");
724  return false;
725  }
726 
727 
728 ENDSCRIPT;
729 
730  return $script;
731  }
732 }
733 
742 {
743  var $item;
744  var $message;
745 
746  function UniqueValidator($field, $title, $item, $message, $extraConstraint = "")
747  {
748  $this->class = $item->table;
749 
750  if(!$message)
751  {
752  $className = $item->prettifyClassName();
753  $message = (preg_match("/^[aeiou]/i", $className)) ? "An " : "A ";
754  $fieldName = strtolower($title);
755  $message .= "{$className} already exists with that {$fieldName}. Please choose a different {$fieldName}.";
756  }
757  $this->message = $message;
758  $this->item =& $item;
759  $this->extraConstraint = $extraConstraint;
760 
762  }
763 
764  function writeClient()
765  {
766  // No client-side component
767 
768  return "";
769  }
770 
771  function validate()
772  {
773  global $_POST;
774 
775  $value = $_POST[$this->field];
776 
777  $result = "";
778 
779  if ($value)
780  {
781  trace("UniqueValidator::validate: ".get_class($this->item), 3);
782 
783  $value = ConnectionManager::quote($value);
784 
785  $pk = $this->item->primary_key;
786 
787  $constraint = "WHERE {$this->field}=$value";
788 
789  if ($this->extraConstraint) $constraint .= " AND {$this->extraConstraint}";
790 
791  if ($this->item->$pk)
792  {
793  $constraint .= " AND {$pk}!='{$this->item->$pk}'";
794  }
795 
796  $matches = $this->item->queryValue("COUNT(1)", $constraint);
797  trace("***************count matches ". count($matches), 3);
798  if ($matches > 0)
799  {
800  trace("UniqueValidator::validate - found $matches match(es), raising error", 3);
801 
802  $result = $this->message;
803  }
804  }
805 
806  return $result;
807  }
808 }
809 
814 {
815  var $confirm;
817  var $caseSensitive = true;
818 
820  {
821  $this->confirm = $confirm;
822  $this->confirmTitle = $confirmTitle;
823  $this->caseSensitive = $caseSensitive;
824 
826  }
827 
828  function writeClient()
829  {
830  trace("MatchValidator::writeClient", 3);
831  if ($this->readOnly) return "";
832 
833  if ($this->caseSensitive)
834  {
835 
836  $script = <<<ENDSCRIPT
837 
838  if (form["{$this->field}"].value != form["{$this->confirm}"].value)
839  {
840  alert("{$this->title} and {$this->confirmTitle} do not match.");
841  return false;
842  }
843 ENDSCRIPT;
844  }
845  else
846  {
847  $script = <<<ENDSCRIPT
848 
849  if (form["{$this->field}"].value.toLowerCase() != form["{$this->confirm}"].value.toLowerCase())
850  {
851  alert("{$this->title} and {$this->confirmTitle} do not match.");
852  return false;
853  }
854 ENDSCRIPT;
855  }
856  return $script;
857  }
858 
859  function validate()
860  {
861  global $_POST;
862  $result = "";
863 
864  $value1 = $_POST[$this->field];
865  $value2 = $_POST[$this->confirm];
866 
867  if ($this->caseSensitive)
868  {
869  if ($value1 != $value2)
870  {
871  $result = "{$this->title} and {$this->confirmTitle} do not match";
872  }
873  }
874  else if (strtolower($value1) != strtolower($value2))
875  {
876  $result = "{$this->title} and {$this->confirmTitle} do not match";
877  }
878  return $result;
879  }
880 }
881 
882 /*
883  * PasswordValidator
884  *
885  * The password validator needs to work differently from the required
886  * validator because the user does not have to enter a password unless
887  * they open the reset block or if the block is opened for them for
888  * new account creation. This validator checks if the password block
889  * style is open by checking if style is not "none". If style is not
890  * none, then the password field cannot be empty.
891  */
893 {
894  var $empty = "";
895  var $form_id;
896 
898  {
899  $this->empty = $empty;
900  $this->form_id = $form_id;
902  }
903 
904  function writeClient()
905  {
906  if ($this->readOnly) return "";
907 
908  $pswField = "{$this->form_id}_{$this->field}_block";
909  $script = <<<ENDSCRIPT
910 
911  var block = document.getElementById("$pswField");
912 
913  if (block.style.display != "none" && form["{$this->field}"].value == "{$this->empty}")
914  {
915  alert("{$this->title} is a required field. Please supply a value.");
916  return false;
917  }
918 
919 ENDSCRIPT;
920 
921  return $script;
922  }
923 
924  function validate()
925  {
926  global $_POST;
927 
928  if ($this->readOnly) return "";
929 
930  $reset = $_POST["{$this->form_id}_reset_password"];
931 
932  if ($reset == 1 AND (!isset($_POST[$this->field]) || $_POST[$this->field] === $this->empty))
933  {
934  return "{$this->title} is a required field.";
935  }
936  else
937  {
938  return "";
939  }
940  }
941 }
942 
950 {
951  var $regexp;
952  var $message;
953 
955  {
956  $this->regexp = $regexp;
957  $this->message = $message;
958  $this->item =& $item;
959 
960  $this->AbstractValidator($field, $title);
961  }
962 
963  function writeClient()
964  {
965  trace("RegularExpressionValidator::writeClient", 3);
966 
967  if ($this->readOnly) return "";
968 
969  $script = <<<ENDSCRIPT
970 
971  if(form["{$this->field}"].value != "" &&
972  !form["{$this->field}"].value.match(/{$this->regexp}/i))
973  {
974  alert("{$this->message}");
975  return false;
976  }
977 ENDSCRIPT;
978 
979  return $script;
980  }
981 
982  function validate()
983  {
984  if ($this->readOnly) return "";
985 
986  //$exp = preg_replace("|([^/])|", "$1\\/", $this->regexp);
987  $exp = $this->regexp;
988  if ($_POST[$this->field] != "" && !preg_match("/{$exp}/i", $_POST[$this->field]))
989  {
990  $result = $this->message;
991  }
992 
993  return $result;
994  }
995 }
996 
1004 {
1005  var $min;
1006  var $max;
1008 
1010  {
1011  $this->min = $min;
1012  $this->max = $max;
1013  $this->message = $message;
1014 
1015  if (!$message)
1016  {
1017  $this->message = "$title must be in the range $min to $max";
1018  }
1019 
1020  $this->AbstractValidator($field, $title);
1021  }
1022 
1023  function writeClient()
1024  {
1025  trace("RangeValidator::writeClient", 3);
1026  if ($this->readOnly) return "";
1027 
1028  $min = is_numeric($this->min) ? $this->min : "'{$this->min}'";
1029  $max = is_numeric($this->max) ? $this->max : "'{$this->max}'";
1030 
1031  $script .= <<<ENDSCRIPT
1032 
1033  if (form["{$this->field}"].value == "" || form["{$this->field}"].value < $min || form["{$this->field}"].value > $max)
1034  {
1035  alert("{$this->message}");
1036  return false;
1037  }
1038 
1039 ENDSCRIPT;
1040  return $script;
1041  }
1042 
1043  function validate()
1044  {
1045  if ($this->readOnly) return "";
1046 
1047  $val = $_POST[$this->field];
1048  if ($val === "" || $val < $this->min || $val > $this->max)
1049  {
1050  $result = $this->message;
1051  }
1052 
1053  return $result;
1054  }
1055 }
1056 
1057 
1058 /*
1059  * SelectFieldRequiredValidator
1060  *
1061  * A custom required validator is needed so that When a calling scripts turns
1062  * add entry text on for the instance of the SelectFieldRenderer class,
1063  * we will check if the user has either selected an option from the list or
1064  * entered text because the standard required validator will consider the field
1065  * not completed if the user adds entry text.
1066  *
1067  * Currently, for this to work, the script must NOT set the field as
1068  * required, because that would cause the ValidationEngine to include
1069  * the generic RequiredValidator, which would not see text in the add
1070  * entry field as meeting the required condition. The "*" can be added
1071  * to the field label using the alias function. Add this validator the
1072  * the script using:
1073  * $form->validator->add(new SelectFieldRequiredValidator($field, $label, $empty));
1074  *
1075  */
1077 {
1078  var $empty = "";
1079 
1081  {
1082  $this->empty = $empty;
1083  $this->AbstractValidator($field, $title);
1084  }
1085 
1086  function writeClient()
1087  {
1088  if ($this->readOnly) return "";
1089 
1090  $script = <<<ENDSCRIPT
1091 
1092  if (form["{$this->field}"].style.display != "none" && form["{$this->field}"].value == "{$this->empty}"
1093  && form["{$this->field}_addEntry"].value == "{$this->empty}")
1094  {
1095  alert("{$this->title} is a required field. Please supply a value.");
1096  return false;
1097  }
1098 
1099 ENDSCRIPT;
1100 
1101  return $script;
1102 
1103  }
1104 
1105  function validate()
1106  {
1107  global $_POST;
1108 
1109  if ($this->readOnly) return "";
1110 
1111  if (!isset($_POST[$this->field]) && !isset($_POST["{$this->field}_addEntry"]))
1112  {
1113  return "{$this->title} is a required field.";
1114  }
1115  else
1116  {
1117  return "";
1118  }
1119  }
1120 
1121 }
1122 
1124 {
1125  var $subtitle = "";
1126 
1128  {
1129  $this->AbstractValidator($field, $title, $subtitle);
1130 
1131  $this->subtitle = $subtitle ? $subtitle : $this->title;
1132  }
1133 
1134  function writeClient()
1135  {
1136  if ($this->readOnly) return "";
1137 
1138  $script = <<<ENDSCRIPT
1139 
1140  if (form["{$this->field}"].style.display != "none")
1141  {
1142  if (form["{$this->field}"].value == "")
1143  {
1144  alert("{$this->title} is a required field. Please supply a value.");
1145  return false;
1146  }
1147  else if (!form["{$this->field}"].manager.validateRequired())
1148  {
1149  alert("{$this->subtitle} is a required field. Please supply a value.");
1150  return false;
1151  }
1152  }
1153 
1154 ENDSCRIPT;
1155 
1156  return $script;
1157 
1158  }
1159 
1160  function validate()
1161  {
1162  global $_POST;
1163 
1164  if ($this->readOnly) return "";
1165 
1166  if (!isset($_POST[$this->field]))
1167  {
1168  return "{$this->title} is a required field.";
1169  }
1170  else
1171  {
1172  return "";
1173  }
1174  }
1175 
1176 }
1188 {
1189  var $min = 0;
1190 
1192  {
1193  $this->min = $min;
1194  $this->AbstractValidator($field, $title);
1195  }
1196 
1197  function writeClient()
1198  {
1199  $script = <<<ENDSCRIPT
1200 
1201  var ctrl = form['{$this->field}'];
1202 
1203  if (ctrl.value.length < {$this->min})
1204  {
1205  alert("Please enter at least $this->min characters for {$this->title}.");
1206  return false;
1207  }
1208 ENDSCRIPT;
1209 
1210  return $script;
1211  }
1212 
1213  function validate()
1214  {
1215  global $_POST;
1216 
1217  if ($this->readOnly) return "";
1218 
1219  if (!isset($_POST[$this->field]))
1220  {
1221  return "Please enter at least {$this->min} for {$this->title}.";
1222  }
1223  else
1224  {
1225  return "";
1226  }
1227  }
1228 }
1229 
1230 
1231 
1232 
1245 {
1246  var $msg;
1247 
1249  {
1250  $this->AbstractValidator($field, $title);
1251  $this->msg = ($msg) ? $msg : $title . " is a required field.";
1252  }
1253 
1254  function writeClient()
1255  {
1256 $script = <<<ENDSCRIPT
1257 
1258  var boolObj = form["{$this->field}"];
1259 
1260  if (boolObj.checked == false)
1261  {
1262  alert("{$this->msg}");
1263  return false;
1264  }
1265 
1266 
1267 ENDSCRIPT;
1268 
1269  return $script;
1270  }
1271 
1272  // no need to overwrite server side validation or parent class
1273 }
1274 
1275 
1276 
1286 {
1287  var $min = "";
1288  var $empty = "";
1289  var $addEntry = false;
1290  var $msgText = "checkbox";
1291 
1293  {
1294  $this->min = $min;
1295  $this->AbstractValidator($field, $title);
1296  $this->addEntry = $addEntry;
1297  if($min > 1)
1298  $this->msgText = pluralize($this->msgText);
1299  }
1300 
1301  function writeClient()
1302  {
1303 $script = <<<ENDSCRIPT
1304 
1305  var i = 1;
1306  var countChecked = 0;
1307  var max = form["count_{$this->field}"].value;
1308  var fieldName;
1309 
1310  var pattern = new RegExp("^{$this->field}\\\\[.*?\\\\]$");
1311 
1312  for(i=0; i < form.elements.length; i++)
1313  {
1314  ctrl = form.elements[i];
1315  if (pattern.test(ctrl.name))
1316  {
1317  if (ctrl.checked == true)
1318  {
1319  countChecked++;
1320  }
1321  }
1322  }
1323 
1324  var addEntry = form["{$this->field}_addEntry"];
1325 
1326  if(addEntry && addEntry.value != "")
1327  {
1328  countChecked += 1;
1329  }
1330 
1331  if ({$this->min} > 0 && countChecked < {$this->min})
1332  {
1333  alert("You must check at least {$this->min} {$this->msgText} for {$this->title}.");
1334  return false;
1335  }
1336 
1337 ENDSCRIPT;
1338 
1339  return $script;
1340  }
1341 
1342 
1343  function validate()
1344  {
1345  global $_POST;
1346 
1347  if ($this->readOnly) return "";
1348 
1349  $values = $_POST[$this->field];
1350 
1351  // convert array of checks to comma delimated string and
1352  // back again to eliminate blanks
1353  if (is_array($values))
1354  {
1355  $valueAnswer = implode(",", array_values($values));
1356  $count = count(explode(",", $valueAnswer));
1357  }
1358  elseif($values)
1359  $count = 1;
1360  else
1361  $count = 0;
1362 
1363  if ($this->addEntry AND isset($_POST["{$this->field}_addEntry"]))
1364  $count += 1;
1365 
1366  if ($count < $this->min)
1367  {
1368  return "You must check at least {$this->min} {$this->msgText} for {$this->title}.";
1369  }
1370  else
1371  {
1372  return "";
1373  }
1374  }
1375 }
1376 
1377 
1387 {
1388  var $min = "";
1389  var $empty = "";
1390  var $addEntry = false;
1391  var $msgText = "checkbox";
1392 
1394  {
1395  $this->min = $min;
1396  $this->AbstractValidator($field, $title);
1397  $this->addEntry = $addEntry;
1398  if($min > 1)
1399  $this->msgText = pluralize($msgText);
1400  }
1401 
1402  function writeClient()
1403  {
1404  if($this->min == 0) return;
1405 
1406 $script = <<<ENDSCRIPT
1407 
1408  var i = 1;
1409  var countChecked = 0;
1410  var max = form["count_{$this->field}"].value;
1411 
1412  var pattern = new RegExp("^{$this->field}\\\\[.*?\\\\]$");
1413 
1414  for(i=0; i < form.elements.length; i++)
1415  {
1416  ctrl = form.elements[i];
1417  if (pattern.test(ctrl.name))
1418  {
1419  if (ctrl.checked == true)
1420  {
1421  countChecked++;
1422  }
1423  }
1424  }
1425 
1426  if (countChecked < {$this->min})
1427  {
1428  alert("You must check at least {$this->min} {$this->msgText} for {$this->title}.");
1429  return false;
1430  }
1431 ENDSCRIPT;
1432 
1433  return $script;
1434  }
1435 
1436 
1437  function validate()
1438  {
1439  global $_POST;
1440 
1441  if ($this->readOnly) return "";
1442 
1443  if (isset($_POST[$this->field]))
1444  {
1445  $count = count($_POST[$this->field]);
1446  }
1447 
1448  if ($count < $this->min)
1449  {
1450  return "You must check at least {$this->min} {$this->msgText} for {$this->title}.";
1451  }
1452  else
1453  {
1454  return "";
1455  }
1456  }
1457 }
1458 
1459 
1466 {
1468  {
1469  $this->empty = 0;
1470  $this->AbstractValidator($field, $title);
1471  }
1472 
1473  function writeClient()
1474  {
1475  if ($this->readOnly) return "";
1476 
1477  $script = <<<ENDSCRIPT
1478 
1479  var num = parseFloat(form["{$this->field}"].value);
1480 
1481  if (isNaN(num))
1482  {
1483  alert("{$this->title} is a required field. Please supply a value.");
1484  return false;
1485  }
1486 
1487 ENDSCRIPT;
1488 
1489  return $script;
1490  }
1491 
1492  function validate()
1493  {
1494  global $_POST;
1495 
1496  if ($this->readOnly) return "";
1497 
1498  if (!isset($_POST[$this->field]) || trim($_POST[$this->field]) === "")
1499  {
1500  return "{$this->title} is a required field.";
1501  }
1502  else
1503  {
1504  return "";
1505  }
1506  }
1507 
1508 }
1509 
1511 {
1513  {
1514  $this->AbstractValidator($field, $title);
1515  }
1516 
1517  function writeClient()
1518  {
1519 $script = <<<ENDSCRIPT
1520 
1521  if (form["{$this->field}"].value &&
1522  (!luhnCheck(form["{$this->field}"].value) ||
1523  form["{$this->field}"].value.length < 13 ||
1524  form["{$this->field}"].value.length > 19))
1525  {
1526  alert("{$this->title} is not valid.");
1527  return false;
1528  }
1529 
1530 ENDSCRIPT;
1531 
1532  return $script;
1533  }
1534 
1535  function validate()
1536  {
1537  global $_POST;
1538 
1539  if ($this->readOnly) return "";
1540 
1541  $val = $_POST[$this->field];
1542  $len = strlen($val);
1543 
1544  if (!luhnTest($len) || $len < 13 || $len > 19)
1545  {
1546  return "{$this->title} is not valid.";
1547  }
1548  }
1549 }
1550 
1558 {
1559  var $validators = array();
1560  var $msg = "";
1561  var $id = "";
1562  var $generateScript = true;
1563 
1567  function ValidationEngine()
1568  {
1569  $this->validators = func_get_args();
1570  }
1571 
1575  function writeScript()
1576  {
1577  $suffix = ($this->id) ? "_{$this->id}" : "";
1578 
1579  $script = <<<ENDSCRIPT
1580 
1581 function validate{$suffix}(form)
1582 {
1583 ENDSCRIPT;
1584 
1585  if ($this->generateScript)
1586  {
1587  foreach($this->validators as $v)
1588  {
1589  $script .= $v->writeClient();
1590  }
1591  }
1592 
1593  $script .= <<<ENDSCRIPT
1594 
1595  return true;
1596 }
1597 
1598 ENDSCRIPT;
1599 
1600  return $script;
1601  }
1602 
1606  function add()
1607  {
1608  foreach(func_get_args() as $v)
1609  {
1610  $this->validators[] = $v;
1611  }
1612  }
1613 
1619  function getValidator($field, $class)
1620  {
1621  foreach($this->validators as $v)
1622  {
1623  if ($v->field == $field && get_class($v) == $class)
1624  {
1625  return $v;
1626  }
1627  }
1628 
1629  return null;
1630  }
1631 
1636  function getValidators($field)
1637  {
1638  $vals = array();
1639  foreach($this->validators as $v)
1640  {
1641  if ($v->field == $field)
1642  {
1643  $vals[] = $v;
1644  }
1645  }
1646 
1647  return $vals;
1648  }
1649 
1655  function validate()
1656  {
1657  $result = "";
1658 
1659  foreach($this->validators as $v)
1660  {
1661  $msg = $v->validate();
1662  if ($msg)
1663  {
1664  $result .= $msg."<br>";
1665  }
1666  }
1667 
1668  $this->msg = $msg;
1669 
1670  return $result;
1671  }
1672 
1678  function isRequired($field)
1679  {
1680  foreach($this->validators as $v)
1681  {
1682  if ($v->field == $field && get_class($v) == RequiredValidator) return true;
1683  }
1684 
1685  return false;
1686  }
1687 
1693  {
1694  foreach($this->validators as $v)
1695  {
1696  if (get_class($v) == RequiredValidator) return true;
1697  }
1698 
1699  return false;
1700  }
1701 }
1702 
1703 
1704 
1726 abstract class ValidateTabSet
1727 {
1728  var $msg;
1729 
1730  function ValidateTabSet()
1731  {
1732  }
1733 
1738  function Validate()
1739  {
1740  }
1741 
1742  /*
1743  * Additional functions as required to validate each page
1744  * in the tab set. For example:
1745  *
1746  * function validateGrant()
1747  *
1748  * function validateProgram()
1749  *
1750  * function validateOrder()
1751  *
1752  * these functions set msg variable to the appropriate error/warning
1753  * message.
1754  */
1755 
1756 
1764  function warning()
1765  {
1766  if($this->msg)
1767  {
1768  echo "<div id='warning'>{$this->msg}</div>\n";
1769  }
1770  }
1771 
1776  function message()
1777  {
1778  if($this->msg)
1779  {
1780  echo "<p>{$this->msg}</p>\n";
1781  }
1782  }
1783 
1789  function writeScript()
1790  {
1791  $msg = stripHTML($this->msg);
1792  if($msg)
1793  {
1794 
1795 $script .= <<<ENDSCRIPT
1796 <script type="text/javascript">
1797 window.addEvent('domready', function()
1798 {
1799  alert('$msg');
1800 });
1801 </script>
1802 ENDSCRIPT;
1803  }
1804  return $script;
1805  }
1806 }
1807 
1808 
1809 
1816 {
1817  var $msg;
1818  var $empty = "";
1819  var $test = "password";
1820 
1827  function SignatureValidator($field, $title, $msg, $test = "password")
1828  {
1829  $this->AbstractValidator($field, $title);
1830  $this->msg = $msg;
1831  $this->test = $test;
1832  }
1833 
1834  function writeScript()
1835  {
1836  // no client side validation
1837  }
1838 
1839  function validate()
1840  {
1841  global $_POST;
1842  global $user;
1843 
1844  $value = $_POST[$this->field];
1845 
1846  if (isset($value) && $value !== $this->empty)
1847  {
1848  if ($this->test == "password")
1849  {
1850  $hash = $user->hashPassword($value, $user->password);
1851 
1852  if($user->password == $hash)
1853  return "";
1854  else
1855  {
1856  trace("Signature Validator::invalid signature", 3);
1857  return $this->msg;
1858  }
1859  }
1860  else
1861  {
1862  if ($user->get($this->test) == $value) return "";
1863  return $this->msg;
1864  }
1865  }
1866  }
1867 }
1868 ?>
Base class for Validators.
Definition: validation.inc:49
AbstractValidator($field, $title)
Definition: validation.inc:53
static quote($str)
Quote a string value based on the character set of the global connection.
CreditCardNumberValidator($field, $title)
DateRangeValidator($field, $title, $min, $max, $message=null)
Definition: validation.inc:497
writeClient()
If min or max set and it is a date string then validate as a date, if set and it is not a date string...
Definition: validation.inc:546
Date Validator.
Definition: validation.inc:338
DateValidator($field, $title)
Definition: validation.inc:339
Tests whether two fields contain the same value.
Definition: validation.inc:814
MatchValidator($field, $title, $confirm, $confirmTitle, $caseSensitive=true)
Definition: validation.inc:819
MaximumAgeValidator can be used with DateOfBirth fields.
Definition: validation.inc:437
MaximumAgeValidator($field, $title, $max, $message=null)
Definition: validation.inc:441
AgeRangeValidator can be used with DateOfBirth fields.
Definition: validation.inc:390
MinimumAgeValidator($field, $title, $min, $message=null)
Definition: validation.inc:394
PasswordValidator($field, $title, $form_id="", $empty="")
Definition: validation.inc:897
RangeValidator class.
RangeValidator($field, $title, $min, $max, $message=null)
Tests wheteher a field's value matches the supplied regular expression.
Definition: validation.inc:950
RegularExpressionValidator($field, $title, $regexp, $message)
Definition: validation.inc:954
RequiredBoolean Validator.
RequiredBooleanValidator($field, $title, $msg="")
RequiredCheckList Validator.
RequiredCheckListValidator($field, $title, $min=1, $addEntry=true)
RequiredCrossReferenceSelectValidator.
RequiredCrossReferenceSelectValidator($field, $title, $min=1)
RequiredCurrencyField Validator.
RequiredCurrencyValidator($field, $title)
RequiredFileValidator($field, $title, $message="")
Definition: validation.inc:237
RequiredIfCheckedValidator($field, $checkbox, $title, $checkboxTitle, $message="")
Definition: validation.inc:283
RequiredRadioButtonValidator($field, $title, $empty="")
Definition: validation.inc:195
RequiredStringValidator.
RequiredStringValidator($field, $title, $min=0)
RequiredTimeValidator.
Definition: validation.inc:702
RequiredTimeValidator($field, $title, $empty="")
Definition: validation.inc:705
Validates a field rendered with TreeSelectFieldRenderer.
Definition: validation.inc:132
RequiredTreeValidator($field, $title, $empty="")
Definition: validation.inc:135
RequiredField Validator.
Definition: validation.inc:76
RequiredValidator($field, $title, $empty="", $checkDisplayNone=true)
Definition: validation.inc:80
SelectFieldRequiredValidator($field, $title, $empty="")
Validates that the user's supplied password is correct.
SignatureValidator($field, $title, $msg, $test="password")
Create a new SignatureValidator.
$test
Field to match.
$empty
Empty value for the control.
$msg
The error message to report if there is a mismatch.
__construct($field, $title, $subtitle)
TimeValidator.
Definition: validation.inc:658
TimeValidator($field, $title)
Definition: validation.inc:661
Uniqueness Validator.
Definition: validation.inc:742
UniqueValidator($field, $title, $item, $message, $extraConstraint="")
Definition: validation.inc:746
When the data entry interface contains multiple tabs (pages) that the user can move around freely (e....
message()
Show the validation issue/error as an informative message instead of a warning.
warning()
Called from summary view class for each section if the section is invalid.
writeScript()
When system returns user to a tab b/c not valid or completed, show a popup message for the error.
Validate()
Called when user clicks submit: validate each tab page and redirect to first page that is invalid or ...
The ValidationEngine takes an array of validator objects in its constructor, and is then able to gene...
getValidators($field)
Get the validators attached to a specific field.
validate()
Perform server-side validation of the posted form values.
$generateScript
Specifies whether to generate client-side validation script.
add()
Add a validator.
$id
The ID of this validator.
writeScript()
Output the client-side form validation function, based on all the validators that have been added.
isRequired($field)
Checks to see whether the specified field has a RequiredValidator attached to it.
$msg
Validation error messages accumulator;.
$validators
The array of validators making up this validation engine.
hasRequiredFields()
Checks to see whether any fields have been marked as required.
getValidator($field, $class)
Get the specified validator by field and class.
ValidationEngine()
Create a new ValidationEngine.
checkNumeric($p)
Security helper function.
Definition: functions.inc:630
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1428
stripHTML($text)
Definition: functions.inc:847
jsSafe($str, $escapeEntities=false)
Utility function to escape a string correctly for use in a Javascript client-side call.
Definition: functions.inc:434
luhnTest($num)
Performs a Luhn validity test for credit card or IMEI numbers.
Definition: functions.inc:1985
prettify($name)
Takes a variable or field name and converts it into a human-readable version (assuming that the origi...
Definition: functions.inc:1413