Jelajahi Sumber

[Form] Fixed: If a DateField or TimeField is displayed with select boxes, either all or no select box must have a value selected

Bernhard Schussek 14 tahun lalu
induk
melakukan
14c3518c6e

+ 2 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/form.html.twig

@@ -141,6 +141,8 @@
 
 {% block date_time_field %}
 {% spaceless %}
+    {{ form_errors(field.date) }}
+    {{ form_errors(field.time) }}
     {{ form_field(field.date) }}
     {{ form_field(field.time) }}
 {% endspaceless %}

+ 24 - 0
src/Symfony/Component/Form/DateField.php

@@ -294,4 +294,28 @@ class DateField extends HybridField
 
         return null === $date || in_array($date->format('d'), $this->getOption('days'));
     }
+
+    /**
+     * Returns whether the field is neither completely filled (a selected
+     * value in each dropdown) nor completely empty
+     *
+     * @return Boolean
+     */
+    public function isPartiallyFilled()
+    {
+        if ($this->isField()) {
+            return false;
+        }
+
+        if ($this->isEmpty()) {
+            return false;
+        }
+
+        if ($this->get('year')->isEmpty() || $this->get('month')->isEmpty()
+                || $this->get('day')->isEmpty()) {
+            return true;
+        }
+
+        return false;
+    }
 }

+ 10 - 0
src/Symfony/Component/Form/Resources/config/validation.xml

@@ -43,6 +43,11 @@
   </class>
 
   <class name="Symfony\Component\Form\DateField">
+    <getter property="partiallyFilled">
+      <constraint name="AssertFalse">
+        <option name="message">The date is not fully selected</option>
+      </constraint>
+    </getter>
     <getter property="yearWithinRange">
       <constraint name="AssertTrue">
         <option name="message">The year is invalid</option>
@@ -61,6 +66,11 @@
   </class>
 
   <class name="Symfony\Component\Form\TimeField">
+    <getter property="partiallyFilled">
+      <constraint name="AssertFalse">
+        <option name="message">The time is not fully selected</option>
+      </constraint>
+    </getter>
     <getter property="hourWithinRange">
       <constraint name="AssertTrue">
         <option name="message">The hour is invalid</option>

+ 24 - 0
src/Symfony/Component/Form/TimeField.php

@@ -227,4 +227,28 @@ class TimeField extends Form
 
         return null === $date || in_array($date->format('s'), $this->getOption('seconds'));
     }
+
+    /**
+     * Returns whether the field is neither completely filled (a selected
+     * value in each dropdown) nor completely empty
+     *
+     * @return Boolean
+     */
+    public function isPartiallyFilled()
+    {
+        if ($this->isField()) {
+            return false;
+        }
+
+        if ($this->isEmpty()) {
+            return false;
+        }
+
+        if ($this->get('hour')->isEmpty() || $this->get('minute')->isEmpty()
+                || ($this->isWithSeconds() && $this->get('second')->isEmpty())) {
+            return true;
+        }
+
+        return false;
+    }
 }

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

@@ -232,4 +232,60 @@ class DateFieldTest extends DateTimeTestCase
 
         $this->assertFalse($field->isDayWithinRange());
     }
+
+    public function testIsPartiallyFilled_returnsFalseIfInput()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'input',
+        ));
+
+        $field->submit('7.6.2010');
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'choice',
+        ));
+
+        $field->submit(array(
+            'day' => '',
+            'month' => '',
+            'year' => '',
+        ));
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'choice',
+        ));
+
+        $field->submit(array(
+            'day' => '2',
+            'month' => '6',
+            'year' => '2010',
+        ));
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsTrueIfChoiceAndDayEmpty()
+    {
+        $field = new DateField('name', array(
+            'widget' => 'choice',
+        ));
+
+        $field->submit(array(
+            'day' => '',
+            'month' => '6',
+            'year' => '2010',
+        ));
+
+        $this->assertTrue($field->isPartiallyFilled());
+    }
 }

+ 108 - 0
tests/Symfony/Tests/Component/Form/TimeFieldTest.php

@@ -243,4 +243,112 @@ class TimeFieldTest extends DateTimeTestCase
 
         $this->assertFalse($field->isSecondWithinRange());
     }
+
+    public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+        ));
+
+        $field->submit(array(
+            'hour' => '',
+            'minute' => '',
+        ));
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty_withSeconds()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+            'with_seconds' => true,
+        ));
+
+        $field->submit(array(
+            'hour' => '',
+            'minute' => '',
+            'second' => '',
+        ));
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+        ));
+
+        $field->submit(array(
+            'hour' => '0',
+            'minute' => '0',
+        ));
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled_withSeconds()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+            'with_seconds' => true,
+        ));
+
+        $field->submit(array(
+            'hour' => '0',
+            'minute' => '0',
+            'second' => '0',
+        ));
+
+        $this->assertFalse($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsTrueIfChoiceAndHourEmpty()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+            'with_seconds' => true,
+        ));
+
+        $field->submit(array(
+            'hour' => '',
+            'minute' => '0',
+            'second' => '0',
+        ));
+
+        $this->assertTrue($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsTrueIfChoiceAndMinuteEmpty()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+            'with_seconds' => true,
+        ));
+
+        $field->submit(array(
+            'hour' => '0',
+            'minute' => '',
+            'second' => '0',
+        ));
+
+        $this->assertTrue($field->isPartiallyFilled());
+    }
+
+    public function testIsPartiallyFilled_returnsTrueIfChoiceAndSecondsEmpty()
+    {
+        $field = new TimeField('name', array(
+            'widget' => 'choice',
+            'with_seconds' => true,
+        ));
+
+        $field->submit(array(
+            'hour' => '0',
+            'minute' => '0',
+            'second' => '',
+        ));
+
+        $this->assertTrue($field->isPartiallyFilled());
+    }
 }