Skip to content

Commit

Permalink
Update day() to use datetime widget.
Browse files Browse the repository at this point in the history
Update the day() method and its tests.
  • Loading branch information
markstory committed Feb 25, 2014
1 parent bead8ab commit 39d9a13
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
27 changes: 15 additions & 12 deletions src/View/Helper/FormHelper.php
Expand Up @@ -1858,19 +1858,22 @@ public function multiCheckbox($fieldName, $options, $attributes = []) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::day
*/
public function day($fieldName = null, $options = []) {
$options += array('empty' => true, 'value' => null);
$options = $this->_dateTimeSelected('day', $fieldName, $options);

if (strlen($attributes['value']) > 2) {
$date = date_create($attributes['value']);
$attributes['value'] = null;
if ($date) {
$attributes['value'] = $date->format('d');
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
$off = array_diff($this->_datetimeParts, ['day']);
$off = array_combine(
$off,
array_fill(0, count($off), false)
);
$options = $off + $options;

// If value is an integer reformat it.
if (isset($options['value']) && $options['value'] > 0 && $options['value'] < 31) {
$options['value'] = [
'year' => date('Y'),
'month' => date('m'),
'day' => (int)$options['value']
];
}
return $this->select($fieldName . ".day", $this->_generateOptions('day'), $options);
return $this->datetime($fieldName, $options);
}

/**
Expand Down
23 changes: 1 addition & 22 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -5516,7 +5516,7 @@ public function testDay() {
$result = $this->Form->day('Model.field', array('value' => false));
$expected = array(
array('select' => array('name' => 'Model[field][day]')),
array('option' => array('value' => '')),
array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '01')),
'1',
Expand Down Expand Up @@ -5571,27 +5571,6 @@ public function testDay() {
);
$this->assertTags($result, $expected);

$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
$result = $this->Form->day('Model.field', array('value' => true));
$expected = array(
array('select' => array('name' => 'Model[field][day]')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
'1',
'/option',
array('option' => array('value' => '02')),
'2',
'/option',
$daysRegex,
array('option' => array('value' => '10', 'selected' => 'selected')),
'10',
'/option',
$daysRegex,
'/select',
);
$this->assertTags($result, $expected);

$this->Form->request->data['Project']['release'] = '2050-10-10';
$result = $this->Form->day('Project.release');

Expand Down

0 comments on commit 39d9a13

Please sign in to comment.