From 311ff2314bfbfb5fc2a48a1cf5d1d3fc347757b6 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Wed, 7 Aug 2013 01:26:40 +0300 Subject: [PATCH] nullable date fields --- ipf/form/field/date.php | 5 ++++- ipf/form/field/datetime.php | 30 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/ipf/form/field/date.php b/ipf/form/field/date.php index a738c34..51448a2 100644 --- a/ipf/form/field/date.php +++ b/ipf/form/field/date.php @@ -9,11 +9,14 @@ class IPF_Form_Field_Date extends IPF_Form_Field '%d %b %Y', '%d %b, %Y', // '25 Oct 2006', '25 Oct, 2006' '%B %d %Y', '%B %d, %Y', // 'October 25 2006', 'October 25, 2006' '%d %B %Y', '%d %B, %Y', // '25 October 2006', '25 October, 2006' - ); + ); public function clean($value) { parent::clean($value); + if (in_array($value, $this->empty_values)) + return null; + foreach ($this->input_formats as $format) { if (false !== ($date = strptime($value, $format))) { $day = str_pad($date['tm_mday'], 2, '0', STR_PAD_LEFT); diff --git a/ipf/form/field/datetime.php b/ipf/form/field/datetime.php index 5eb6200..b58d2e9 100644 --- a/ipf/form/field/datetime.php +++ b/ipf/form/field/datetime.php @@ -4,20 +4,24 @@ class IPF_Form_Field_Datetime extends IPF_Form_Field { public $widget = 'IPF_Form_Widget_DatetimeInput'; public $input_formats = array( - '%Y-%m-%d %H:%M:%S', // '2006-10-25 14:30:59' - '%Y-%m-%d %H:%M', // '2006-10-25 14:30' - '%Y-%m-%d', // '2006-10-25' - '%m/%d/%Y %H:%M:%S', // '10/25/2006 14:30:59' - '%m/%d/%Y %H:%M', // '10/25/2006 14:30' - '%m/%d/%Y', // '10/25/2006' - '%m/%d/%y %H:%M:%S', // '10/25/06 14:30:59' - '%m/%d/%y %H:%M', // '10/25/06 14:30' - '%m/%d/%y', // '10/25/06' - ); + '%Y-%m-%d %H:%M:%S', // '2006-10-25 14:30:59' + '%Y-%m-%d %H:%M', // '2006-10-25 14:30' + '%Y-%m-%d', // '2006-10-25' + '%m/%d/%Y %H:%M:%S', // '10/25/2006 14:30:59' + '%m/%d/%Y %H:%M', // '10/25/2006 14:30' + '%m/%d/%Y', // '10/25/2006' + '%m/%d/%y %H:%M:%S', // '10/25/06 14:30:59' + '%m/%d/%y %H:%M', // '10/25/06 14:30' + '%m/%d/%y', // '10/25/06' + ); public function clean($value) { parent::clean($value); + if (in_array($value, $this->empty_values)) { + return null; + } + $out = null; foreach ($this->input_formats as $format) { if (false !== ($date = strptime($value, $format))) { @@ -30,13 +34,9 @@ class IPF_Form_Field_Datetime extends IPF_Form_Field if ($s > 59) $s=59; $s = str_pad($s, 2, '0', STR_PAD_LEFT); $out = $year.'-'.$month.'-'.$day.' '.$h.':'.$m.':'.$s; - break; + return gmdate('Y-m-d H:i:s', strtotime($out)); } } - if ($out !== null) { - // We internally use GMT, so we convert it to a GMT date. - return gmdate('Y-m-d H:i:s', strtotime($out)); - } throw new IPF_Exception_Form(__('Enter a valid date/time.')); } } -- 2.49.0