From c02b4418bbab61a3c28178e75387d405f742425a Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 27 Mar 2012 14:38:45 +1300 Subject: [PATCH] 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. --- forms/DateField.php | 9 +++++++-- tests/forms/DateFieldTest.php | 13 ++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/forms/DateField.php b/forms/DateField.php index cc4c49615..a1f2c6b66 100644 --- a/forms/DateField.php +++ b/forms/DateField.php @@ -196,8 +196,13 @@ class DateField extends TextField { // Setting in correct locale if(is_array($val) && $this->validateArrayValue($val)) { // set() gets confused with custom date formats when using array notation - $this->valueObj = new Zend_Date($val, null, $this->locale); - $this->value = $this->valueObj->toArray(); + if(!(empty($val['day']) || empty($val['month']) || empty($val['year']))) { + $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()) else if(!empty($val) && Zend_Date::isDate($val, $this->getConfig('datavalueformat'), $this->locale)) { diff --git a/tests/forms/DateFieldTest.php b/tests/forms/DateFieldTest.php index 24d990cd2..5bb1c29e8 100644 --- a/tests/forms/DateFieldTest.php +++ b/tests/forms/DateFieldTest.php @@ -135,7 +135,18 @@ class DateFieldTest extends SapphireTest { // $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999)); // $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() { $f = new DateField('Date', 'Date'); $this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003)));