mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
1d529c1dd2
commit
fd72f894ad
2 changed files with 30 additions and 1 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue