/** * 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; }
/** * testDateCustomRegx method * * @return void */ public function testDateCustomRegx() { $this->assertTrue(Validation::date('2006-12-27', null, '%^(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%')); $this->assertFalse(Validation::date('12-27-2006', null, '%^(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%')); }