Skip to content
Permalink
Browse files

Expanding year range based on the provided value.

This solves issues where editing a record with a year outside the year range would leave the year selection empty, as now it will expand to accomodate the value.
  • Loading branch information...
renan committed Mar 1, 2013
1 parent 1d529c1 commit fd72f894ad091bbe3c10314091ee3ab34769afa2
Showing with 30 additions and 1 deletion.
  1. +23 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +7 −1 lib/Cake/View/Helper/FormHelper.php
@@ -6287,6 +6287,29 @@ public function testYear() {
$this->assertContains('data[Contact][published][year]', $result);
}
/**
* testYearAutoExpandRange method
*
* @return void
*/
public function testYearAutoExpandRange() {
$this->Form->request->data['User']['birthday'] = '1930-10-10';
$result = $this->Form->year('User.birthday');
preg_match_all('/<option value="([\d]+)"/', $result, $matches);
$result = $matches[1];
$expected = range(date('Y') + 20, 1930);
$this->assertEquals($result, $expected);
$this->Form->request->data['Project']['release'] = '2050-10-10';
$result = $this->Form->year('Project.release');
preg_match_all('/<option value="([\d]+)"/', $result, $matches);
$result = $matches[1];
$expected = range(2050, date('Y') - 20);
$this->assertEquals($result, $expected);
}
/**
* testTextArea method
*
@@ -2127,7 +2127,7 @@ public function year($fieldName, $minYear = null, $maxYear = null, $attributes =
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
$yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
$yearOptions = array('value' => $attributes['value'], 'min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
if (isset($attributes['orderYear'])) {
$yearOptions['order'] = $attributes['orderYear'];
unset($attributes['orderYear']);
@@ -2764,6 +2764,12 @@ protected function _generateOptions($name, $options = array()) {
if ($min > $max) {
list($min, $max) = array($max, $min);
}
if (!empty($options['value']) && (int)$options['value'] < $min) {
$min = (int)$options['value'];
} elseif (!empty($options['value']) && (int)$options['value'] > $max) {
$max = (int)$options['value'];
}
for ($i = $min; $i <= $max; $i++) {
$data[$i] = $i;
}

3 comments on commit fd72f89

@luksm

This comment has been minimized.

Copy link
Contributor

luksm replied Jun 12, 2013

This is causing the year to return always with the current year, ignoring the default maxYear since the value will always be greater than the maxYear.

I don't know if this should be done in the formHelper by who is coding and check if the value year is greater than the maxYear set in the view.

@dereuromark

This comment has been minimized.

Copy link
Member

dereuromark replied Jun 12, 2013

Isnt't this already fixed in the current head? Do you got a test case to proof this?

@markstory

This comment has been minimized.

Copy link
Member

markstory replied Jun 13, 2013

As far as I know its already fixed. Have you tried reproducing with 2.3.6?

Please sign in to comment.
You can’t perform that action at this time.