[Form] Added validation of years, months and days to DateField

This commit is contained in:
Bernhard Schussek 2010-10-21 21:35:56 +02:00 committed by Fabien Potencier
parent e4c21708ca
commit 6c7fab212b
3 changed files with 180 additions and 0 deletions

View File

@ -271,4 +271,49 @@ class DateField extends HybridField
'choices' => $this->generatePaddedChoices($this->getOption('days'), 2),
)));
}
/**
* Returns whether the year of the field's data is validd
*
* 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
*
* 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
*
* 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'));
}
}

View File

@ -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>

View File

@ -116,4 +116,121 @@ class DateFieldTest extends DateTimeTestCase
$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());
}
}