* @package tao */ class tao_helpers_form_validators_DateTime extends tao_helpers_form_Validator { /** * (non-PHPdoc) * @see tao_helpers_form_Validator::evaluate() */ public function evaluate($values) { $returnValue = (bool) false; $value = trim($values); try { $dateTime = new DateTime($value); // if no date given no need to go further. To check if not empty, use the NotEmpty validator if (!empty($value) && !empty($this->getOption('comparator')) && $this->getOption('datetime2_ref') instanceof tao_helpers_form_FormElement) { //try comparison: try { $dateTime2 = new DateTime($this->getOption('datetime2_ref')->getRawValue()); } catch (Exception $e) { } if ($dateTime2 instanceof DateTimeInterface) { switch ($this->getOption('comparator')) { case 'after': case 'later': case 'sup': case '>':{ if ($dateTime > $dateTime2) { $returnValue = true; } else { $this->setMessage(__('Invalid date range (must be after: %s)', $dateTime2->format('Y-m-d'))); } break; } case '>=':{ if ($dateTime >= $dateTime2) { $returnValue = true; } else { $this->setMessage(__('Invalid date range (must be after or the same as: %s)', $dateTime2->format('Y-m-d'))); } break; } case 'before': case 'earlier': case 'inf': case '<':{ if ($dateTime < $dateTime2) { $returnValue = true; } else { $this->setMessage(__('Invalid date range (must be before: %s)', $dateTime2->format('Y-m-d'))); } break; } case '<=':{ if ($dateTime <= $dateTime2) { $returnValue = true; } else { $this->setMessage(__('Invalid date range (must be before or the same as: %s)', $dateTime2->format('Y-m-d'))); } break; } default: throw new common_Exception('Usuported comparator in DateTime Validator: ' . $this->getOption('comparator')); } } } else { $returnValue = true; } } catch (Exception $e) { $this->setMessage(__('The value of this field must be a valide date format, e.g. YYYY-MM-DD')); $returnValue = false; } return (bool) $returnValue; } }