/** * Validate the input value and set the value of @see validPattern if the input machtes a pattern * * @param string $value The format string to validate * @param null $context The form context (ignored) * * @return bool True when the input is valid, otherwise false * * @see Zend_Validate_Abstract::isValid() */ public function isValid($value, $context = null) { $this->validPattern = false; if (!is_string($value) && !is_int($value)) { $this->error('INVALID_TYPE'); return false; } if ($this->isUnixTimestamp($value)) { $dt = DateTimeFactory::create(); $dt->setTimestamp($value); } else { if (!isset($this->patterns)) { throw new ProgrammingError('There are no allowed timeformats configured'); } $match_found = false; foreach ($this->patterns as $pattern) { $dt = DateTimeFactory::parse($value, $pattern); if ($dt !== false && $dt->format($pattern) === $value) { $match_found = true; $this->validPattern = $pattern; break; } } if (!$match_found) { $this->_error('NO_MATCHING_PATTERN'); return false; } } return true; }
/** * Validate filtered date/time strings * * Expects one or more valid formats being set in $this->patterns. Sets element value as Unix timestamp * if the input is considered valid. Utilizes DateTimeFactory to ensure time zone awareness. * * @param string $value * @param mixed $context * @return bool */ public function isValid($value, $context = null) { // Overwrite the internal validator to use if (!parent::isValid($value, $context)) { return false; } $pattern = $this->dateValidator->getValidPattern(); if (!$pattern) { $this->setValue($value); return true; } $this->setValue(DateTimeFactory::parse($value, $pattern)->getTimestamp()); return true; }