Skip to content

2.2RC1 BC Break: DateTimeFormatter sets blank data to today's date #4393

Closed
akrabat opened this Issue May 2, 2013 · 13 comments

5 participants

@akrabat
Zend Framework member
akrabat commented May 2, 2013

Updating my project to 2.2 RC1, I find that any Zend\Form\Element\Date element can now no longer be empty as today's date is returned from the automatically added DateTimeFormatter filter...

@weierophinney
Zend Framework member

Are you working on a fix yet? (Don't want to duplicate effort!)

@akrabat
Zend Framework member
akrabat commented May 2, 2013

Not sure of how complex a fix is required. The most simplistic fix is:

diff --git a/library/Zend/Filter/DateTimeFormatter.php b/library/Zend/Filter/DateTimeFormatter.php
index f0dee58..19a9e6c 100644
--- a/library/Zend/Filter/DateTimeFormatter.php
+++ b/library/Zend/Filter/DateTimeFormatter.php
@@ -71,7 +71,9 @@ class DateTimeFormatter extends AbstractFilter
      */
     protected function normalizeDateTime($value)
     {
-        if (is_int($value)) {
+        if (empty($value)) {
+            return '';
+        } elseif (is_int($value)) {
             $dateTime = new DateTime('@' . $value);
         } elseif (!$value instanceof DateTime) {
             $dateTime = new DateTime($value);

But, I'm unsure whether we should introduce an option for this.

@weierophinney
Zend Framework member

I'd argue if it was the original behavior, we should probably add a test and make the change. Check through the changelog to see what may have prompted the change in behavior, though; if it was on purpose, we should add an option.

@akrabat
Zend Framework member
akrabat commented May 2, 2013

PR that introduced DateTimeFormatter is #3632 . Any thoughts on this @davidwindell ?

@davidwindell

@akrabat I clearly missed handling for empty values and agree we need to do so, I would be against returning '', instead simply return $value if it isempty. In an API scenario one may be handling null values, not just empty strings.

     protected function normalizeDateTime($value)
     {
-        if (is_int($value)) {
+        if (empty($value)) {
+            return $value;
+        } elseif (is_int($value)) {
             $dateTime = new DateTime('@' . $value);
         } elseif (!$value instanceof DateTime) {
             $dateTime = new DateTime($value);
@akrabat
Zend Framework member
akrabat commented May 2, 2013

Thanks @davidwindell - PR created.

@akrabat akrabat added a commit that closed this issue May 2, 2013
@akrabat akrabat Ensure that DateTimeFormatter doesn't format an empty string
This fixes #4393 and maintains BC.
812ea7e
@akrabat akrabat closed this in 812ea7e May 2, 2013
@weierophinney weierophinney added a commit that referenced this issue May 2, 2013
@weierophinney weierophinney Merge branch 'hotfix/4394'
Close #4394
Fixes #4393
dd12afa
@akrabat akrabat was assigned May 2, 2013
@DASPRiD
Zend Framework member
DASPRiD commented May 2, 2013

Wait wait, not empty(), do a === '' comparision!

@davidwindell

Why? I would never want an empty value normalised to today's date.

@gws
gws commented May 2, 2013

0 is a perfectly valid UNIX timestamp.

@davidwindell

Ooh yeah, @akrabat can you do a check for "" and null instead please?

@akrabat
Zend Framework member
akrabat commented May 3, 2013

Would people prefer if ($value === '' || $value === null) { ?

@davidwindell

: thumbsup

@gws
gws commented May 3, 2013

:+1:

@akrabat akrabat added a commit that referenced this issue May 3, 2013
@akrabat akrabat Allow DateTimeFormatter to format zero.
This addresses the note raised in issue #4393 after merge that zero is a
valid UNIX timestamp.
19817ef
@ghost Unknown pushed a commit that referenced this issue Jul 14, 2013
@akrabat akrabat Ensure that DateTimeFormatter doesn't format an empty string
This fixes #4393 and maintains BC.
fe0de31
@ghost Unknown pushed a commit that referenced this issue Jul 14, 2013
@weierophinney weierophinney Merge branch 'hotfix/4394'
Close #4394
Fixes #4393
17604ed
@ghost Unknown pushed a commit that referenced this issue Jul 14, 2013
@akrabat akrabat Allow DateTimeFormatter to format zero.
This addresses the note raised in issue #4393 after merge that zero is a
valid UNIX timestamp.
14bc502
@gianarb gianarb pushed a commit to zendframework/zend-filter that referenced this issue May 15, 2015
@akrabat akrabat Ensure that DateTimeFormatter doesn't format an empty string
This fixes zendframework/zf2#4393 and maintains BC.
8154c46
@weierophinney weierophinney added a commit to zendframework/zend-filter that referenced this issue May 15, 2015
@weierophinney weierophinney Merge branch 'hotfix/4394' c140dfc
@gianarb gianarb pushed a commit to zendframework/zend-filter that referenced this issue May 15, 2015
@akrabat akrabat Allow DateTimeFormatter to format zero.
This addresses the note raised in issue zendframework/zf2#4393 after merge that zero is a
valid UNIX timestamp.
18ef216
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.