소스 검색

[Form] Added validiation of hours, minutes and seconds to TimeField

Bernhard Schussek 14 년 전
부모
커밋
72dcee594a

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

@@ -56,4 +56,22 @@
       </constraint>
     </getter>
   </class>
+
+  <class name="Symfony\Component\Form\TimeField">
+    <getter property="hourWithinRange">
+      <constraint name="AssertTrue">
+        <option name="message">The hour is invalid</option>
+      </constraint>
+    </getter>
+    <getter property="minuteWithinRange">
+      <constraint name="AssertTrue">
+        <option name="message">The minutes are invalid</option>
+      </constraint>
+    </getter>
+    <getter property="secondWithinRange">
+      <constraint name="AssertTrue">
+        <option name="message">The seconds are invalid</option>
+      </constraint>
+    </getter>
+  </class>
 </constraint-mapping>

+ 45 - 2
src/Symfony/Component/Form/TimeField.php

@@ -75,8 +75,6 @@ class TimeField extends FieldGroup
             }
         }
 
-        $transformers = array();
-
         $fields = array('hour', 'minute');
 
         if ($this->getOption('with_seconds')) {
@@ -158,4 +156,49 @@ class TimeField extends FieldGroup
 
         return $choices;
     }
+
+    /**
+     * Returns whether the hour of the field's data is valid
+     *
+     * The hour is valid if it is contained in the list passed to the field's
+     * option "hours".
+     *
+     * @return boolean
+     */
+    public function isHourWithinRange()
+    {
+        $date = $this->getNormalizedData();
+
+        return $date === null || in_array($date->format('H'), $this->getOption('hours'));
+    }
+
+    /**
+     * Returns whether the minute of the field's data is valid
+     *
+     * The minute is valid if it is contained in the list passed to the field's
+     * option "minutes".
+     *
+     * @return boolean
+     */
+    public function isMinuteWithinRange()
+    {
+        $date = $this->getNormalizedData();
+
+        return $date === null || in_array($date->format('i'), $this->getOption('minutes'));
+    }
+
+    /**
+     * Returns whether the second of the field's data is valid
+     *
+     * The second is valid if it is contained in the list passed to the field's
+     * option "seconds".
+     *
+     * @return boolean
+     */
+    public function isSecondWithinRange()
+    {
+        $date = $this->getNormalizedData();
+
+        return $date === null || in_array($date->format('s'), $this->getOption('seconds'));
+    }
 }

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

@@ -132,4 +132,106 @@ class TimeFieldTest extends DateTimeTestCase
 
         $this->assertEquals($displayedData, $field->getDisplayedData());
     }
+
+    public function testIsHourWithinRange_returnsTrueIfWithin()
+    {
+        $field = new TimeField('name', array(
+            'hours' => array(6, 7),
+        ));
+
+        $field->bind(array('hour' => '06', 'minute' => '12'));
+
+        $this->assertTrue($field->isHourWithinRange());
+    }
+
+    public function testIsHourWithinRange_returnsTrueIfEmpty()
+    {
+        $field = new TimeField('name', array(
+            'hours' => array(6, 7),
+        ));
+
+        $field->bind(array('hour' => '', 'minute' => ''));
+
+        $this->assertTrue($field->isHourWithinRange());
+    }
+
+    public function testIsHourWithinRange_returnsFalseIfNotContained()
+    {
+        $field = new TimeField('name', array(
+            'hours' => array(6, 7),
+        ));
+
+        $field->bind(array('hour' => '08', 'minute' => '12'));
+
+        $this->assertFalse($field->isHourWithinRange());
+    }
+
+    public function testIsMinuteWithinRange_returnsTrueIfWithin()
+    {
+        $field = new TimeField('name', array(
+            'minutes' => array(6, 7),
+        ));
+
+        $field->bind(array('hour' => '06', 'minute' => '06'));
+
+        $this->assertTrue($field->isMinuteWithinRange());
+    }
+
+    public function testIsMinuteWithinRange_returnsTrueIfEmpty()
+    {
+        $field = new TimeField('name', array(
+            'minutes' => array(6, 7),
+        ));
+
+        $field->bind(array('hour' => '', 'minute' => ''));
+
+        $this->assertTrue($field->isMinuteWithinRange());
+    }
+
+    public function testIsMinuteWithinRange_returnsFalseIfNotContained()
+    {
+        $field = new TimeField('name', array(
+            'minutes' => array(6, 7),
+        ));
+
+        $field->bind(array('hour' => '06', 'minute' => '08'));
+
+        $this->assertFalse($field->isMinuteWithinRange());
+    }
+
+    public function testIsSecondWithinRange_returnsTrueIfWithin()
+    {
+        $field = new TimeField('name', array(
+            'seconds' => array(6, 7),
+            'with_seconds' => true,
+        ));
+
+        $field->bind(array('hour' => '04', 'minute' => '05', 'second' => '06'));
+
+        $this->assertTrue($field->isSecondWithinRange());
+    }
+
+    public function testIsSecondWithinRange_returnsTrueIfEmpty()
+    {
+        $field = new TimeField('name', array(
+            'seconds' => array(6, 7),
+            'with_seconds' => true,
+        ));
+
+        $field->bind(array('hour' => '', 'minute' => ''));
+
+        $this->assertTrue($field->isSecondWithinRange());
+    }
+
+    public function testIsSecondWithinRange_returnsFalseIfNotContained()
+    {
+        $field = new TimeField('name', array(
+            'seconds' => array(6, 7),
+            'with_seconds' => true,
+        ));
+
+        $field->bind(array('hour' => '04', 'minute' => '05', 'second' => '08'));
+
+        $this->assertFalse($field->isSecondWithinRange());
+    }
 }