示例#1
0
 /**
  * Validation of Date fields (as the core one is buggy!!!)
  *
  * @param mixed $value
  * @param array $options
  * - dateFormat (defaults to 'ymd')
  * - allowEmpty
  * - after/before (fieldName to validate against)
  * - min (defaults to 0 - equal is OK too)
  * @param array $context
  * @return bool Success
  */
 public function validateDate($value, $options = [], array $context = [])
 {
     if (!$value) {
         if (!empty($options['allowEmpty'])) {
             return true;
         }
         return false;
     }
     $format = !empty($options['format']) ? $options['format'] : 'ymd';
     if (!is_object($value)) {
         $value = new Time($value);
     }
     $date = $value->format(FORMAT_DB_DATE);
     if (!empty($options['allowEmpty']) && (empty($date) || $date == DEFAULT_DATE)) {
         return true;
     }
     if (Validation::date($date, $format)) {
         // after/before?
         $days = !empty($options['min']) ? $options['min'] : 0;
         if (!empty($options['after']) && isset($context['data'][$options['after']])) {
             $compare = $value->subDays($days);
             if ($context['data'][$options['after']]->gt($compare)) {
                 return false;
             }
         }
         if (!empty($options['before']) && isset($context['data'][$options['before']])) {
             $compare = $value->addDays($days);
             if ($context['data'][$options['before']]->lt($compare)) {
                 return false;
             }
         }
         return true;
     }
     return false;
 }