Tools\Model\Table\Table::validateDate PHP Method

validateDate() public method

Validation of Date fields (as the core one is buggy!!!)
public validateDate ( mixed $value, array $options = [], array $context = [] ) : boolean
$value mixed
$options array - dateFormat (defaults to 'ymd') - allowEmpty - after/before (fieldName to validate against) - min (defaults to 0 - equal is OK too)
$context array
return boolean 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);
                /* @var \Cake\I18n\Time $after */
                $after = $context['data'][$options['after']];
                if ($after->gt($compare)) {
                    return false;
                }
            }
            if (!empty($options['before']) && isset($context['data'][$options['before']])) {
                $compare = $value->addDays($days);
                /* @var \Cake\I18n\Time $before */
                $before = $context['data'][$options['before']];
                if ($before->lt($compare)) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }