diff --git a/library/Icinga/Web/Form/Element/DateTimePicker.php b/library/Icinga/Web/Form/Element/DateTimePicker.php index 1350e7f9a..529175af4 100644 --- a/library/Icinga/Web/Form/Element/DateTimePicker.php +++ b/library/Icinga/Web/Form/Element/DateTimePicker.php @@ -4,7 +4,6 @@ namespace Icinga\Web\Form\Element; use DateTime; -use Icinga\Web\Form; use Icinga\Web\Form\FormElement; use Icinga\Web\Form\Validator\DateTimeValidator; @@ -126,10 +125,17 @@ class DateTimePicker extends FormElement if (! parent::isValid($value, $context)) { return false; } + if (! $value instanceof DateTime) { $format = $this->local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339; - $this->setValue(DateTime::createFromFormat($format, $value)); + $dateTime = DateTime::createFromFormat($format, $value); + if ($dateTime === false) { + $dateTime = DateTime::createFromFormat(substr($format, 0, strrpos($format, ':')), $value); + } + + $this->setValue($dateTime); } + return true; } } diff --git a/library/Icinga/Web/Form/Validator/DateTimeValidator.php b/library/Icinga/Web/Form/Validator/DateTimeValidator.php index f1e22e250..576ed36f8 100644 --- a/library/Icinga/Web/Form/Validator/DateTimeValidator.php +++ b/library/Icinga/Web/Form/Validator/DateTimeValidator.php @@ -57,14 +57,22 @@ class DateTimeValidator extends Zend_Validate_Abstract $this->_error(self::INVALID_DATETIME_TYPE); return false; } - if (is_string($value)) { - $format = $this->local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339; + + if (! $value instanceof DateTime) { + $format = $baseFormat = $this->local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339; $dateTime = DateTime::createFromFormat($format, $value); + + if ($dateTime === false) { + $format = substr($format, 0, strrpos($format, ':')); + $dateTime = DateTime::createFromFormat($format, $value); + } + if ($dateTime === false || $dateTime->format($format) !== $value) { - $this->_error(self::INVALID_DATETIME_FORMAT, DateTimeFactory::create()->format($format)); + $this->_error(self::INVALID_DATETIME_FORMAT, DateTimeFactory::create()->format($baseFormat)); return false; } } + return true; } }