From 2dcda9bd9174c057711b1b7ec551a2490a05f044 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 23 Sep 2008 01:44:40 +0000 Subject: [PATCH] BUGFIX: Fixed a number of really basic problems with a number of date fields - got basic loading and saving working across them all git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@62885 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/CompositeDateField.php | 5 +++-- forms/DMYCalendarDateField.php | 31 ++++++++++++++++++------------- forms/DMYDateField.php | 13 ++++++++----- forms/DateField.php | 8 ++++++-- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/forms/CompositeDateField.php b/forms/CompositeDateField.php index a99309803..b5a9d3bdc 100755 --- a/forms/CompositeDateField.php +++ b/forms/CompositeDateField.php @@ -7,7 +7,8 @@ class CompositeDateField extends DateField { function __construct($name, $title, $value = null, $yearRange = null){ - list($year,$month, $date) = explode('-', $value); + $year = $month = $date = null; + if($value) list($year,$month, $date) = explode('-', $value); $this->dateDropdown = new DropdownField($name."[date]", "", array('NotSet' => '('._t('CompositeDateField.DAY', 'Day').')', @@ -32,7 +33,7 @@ class CompositeDateField extends DateField { ); if($yearRange == null){ - $this->customiseYearDropDown($name, "1995-2006", $year); + $this->customiseYearDropDown($name, "1995-2012", $year); }else{ $this->customiseYearDropDown($name, $yearRange, $year); } diff --git a/forms/DMYCalendarDateField.php b/forms/DMYCalendarDateField.php index 49a30c23e..87523657e 100755 --- a/forms/DMYCalendarDateField.php +++ b/forms/DMYCalendarDateField.php @@ -20,29 +20,34 @@ class DMYCalendarDateField extends CalendarDateField { $id = $this->id(); $val = $this->attrValue(); + $day = $month = $year = null; + if( preg_match( '/^\d{2}\/\d{2}\/\d{4}$/', $val ) ) { $dateArray = explode( '/', $val ); - $val = $dateArray[2] . '-' . $dateArray[1] . '-' . $dateArray[0]; } - $dateArray = explode( '-', $val ); + if($val) { + $dateArray = explode( '-', $val ); + $day = $dateArray[2]; + $month = $dateArray[1]; + $year = $dateArray[0]; + } - $day = $dateArray[2]; - $month = $dateArray[1]; - $year = $dateArray[0]; - - preg_match('/(.*)[(.+)]$/', $this->name, $fieldNameParts ); - - $fieldNamePrefix = $fieldNameParts[1]; - $fieldName = $fieldNameParts[2]; + if(preg_match('/(.*)[(.+)]$/', $this->name, $fieldNameParts)) { + $fieldNamePrefix = $fieldNameParts[1]; + $fieldName = $fieldNameParts[2]; + } else { + $fieldNamePrefix = $this->name; + $fieldName = $this->name; + } return << - / - / - + / + / +
HTML; diff --git a/forms/DMYDateField.php b/forms/DMYDateField.php index f7dbfc379..031bd3eba 100644 --- a/forms/DMYDateField.php +++ b/forms/DMYDateField.php @@ -34,12 +34,15 @@ class DMYDateField extends CalendarDateField { $val = $dateArray[2] . '-' . $dateArray[1] . '-' . $dateArray[0]; } + + $day = $month = $year = null; + if($val) { + $dateArray = explode( '-', $val ); - $dateArray = explode( '-', $val ); - - $day = $dateArray[2]; - $month = $dateArray[1]; - $year = $dateArray[0]; + $day = $dateArray[2]; + $month = $dateArray[1]; + $year = $dateArray[0]; + } $fieldName = $this->name; diff --git a/forms/DateField.php b/forms/DateField.php index fb2e090a0..b8d59744b 100755 --- a/forms/DateField.php +++ b/forms/DateField.php @@ -8,7 +8,7 @@ class DateField extends TextField { function setValue($val) { - if($val && preg_match('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/', $val)) { + if(is_string($val) && preg_match('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/', $val)) { $this->value = preg_replace('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/','\\3/\\2/\\1', $val); } else { $this->value = $val; @@ -17,7 +17,11 @@ class DateField extends TextField { function dataValue() { if(is_array($this->value)) { - return $this->value['Year'] . '-' . $this->value['Month'] . '-' . $this->value['Day']; + if(isset($this->value['Year']) && isset($this->value['Month']) && isset($this->value['Day'])) { + return $this->value['Year'] . '-' . $this->value['Month'] . '-' . $this->value['Day']; + } else { + user_error("Bad DateField value " . var_export($this->value,true), E_USER_WARNING); + } } elseif(preg_match('/^([\d]{1,2})\/([\d]{1,2})\/([\d]{2,4})/', $this->value, $parts)) { return "$parts[3]-$parts[2]-$parts[1]"; } elseif(!empty($this->value)) {