Framework  3.9
DateRangeValidator Class Reference
+ Inheritance diagram for DateRangeValidator:
+ Collaboration diagram for DateRangeValidator:

Public Member Functions

 DateRangeValidator ($field, $title, $min, $max, $message=null)
 
 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, then get the value from the form's field. More...
 
 validate ()
 
- Public Member Functions inherited from AbstractValidator
 AbstractValidator ($field, $title)
 

Additional Inherited Members

- Public Attributes inherited from AbstractValidator
 $field
 
 $title
 

Detailed Description

Parameters
string$field
string$title
stringor Date $min
stringor Date $max
string$messageMin can be either a static date min that the field's value must be set to or the name of a field whose value will be compared to the given field. Date strings can be in either format '2000-01-01' or '01/01/2000'

e.g. static date param: today() or '2000-01-01' field date: "end_date" where end_date is the tag for a date input field displayed on the form

Definition at line 495 of file validation.inc.

Member Function Documentation

◆ DateRangeValidator()

DateRangeValidator::DateRangeValidator (   $field,
  $title,
  $min,
  $max,
  $message = null 
)

Definition at line 497 of file validation.inc.

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  }
AbstractValidator($field, $title)
Definition: validation.inc:53
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
prettify($name)
Takes a variable or field name and converts it into a human-readable version (assuming that the origi...
Definition: functions.inc:1413

◆ validate()

DateRangeValidator::validate ( )

Reimplemented from AbstractValidator.

Definition at line 601 of file validation.inc.

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  }

◆ writeClient()

DateRangeValidator::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, then get the value from the form's field.

Reimplemented from AbstractValidator.

Definition at line 546 of file validation.inc.

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  }

The documentation for this class was generated from the following file: