/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof DateTime) { throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\DateTime'); } if (null === $value || '' === $value || $value instanceof \DateTime) { return; } if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedTypeException($value, 'string'); } $value = (string) $value; if (!preg_match(static::PATTERN, $value, $matches)) { if ($this->context instanceof ExecutionContextInterface) { $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_FORMAT_ERROR)->addViolation(); } else { $this->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_FORMAT_ERROR)->addViolation(); } return; } if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) { if ($this->context instanceof ExecutionContextInterface) { $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_DATE_ERROR)->addViolation(); } else { $this->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_DATE_ERROR)->addViolation(); } } if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) { if ($this->context instanceof ExecutionContextInterface) { $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_TIME_ERROR)->addViolation(); } else { $this->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_TIME_ERROR)->addViolation(); } } }
/** * {@inheritDoc} */ public function validate($value, Constraint $constraint) { parent::validate($value, $constraint); if ($value instanceof \DateTime) { $now = new \DateTime('today'); if ($value < $now) { $this->context->addViolation($constraint->message, array('%date%' => $value)); } } if (is_string($value) && date('Y-m-d', strtotime($value)) < date('Y-m-d', time())) { $this->context->addViolation($constraint->message, array('%date%' => $value)); } }
/** * Checks if the passed value is a valid date and if the (i.e. in the past) * * @param mixed $value The value that should be validated * @param Constraint $constraint The constraint for the validation * * @return Boolean Whether or not the value is valid * * @api */ public function isValid($value, Constraint $constraint) { if (parent::isValid($value, $constraint)) { if ($value instanceof \DateTime) { $tm = $value->getTimestamp(); } else { $tm = mktime((string) $value); } if (mktime($constraint->dateMax) > $tm) { return true; } $this->setMessage($constraint->message, array('{{ value }}' => $value, '{{ dateMax }}' => $constraint->dateMax)); } return false; }
public function isValid($value, Constraint $constraint) { if (parent::isValid($value, $constraint)) { if ($value instanceof \DateTime) { $tm = $value->getTimestamp(); } else { preg_match(self::PATTERN_BIRTHDAY, string($value), $matches); $tm = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); } if (time() > $tm) { return true; } $this->setMessage($constraint->messageBirthday, array('{{ value }}' => $value)); } return false; }