From 2006b1f37237a429fc9e48b41b76a491e403d373 Mon Sep 17 00:00:00 2001 From: nathangray Date: Fri, 8 Sep 2017 09:51:06 -0600 Subject: [PATCH] Etemplate - fix some bugs in min/max validation --- api/src/Etemplate/Widget/Date.php | 44 ++++++++++++++-------- api/src/Etemplate/Widget/test/DateTest.php | 15 ++++---- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/api/src/Etemplate/Widget/Date.php b/api/src/Etemplate/Widget/Date.php index 9a39a5c41f..7efad0bccd 100644 --- a/api/src/Etemplate/Widget/Date.php +++ b/api/src/Etemplate/Widget/Date.php @@ -150,6 +150,21 @@ class Date extends Transformer $value = self::get_array($content, $form_name); $valid =& self::get_array($validated, $form_name, true); + if($value) + { + try + { + $date = new Api\DateTime($value); + } + catch(\Exception $e) + { + $date = null; + $value = ''; + // this is not really a user error, but one of the clientside engine + self::set_validation_error($form_name,lang("'%1' is not a valid date !!!", $value).' '.$this->dataformat); + } + } + if ((string)$value === '' && $this->attrs['needed']) { self::set_validation_error($form_name,lang('Field must not be empty !!!')); @@ -162,29 +177,23 @@ class Date extends Transformer { $valid = (string)$value === '' ? '' : (int)$value; } - if($value) - { - try - { - $date = new Api\DateTime($value); - } - catch(\Exception $e) - { - $date = null; - $value = ''; - } - } + if (!empty($this->attrs['min'])) { if(is_numeric($this->attrs['min'])) { $min = new Api\DateTime(strtotime( $this->attrs['min'] . 'days')); } + elseif (preg_match('/[+-][[:digit:]]+[ymwd]/',$this->attrs['min'])) + { + // Relative date with periods + $min = new Api\DateTime(strtotime(str_replace(array('y','m','w','d'), array('years','months','weeks','days'), $this->attrs['min']))); + } else { $min = new Api\DateTime(strtotime($this->attrs['min'])); } - if($value < $min) + if($date < $min) { self::set_validation_error($form_name,lang( "Value has to be at least '%1' !!!", @@ -199,11 +208,16 @@ class Date extends Transformer { $max = new Api\DateTime(strtotime( $this->attrs['max'] . 'days')); } + elseif (preg_match('/[+-][[:digit:]]+[ymwd]/',$this->attrs['max'])) + { + // Relative date with periods + $max = new Api\DateTime(strtotime(str_replace(array('y','m','w','d'), array('years','months','weeks','days'), $this->attrs['max']))); + } else { $max = new Api\DateTime(strtotime($this->attrs['max'])); } - if($value < $max) + if($date > $max) { self::set_validation_error($form_name,lang( "Value has to be at maximum '%1' !!!", @@ -217,7 +231,7 @@ class Date extends Transformer // Not null, blank $value = ''; } - elseif (empty($this->attrs['dataformat'])) // integer timestamp + elseif ($date && empty($this->attrs['dataformat'])) // integer timestamp { $valid = $date->format('ts'); } diff --git a/api/src/Etemplate/Widget/test/DateTest.php b/api/src/Etemplate/Widget/test/DateTest.php index 36aa884f8d..029d4d01a0 100644 --- a/api/src/Etemplate/Widget/test/DateTest.php +++ b/api/src/Etemplate/Widget/test/DateTest.php @@ -117,9 +117,6 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest array(array('date' => 'Invalid')), array(array('date_time' => 'Invalid')), array(array('date_timeonly' => 'Invalid')), - array(array('date' => -1000)), - array(array('date_time' => -1000)), - array(array('date_timeonly' => -1000)), ); } @@ -144,8 +141,9 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest // Need to exec the template so the widget is there to modify $result = $this->mockedExec($etemplate, $content, array(), array(), array()); - // Set limit + // Set limits $etemplate->getElementById('date')->attrs['min'] = $min; + $etemplate->getElementById('date')->attrs['max'] = null; // Check for the load $data = array(); @@ -167,7 +165,7 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest $content = static::$mocked_exec_result; static::$mocked_exec_result = array(); - return $this->validateTest($content, + $this->validateTest($content, $error ? array() : array('date' => is_string($value) ? strtotime($value) : $value), $error ? array('date' => $error) : array() ); @@ -185,7 +183,7 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest array('two days from now', 2, FALSE), array(time(), 2, TRUE), array(time(), -1, FALSE), - array('yesterday', 0, TRUE), + array('yesterday', 'today', TRUE), // Different periods array('yesterday', '+2d', TRUE), array('yesterday', '-2d', FALSE), @@ -219,8 +217,9 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest // Need to exec the template so the widget is there to modify $result = $this->mockedExec($etemplate, $content, array(), array(), array()); - // Set limit - $etemplate->getElementById('date')->attrs['min'] = $max; + // Set limits + $etemplate->getElementById('date')->attrs['min'] = null; + $etemplate->getElementById('date')->attrs['max'] = $max; // Check for the load $data = array();