فهرست منبع

[Form] Fixed isXXXWithinRange() methods in TimeField and DateField to ignore empty dropdowns

Bernhard Schussek 14 سال پیش
والد
کامیت
df011ed1ef

+ 6 - 3
src/Symfony/Component/Form/DateField.php

@@ -262,7 +262,8 @@ class DateField extends HybridField
     {
         $date = $this->getNormalizedData();
 
-        return null === $date || in_array($date->format('Y'), $this->getOption('years'));
+        return $this->isEmpty() || ($this->isGroup() && $this->get('year')->isEmpty())
+                || in_array($date->format('Y'), $this->getOption('years'));
     }
 
     /**
@@ -277,7 +278,8 @@ class DateField extends HybridField
     {
         $date = $this->getNormalizedData();
 
-        return null === $date || in_array($date->format('m'), $this->getOption('months'));
+        return $this->isEmpty() || ($this->isGroup() && $this->get('month')->isEmpty())
+                || in_array($date->format('m'), $this->getOption('months'));
     }
 
     /**
@@ -292,7 +294,8 @@ class DateField extends HybridField
     {
         $date = $this->getNormalizedData();
 
-        return null === $date || in_array($date->format('d'), $this->getOption('days'));
+        return $this->isEmpty() || ($this->isGroup() && $this->get('day')->isEmpty())
+                || in_array($date->format('d'), $this->getOption('days'));
     }
 
     /**

+ 6 - 3
src/Symfony/Component/Form/TimeField.php

@@ -195,7 +195,8 @@ class TimeField extends Form
     {
         $date = $this->getNormalizedData();
 
-        return null === $date || in_array($date->format('H'), $this->getOption('hours'));
+        return $this->isEmpty() || $this->get('hour')->isEmpty()
+                || in_array($date->format('H'), $this->getOption('hours'));
     }
 
     /**
@@ -210,7 +211,8 @@ class TimeField extends Form
     {
         $date = $this->getNormalizedData();
 
-        return null === $date || in_array($date->format('i'), $this->getOption('minutes'));
+        return $this->isEmpty() || $this->get('minute')->isEmpty()
+                || in_array($date->format('i'), $this->getOption('minutes'));
     }
 
     /**
@@ -225,7 +227,8 @@ class TimeField extends Form
     {
         $date = $this->getNormalizedData();
 
-        return null === $date || in_array($date->format('s'), $this->getOption('seconds'));
+        return $this->isEmpty() || !$this->has('second') || $this->get('second')->isEmpty()
+                || in_array($date->format('s'), $this->getOption('seconds'));
     }
 
     /**

+ 48 - 0
tests/Symfony/Tests/Component/Form/DateFieldTest.php

@@ -149,6 +149,22 @@ class DateFieldTest extends DateTimeTestCase
         $this->assertTrue($field->isYearWithinRange());
     }
 
+    public function testIsYearWithinRange_returnsTrueIfEmpty_choice()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'choice',
+            'years' => array(2010, 2011),
+        ));
+
+        $field->submit(array(
+            'day' => '1',
+            'month' => '2',
+            'year' => '',
+        ));
+
+        $this->assertTrue($field->isYearWithinRange());
+    }
+
     public function testIsYearWithinRange_returnsFalseIfNotContained()
     {
         $field = new DateField('name', array(
@@ -185,6 +201,22 @@ class DateFieldTest extends DateTimeTestCase
         $this->assertTrue($field->isMonthWithinRange());
     }
 
+    public function testIsMonthWithinRange_returnsTrueIfEmpty_choice()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'choice',
+            'months' => array(6, 7),
+        ));
+
+        $field->submit(array(
+            'day' => '1',
+            'month' => '',
+            'year' => '2011',
+        ));
+
+        $this->assertTrue($field->isMonthWithinRange());
+    }
+
     public function testIsMonthWithinRange_returnsFalseIfNotContained()
     {
         $field = new DateField('name', array(
@@ -221,6 +253,22 @@ class DateFieldTest extends DateTimeTestCase
         $this->assertTrue($field->isDayWithinRange());
     }
 
+    public function testIsDayWithinRange_returnsTrueIfEmpty_choice()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'choice',
+            'days' => array(6, 7),
+        ));
+
+        $field->submit(array(
+            'day' => '',
+            'month' => '1',
+            'year' => '2011',
+        ));
+
+        $this->assertTrue($field->isDayWithinRange());
+    }
+
     public function testIsDayWithinRange_returnsFalseIfNotContained()
     {
         $field = new DateField('name', array(

+ 14 - 3
tests/Symfony/Tests/Component/Form/TimeFieldTest.php

@@ -159,7 +159,7 @@ class TimeFieldTest extends DateTimeTestCase
             'hours' => array(6, 7),
         ));
 
-        $field->submit(array('hour' => '', 'minute' => ''));
+        $field->submit(array('hour' => '', 'minute' => '06'));
 
         $this->assertTrue($field->isHourWithinRange());
     }
@@ -192,7 +192,7 @@ class TimeFieldTest extends DateTimeTestCase
             'minutes' => array(6, 7),
         ));
 
-        $field->submit(array('hour' => '', 'minute' => ''));
+        $field->submit(array('hour' => '06', 'minute' => ''));
 
         $this->assertTrue($field->isMinuteWithinRange());
     }
@@ -227,7 +227,18 @@ class TimeFieldTest extends DateTimeTestCase
             'with_seconds' => true,
         ));
 
-        $field->submit(array('hour' => '', 'minute' => ''));
+        $field->submit(array('hour' => '06', 'minute' => '06', 'second' => ''));
+
+        $this->assertTrue($field->isSecondWithinRange());
+    }
+
+    public function testIsSecondWithinRange_returnsTrueIfNotWithSeconds()
+    {
+        $field = new TimeField('name', array(
+            'seconds' => array(6, 7),
+        ));
+
+        $field->submit(array('hour' => '06', 'minute' => '06'));
 
         $this->assertTrue($field->isSecondWithinRange());
     }