diff --git a/CHANGELOG.md b/CHANGELOG.md index 927fa299..dfdbce3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,12 @@ All notable changes to this project will be documented in this file, in reverse binding values in a fieldset will correctly identify the elements in the provided data. +- [#172](https://github.com/zendframework/zend-form/pull/172) fixes the + `DateTime` element such that it no longer attempts to use its + `DATETIME_FORMAT` constant, but, rather, the value of the `$format` property, + when representing the element; this change allows developers to override the + format, which was the original intention. + ## 2.10.3 - TBD ### Added diff --git a/src/Element/DateTime.php b/src/Element/DateTime.php index 88d9135e..e3ac1e9d 100644 --- a/src/Element/DateTime.php +++ b/src/Element/DateTime.php @@ -135,7 +135,7 @@ protected function getValidators() throw new InvalidArgumentException(sprintf( '%1$s expects "min" to conform to %2$s; received "%3$s"', __METHOD__, - static::DATETIME_FORMAT, + $this->format, $this->attributes['min'] )); } @@ -153,7 +153,7 @@ protected function getValidators() throw new InvalidArgumentException(sprintf( '%1$s expects "max" to conform to %2$s; received "%3$s"', __METHOD__, - static::DATETIME_FORMAT, + $this->format, $this->attributes['max'] )); } @@ -226,7 +226,7 @@ public function getInputSpecification() private function valueIsValidDateTimeFormat($value) { return PhpDateTime::createFromFormat( - static::DATETIME_FORMAT, + $this->format, $value ) instanceof DateTimeInterface; } diff --git a/src/Element/Month.php b/src/Element/Month.php index a5e24d2b..104f56d4 100644 --- a/src/Element/Month.php +++ b/src/Element/Month.php @@ -18,6 +18,13 @@ class Month extends DateTime const DATETIME_FORMAT = 'Y-m'; + /** + * A valid format string accepted by date() + * + * @var string + */ + protected $format = self::DATETIME_FORMAT; + /** * Seed attributes * diff --git a/test/Element/DateTest.php b/test/Element/DateTest.php index 0c5d49e0..0ef6795b 100644 --- a/test/Element/DateTest.php +++ b/test/Element/DateTest.php @@ -125,8 +125,8 @@ public function testCorrectFormatPassedToDateValidator() { $element = new DateElement('foo'); $element->setAttributes([ - 'min' => '2012-01-01', - 'max' => '2012-12-31', + 'min' => '01-01-2012', + 'max' => '31-12-2012', ]); $element->setFormat('d-m-Y');