Skip to content

Commit

Permalink
[Form] Added validiation of hours, minutes and seconds to TimeField
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Oct 22, 2010
1 parent 6c7fab2 commit 72dcee5
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Symfony/Component/Form/Resources/config/validation.xml
Expand Up @@ -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>
47 changes: 45 additions & 2 deletions src/Symfony/Component/Form/TimeField.php
Expand Up @@ -75,8 +75,6 @@ protected function configure()
}
}

$transformers = array();

$fields = array('hour', 'minute');

if ($this->getOption('with_seconds')) {
Expand Down Expand Up @@ -158,4 +156,49 @@ protected function generatePaddedChoices(array $values, $padLength)

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 changes: 102 additions & 0 deletions tests/Symfony/Tests/Component/Form/TimeFieldTest.php
Expand Up @@ -132,4 +132,106 @@ public function testSetData_differentTimezones()

$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());
}
}

0 comments on commit 72dcee5

Please sign in to comment.