Skip to content

Instantly share code, notes, and snippets.

@renan
Last active December 14, 2015 09:19
Show Gist options
  • Select an option

  • Save renan/5063785 to your computer and use it in GitHub Desktop.

Select an option

Save renan/5063785 to your computer and use it in GitHub Desktop.
diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php
index 4b14a4f..5401ea9 100644
--- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php
+++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -6288,6 +6288,29 @@ class FormHelperTest extends CakeTestCase {
}
/**
+ * testYearAutoExpand method
+ *
+ * @return void
+ */
+ public function testYearAutoExpand() {
+ $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
*
* @return void
diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php
index d4e8734..2fb45e8 100644
--- a/lib/Cake/View/Helper/FormHelper.php
+++ b/lib/Cake/View/Helper/FormHelper.php
@@ -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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment