BUGFIX Using DateField "dmyfields" option, if you set empty

day/month/year values, valueObj on DateField will contain erroneous values.
Check that all the value inputs aren't null or empty values BEFORE
calling Zend_Date on the value.
This commit is contained in:
Sean Harvey 2012-03-27 14:38:45 +13:00
parent 06a4e1aad6
commit c02b4418bb
2 changed files with 19 additions and 3 deletions

View File

@ -196,8 +196,13 @@ class DateField extends TextField {
// Setting in correct locale // Setting in correct locale
if(is_array($val) && $this->validateArrayValue($val)) { if(is_array($val) && $this->validateArrayValue($val)) {
// set() gets confused with custom date formats when using array notation // set() gets confused with custom date formats when using array notation
$this->valueObj = new Zend_Date($val, null, $this->locale); if(!(empty($val['day']) || empty($val['month']) || empty($val['year']))) {
$this->value = $this->valueObj->toArray(); $this->valueObj = new Zend_Date($val, null, $this->locale);
$this->value = $this->valueObj->toArray();
} else {
$this->value = $val;
$this->valueObj = null;
}
} }
// load ISO date from database (usually through Form->loadDataForm()) // load ISO date from database (usually through Form->loadDataForm())
else if(!empty($val) && Zend_Date::isDate($val, $this->getConfig('datavalueformat'), $this->locale)) { else if(!empty($val) && Zend_Date::isDate($val, $this->getConfig('datavalueformat'), $this->locale)) {

View File

@ -135,7 +135,18 @@ class DateFieldTest extends SapphireTest {
// $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999)); // $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999));
// $this->assertFalse($f->validate(new RequiredFields())); // $this->assertFalse($f->validate(new RequiredFields()));
} }
function testValidateEmptyArrayValuesSetsNullForValueObject() {
$f = new DateField('Date', 'Date');
$f->setConfig('dmyfields', true);
$f->setValue(array('day' => '', 'month' => '', 'year' => ''));
$this->assertNull($f->dataValue());
$f->setValue(array('day' => null, 'month' => null, 'year' => null));
$this->assertNull($f->dataValue());
}
function testValidateArrayValue() { function testValidateArrayValue() {
$f = new DateField('Date', 'Date'); $f = new DateField('Date', 'Date');
$this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003))); $this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003)));