]> git.andy128k.dev Git - ipf.git/commitdiff
nullable date fields
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 6 Aug 2013 22:26:40 +0000 (01:26 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 6 Aug 2013 22:26:40 +0000 (01:26 +0300)
ipf/form/field/date.php
ipf/form/field/datetime.php

index a738c342471cfd9328a86033c7a36eb2b754fbbb..51448a2c86c3b4663da4f6b9b83cec5a28468a3b 100644 (file)
@@ -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);
index 5eb62009ff7ae58ed72035e71c5dae7ccc90cef3..b58d2e9f61abad7c1b65bb072060938424fddac7 100644 (file)
@@ -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.'));
     }
 }