Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Form] Added validation of years, months and days to DateField
  • Loading branch information
Bernhard Schussek authored and fabpot committed Oct 22, 2010
1 parent e4c2170 commit 6c7fab2
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Symfony/Component/Form/DateField.php
Expand Up @@ -271,4 +271,49 @@ protected function addChoiceFields()
'choices' => $this->generatePaddedChoices($this->getOption('days'), 2),
)));
}

/**
* Returns whether the year of the field's data is validd

This comment has been minimized.

Copy link
@staabm

staabm Oct 25, 2010

Contributor

typo "validd" -> "valid"

*
* The year is valid if it is contained in the list passed to the field's
* option "years".
*
* @return boolean
*/
public function isYearWithinRange()
{
$date = $this->getNormalizedData();

return $date === null || in_array($date->format('Y'), $this->getOption('years'));
}

/**
* Returns whether the month of the field's data is validd

This comment has been minimized.

Copy link
@staabm

staabm Oct 25, 2010

Contributor

typo "validd" -> "valid"

*
* The month is valid if it is contained in the list passed to the field's
* option "months".
*
* @return boolean
*/
public function isMonthWithinRange()
{
$date = $this->getNormalizedData();

return $date === null || in_array($date->format('m'), $this->getOption('months'));
}

/**
* Returns whether the day of the field's data is validd

This comment has been minimized.

Copy link
@staabm

staabm Oct 25, 2010

Contributor

typo "validd" -> "valid"

*
* The day is valid if it is contained in the list passed to the field's
* option "days".
*
* @return boolean
*/
public function isDayWithinRange()
{
$date = $this->getNormalizedData();

return $date === null || in_array($date->format('d'), $this->getOption('days'));
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Form/Resources/config/validation.xml
Expand Up @@ -38,4 +38,22 @@
</constraint>
</getter>
</class>

<class name="Symfony\Component\Form\DateField">
<getter property="yearWithinRange">
<constraint name="AssertTrue">
<option name="message">The year is invalid</option>
</constraint>
</getter>
<getter property="monthWithinRange">
<constraint name="AssertTrue">
<option name="message">The month is invalid</option>
</constraint>
</getter>
<getter property="dayWithinRange">
<constraint name="AssertTrue">
<option name="message">The day is invalid</option>
</constraint>
</getter>
</class>
</constraint-mapping>
117 changes: 117 additions & 0 deletions tests/Symfony/Tests/Component/Form/DateFieldTest.php
Expand Up @@ -116,4 +116,121 @@ public function testSetData_differentTimezones()

$this->assertEquals('01.06.2010', $field->getDisplayedData());
}

public function testIsYearWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'widget' => 'input',
'years' => array(2010, 2011),
));

$field->setLocale('de_AT');
$field->bind('2.6.2010');

$this->assertTrue($field->isYearWithinRange());
}

public function testIsYearWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'widget' => 'input',
'years' => array(2010, 2011),
));

$field->setLocale('de_AT');
$field->bind('');

$this->assertTrue($field->isYearWithinRange());
}

public function testIsYearWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'widget' => 'input',
'years' => array(2010, 2012),
));

$field->setLocale('de_AT');
$field->bind('2.6.2011');

$this->assertFalse($field->isYearWithinRange());
}

public function testIsMonthWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'widget' => 'input',
'months' => array(6, 7),
));

$field->setLocale('de_AT');
$field->bind('2.6.2010');

$this->assertTrue($field->isMonthWithinRange());
}

public function testIsMonthWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'widget' => 'input',
'months' => array(6, 7),
));

$field->setLocale('de_AT');
$field->bind('');

$this->assertTrue($field->isMonthWithinRange());
}

public function testIsMonthWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'widget' => 'input',
'months' => array(6, 8),
));

$field->setLocale('de_AT');
$field->bind('2.7.2010');

$this->assertFalse($field->isMonthWithinRange());
}

public function testIsDayWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'widget' => 'input',
'days' => array(6, 7),
));

$field->setLocale('de_AT');
$field->bind('6.6.2010');

$this->assertTrue($field->isDayWithinRange());
}

public function testIsDayWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'widget' => 'input',
'days' => array(6, 7),
));

$field->setLocale('de_AT');
$field->bind('');

$this->assertTrue($field->isDayWithinRange());
}

public function testIsDayWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'widget' => 'input',
'days' => array(6, 8),
));

$field->setLocale('de_AT');
$field->bind('7.6.2010');

$this->assertFalse($field->isDayWithinRange());
}
}

0 comments on commit 6c7fab2

Please sign in to comment.