From 3b792fd533f1ddd5f6603e8c1eefda9ab5d5b782 Mon Sep 17 00:00:00 2001 From: Guido Date: Tue, 11 Jul 2017 23:37:54 +0200 Subject: [PATCH 1/4] Updated the DateTime format to use the format field instead of the static value --- src/Element/DateTime.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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; } From b7f9cd8c5cbc60256e3f48e9ce5d9e25243b8688 Mon Sep 17 00:00:00 2001 From: Guido Date: Tue, 11 Jul 2017 23:39:57 +0200 Subject: [PATCH 2/4] Fixed broken tests. Updated the formats within the tests. --- test/Element/DateTest.php | 4 ++-- test/Element/MonthTest.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) 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'); diff --git a/test/Element/MonthTest.php b/test/Element/MonthTest.php index 6f8d17ca..d91a014b 100644 --- a/test/Element/MonthTest.php +++ b/test/Element/MonthTest.php @@ -17,6 +17,7 @@ class MonthTest extends TestCase public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes() { $element = new MonthElement('foo'); + $element->setFormat('Y-m'); $element->setAttributes([ 'inclusive' => true, 'min' => '2000-01', From 9eaecdcf387aa1ec12911f90e091366279c89287 Mon Sep 17 00:00:00 2001 From: Guido Buiting Date: Wed, 12 Jul 2017 09:23:19 +0200 Subject: [PATCH 3/4] Added the format field to the Element\Month. Updated the test to no longer set the format for the Element\Month. --- src/Element/Month.php | 7 +++++++ test/Element/MonthTest.php | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) 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/MonthTest.php b/test/Element/MonthTest.php index d91a014b..6f8d17ca 100644 --- a/test/Element/MonthTest.php +++ b/test/Element/MonthTest.php @@ -17,7 +17,6 @@ class MonthTest extends TestCase public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes() { $element = new MonthElement('foo'); - $element->setFormat('Y-m'); $element->setAttributes([ 'inclusive' => true, 'min' => '2000-01', From 78fed36fc9bfbc69cb354d2c79224757c5ff12d7 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 6 Dec 2017 14:21:51 -0600 Subject: [PATCH 4/4] Adds CHANGELOG entry for #172 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) 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