Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update year() to work with the datetime widget.

  • Loading branch information...
commit 41c3f5708e90ad1723501c43e968d31bd5cc2caf 1 parent e36e5de
@markstory markstory authored
View
66 src/View/Helper/FormHelper.php
@@ -1890,53 +1890,37 @@ public function day($fieldName = null, $options = []) {
* - `orderYear` - Ordering of year values in select options.
* Possible values 'asc', 'desc'. Default 'desc'
* - `value` The selected value of the input.
+ * - `maxYear` The max year to appear in the select element.
+ * - `minYear` The min year to appear in the select element.
*
* @param string $fieldName Prefix name for the SELECT element
- * @param integer $minYear First year in sequence
- * @param integer $maxYear Last year in sequence
- * @param array $attributes Attribute array for the select elements.
+ * @param array $options Options & attributes for the select elements.
* @return string Completed year select input
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::year
*/
- public function year($fieldName, $minYear = null, $maxYear = null, $attributes = array()) {
- $attributes += array('empty' => true, 'value' => null);
- if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
- if (is_array($value)) {
- $year = null;
- extract($value);
- $attributes['value'] = $year;
- } else {
- if (empty($value)) {
- if (!$attributes['empty'] && !$maxYear) {
- $attributes['value'] = 'now';
+ public function year($fieldName, $options = []) {
+ $off = array_diff($this->_datetimeParts, ['year']);
+ $off = array_combine(
+ $off,
+ array_fill(0, count($off), false)
+ );
+ $options = $off + $options;
- } elseif (!$attributes['empty'] && $maxYear && !$attributes['value']) {
- $attributes['value'] = $maxYear;
- }
- } else {
- $attributes['value'] = $value;
- }
- }
+ if (isset($options['value'])) {
+ $options['val'] = $options['value'];
}
- if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
- $date = date_create($attributes['value']);
- $attributes['value'] = null;
- if ($date) {
- $attributes['value'] = $date->format('Y');
- }
- } elseif ($attributes['value'] === false) {
- $attributes['value'] = null;
- }
- $yearOptions = array('value' => $attributes['value'], 'min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
- if (isset($attributes['orderYear'])) {
- $yearOptions['order'] = $attributes['orderYear'];
- unset($attributes['orderYear']);
+ // If value is an integer reformat it.
+ $len = isset($options['val']) ? strlen($options['val']) : 0;
+ if (isset($options['val']) && $len > 0 && $len < 5) {
+ $options['val'] = [
+ 'year' => (int)$options['val'],
+ 'month' => date('m'),
+ 'day' => date('d')
+ ];
}
- return $this->select(
- $fieldName . '.year', $this->_generateOptions('year', $yearOptions),
- $attributes
- );
+
+ return $this->datetime($fieldName, $options);
}
/**
@@ -2172,6 +2156,7 @@ public function dateTime($fieldName, $options = array()) {
'monthNames' => true,
'minYear' => null,
'maxYear' => null,
+ 'orderYear' => 'desc',
'timeFormat' => 12,
'second' => false,
];
@@ -2216,7 +2201,10 @@ protected function _datetimeOptions($options) {
if ($hasYear && isset($options['maxYear'])) {
$options['year']['end'] = $options['maxYear'];
}
- unset($options['minYear'], $options['maxYear']);
+ if ($hasYear && isset($options['orderYear'])) {
+ $options['year']['order'] = $options['orderYear'];
+ }
+ unset($options['minYear'], $options['maxYear'], $options['orderYear']);
if (is_array($options['month'])) {
$options['month']['names'] = $options['monthNames'];
View
43 tests/TestCase/View/Helper/FormHelperTest.php
@@ -1943,7 +1943,7 @@ public function testFormSecuredAndDisabled() {
$this->Form->textarea('Model.textarea', array('disabled' => true));
$this->Form->select('Model.select', array(1, 2), array('disabled' => true));
$this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
- $this->Form->year('Model.year', null, null, array('disabled' => true));
+ $this->Form->year('Model.year', array('disabled' => true));
$this->Form->month('Model.month', array('disabled' => true));
$this->Form->day('Model.day', array('disabled' => true));
$this->Form->hour('Model.hour', false, array('disabled' => true));
@@ -5792,10 +5792,10 @@ public function testHour() {
* @return void
*/
public function testYear() {
- $result = $this->Form->year('Model.field', ['minYear' => 2006, 'maxYear' => 2007]);
+ $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
$expected = array(
array('select' => array('name' => 'Model[field][year]')),
- array('option' => array('value' => '')),
+ array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '2007')),
'2007',
@@ -5808,13 +5808,14 @@ public function testYear() {
$this->assertTags($result, $expected);
$result = $this->Form->year('Model.field', [
+ 'value' => '',
'minYear' => 2006,
'maxYear' => 2007,
'orderYear' => 'asc'
]);
$expected = array(
array('select' => array('name' => 'Model[field][year]')),
- array('option' => array('value' => '')),
+ array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '2006')),
'2006',
@@ -5829,35 +5830,15 @@ public function testYear() {
$this->Form->request->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', [
'empty' => false,
- 'maxYear' => 2006,
- 'minYear' => 2007,
- ]);
- $expected = array(
- array('select' => array('name' => 'Contact[published][year]')),
- array('option' => array('value' => '2007')),
- '2007',
- '/option',
- array('option' => array('value' => '2006', 'selected' => 'selected')),
- '2006',
- '/option',
- '/select',
- );
- $this->assertTags($result, $expected);
-
- $this->Form->request->data['Contact']['published'] = '';
- $result = $this->Form->year('Contact.published', [
'minYear' => 2006,
'maxYear' => 2007,
- 'value' => 2007
]);
$expected = array(
array('select' => array('name' => 'Contact[published][year]')),
- array('option' => array('value' => '')),
- '/option',
- array('option' => array('value' => '2007', 'selected' => 'selected')),
+ array('option' => array('value' => '2007')),
'2007',
'/option',
- array('option' => array('value' => '2006')),
+ array('option' => array('value' => '2006', 'selected' => 'selected')),
'2006',
'/option',
'/select',
@@ -5871,13 +5852,12 @@ public function testYear() {
* @return void
*/
public function testYearAutoExpandRange() {
- $this->markTestIncomplete('Need to revisit once models work again.');
$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);
+ $expected = range(date('Y') + 5, 1930);
$this->assertEquals($expected, $result);
$this->Form->request->data['Project']['release'] = '2050-10-10';
@@ -5885,11 +5865,14 @@ public function testYearAutoExpandRange() {
preg_match_all('/<option value="([\d]+)"/', $result, $matches);
$result = $matches[1];
- $expected = range(2050, date('Y') - 20);
+ $expected = range(2050, date('Y') - 5);
$this->assertEquals($expected, $result);
$this->Form->request->data['Project']['release'] = '1881-10-10';
- $result = $this->Form->year('Project.release', 1890, 1900);
+ $result = $this->Form->year('Project.release', [
+ 'minYear' => 1890,
+ 'maxYear' => 1900
+ ]);
preg_match_all('/<option value="([\d]+)"/', $result, $matches);
$result = $matches[1];
Please sign in to comment.
Something went wrong with that request. Please try again.