Make sure a datetime instance is valid to avoid fatal errors.

This commit is contained in:
jalbertocr 2014-02-19 20:50:30 -03:00
parent 4497bab131
commit 35f152b333
2 changed files with 119 additions and 10 deletions

View file

@ -6552,6 +6552,22 @@ class FormHelperTest extends CakeTestCase {
'*/select',
);
$this->assertTags($result, $expected);
$this->Form->request->data['Model']['field'] = '12a';
$result = $this->Form->month('Model.field');
$expected = array(
array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
date('F', strtotime('2008-01-01 00:00:00')),
'/option',
array('option' => array('value' => '02')),
date('F', strtotime('2008-02-01 00:00:00')),
'/option',
'*/select',
);
$this->assertTags($result, $expected);
}
/**
@ -6662,6 +6678,23 @@ class FormHelperTest extends CakeTestCase {
'/select',
);
$this->assertTags($result, $expected);
$this->Form->request->data['Model']['field'] = '12e';
$result = $this->Form->day('Model.field');
$expected = array(
array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
'1',
'/option',
array('option' => array('value' => '02')),
'2',
'/option',
$daysRegex,
'/select',
);
$this->assertTags($result, $expected);
}
/**
@ -6754,6 +6787,25 @@ class FormHelperTest extends CakeTestCase {
'/select',
);
$this->assertTags($result, $expected);
$result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
$expected = array(
array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '00')),
'00',
'/option',
array('option' => array('value' => '01')),
'01',
'/option',
array('option' => array('value' => '02')),
'02',
'/option',
$minutesRegex,
'/select',
);
$this->assertTags($result, $expected);
}
/**
@ -6852,6 +6904,23 @@ class FormHelperTest extends CakeTestCase {
'/select',
);
$this->assertTags($result, $expected);
$this->Form->request->data['Model']['field'] = '18a';
$result = $this->Form->hour('Model.field', false);
$expected = array(
array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
'1',
'/option',
array('option' => array('value' => '02')),
'2',
'/option',
$hoursRegex,
'/select',
);
$this->assertTags($result, $expected);
}
/**
@ -7034,6 +7103,22 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->year('published', array(), array(), array('empty' => false));
$this->assertContains('data[Contact][published][year]', $result);
$this->Form->request->data['Contact']['published'] = '2014ee';
$result = $this->Form->year('Contact.published', 2010, 2011);
$expected = array(
array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '2011')),
'2011',
'/option',
array('option' => array('value' => '2010')),
'2010',
'/option',
'/select',
);
$this->assertTags($result, $expected);
}
/**

View file

@ -2116,7 +2116,11 @@ class FormHelper extends AppHelper {
$attributes = $this->_dateTimeSelected('day', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$attributes['value'] = date_create($attributes['value'])->format('d');
if (($Date = date_create($attributes['value'])) !== false) {
$attributes['value'] = $Date->format('d');
} else {
$attributes['value'] = null;
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@ -2163,7 +2167,11 @@ class FormHelper extends AppHelper {
}
if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
$attributes['value'] = date_create($attributes['value'])->format('Y');
if (($Date = date_create($attributes['value'])) !== false) {
$attributes['value'] = $Date->format('Y');
} else {
$attributes['value'] = null;
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@ -2199,7 +2207,11 @@ class FormHelper extends AppHelper {
$attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$attributes['value'] = date_create($attributes['value'])->format('m');
if (($Date = date_create($attributes['value'])) !== false) {
$attributes['value'] = $Date->format('m');
} else {
$attributes['value'] = null;
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@ -2235,11 +2247,15 @@ class FormHelper extends AppHelper {
$attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$Date = new DateTime($attributes['value']);
if ($format24Hours) {
$attributes['value'] = $Date->format('H');
} else {
$attributes['value'] = $Date->format('g');
try {
$Date = new DateTime($attributes['value']);
if ($format24Hours) {
$attributes['value'] = $Date->format('H');
} else {
$attributes['value'] = $Date->format('g');
}
} catch (Exception $e) {
$attributes['value'] = null;
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
@ -2278,7 +2294,11 @@ class FormHelper extends AppHelper {
$attributes = $this->_dateTimeSelected('min', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$attributes['value'] = date_create($attributes['value'])->format('i');
if (($Date = date_create($attributes['value'])) !== false) {
$attributes['value'] = $Date->format('i');
} else {
$attributes['value'] = null;
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@ -2346,7 +2366,11 @@ class FormHelper extends AppHelper {
$attributes['value'] = date('a');
}
} else {
$attributes['value'] = date_create($attributes['value'])->format('a');
if (($Date = date_create($attributes['value'])) !== false) {
$attributes['value'] = $Date->format('a');
} else {
$attributes['value'] = null;
}
}
}
}