Etemplate - fix some bugs in min/max validation

This commit is contained in:
nathangray 2017-09-08 09:51:06 -06:00
parent 0b3cbdeae5
commit 2006b1f372
2 changed files with 36 additions and 23 deletions

View File

@ -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');
}

View File

@ -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();