Skip to content
Browse files

Fix to the date input year field

Create the field if no value is informed only to the maxYear and not to
the current date.
  • Loading branch information...
1 parent 691288a commit dc3f9113b03653a7fe9ec44354540404dd7e2cbf @luksm luksm committed with markstory
Showing with 30 additions and 1 deletion.
  1. +25 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +5 −1 lib/Cake/View/Helper/FormHelper.php
View
25 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -6629,6 +6629,31 @@ public function testYearAutoExpandRange() {
}
/**
+ * testInputDateMaxYear method
+ *
+ * Let's say we want to only allow users born from
+ * 2006 to 2008 to register
+ * This beeing the first singup page, we still don't have any data
+ *
+ * @return void
+ */
+ public function testInputDateMaxYear() {
+ $this->Form->request->data = array();
+ $this->Form->create('User');
+ $result = $this->Form->input('birthday',
+ array(
+ 'label' => false,
+ 'div' => false,
+ 'type' => 'date',
+ 'dateFormat' => 'DMY',
+ 'minYear' => 2006,
+ 'maxYear' => 2008
+ )
+ );
+ $this->assertContains('value="2008" selected="selected"', $result);
+ }
+
+/**
* testTextArea method
*
* @return void
View
6 lib/Cake/View/Helper/FormHelper.php
@@ -2359,7 +2359,11 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
}
if ($attributes['value'] === null && $attributes['empty'] != true) {
- $attributes['value'] = time();
+ if(!empty($attributes['maxYear']) && $attributes['maxYear'] < date('Y')) {
+ $attributes['value'] = strtotime(date($attributes['maxYear'] . '-m-d'));
+ } else {
+ $attributes['value'] = time();
+ }
}
if (!empty($attributes['value'])) {

0 comments on commit dc3f911

Please sign in to comment.
Something went wrong with that request. Please try again.