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.
This commit is contained in:
Renan Gonçalves 2013-03-01 13:37:52 +01:00
parent 1d529c1dd2
commit fd72f894ad
2 changed files with 30 additions and 1 deletions

View file

@ -6287,6 +6287,29 @@ class FormHelperTest extends CakeTestCase {
$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
*

View file

@ -2127,7 +2127,7 @@ class FormHelper extends AppHelper {
} 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 @@ class FormHelper extends AppHelper {
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;
}