Skip to content

Commit

Permalink
Datetime labels should point at the first input.
Browse files Browse the repository at this point in the history
Label elements generated for datetime/date/time inputs should point
at the first generated input. Apply + update patch from MrRio.

Fixes #427
  • Loading branch information
markstory committed Dec 20, 2011
1 parent 99e9f62 commit c13e658
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
18 changes: 18 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -5185,6 +5185,24 @@ public function testFormDateTimeMulti() {
$this->assertTags($result, $expected);
}

/**
* When changing the date format, the label should always focus the first select box when
* clicked.
*
* @return void
*/
function testDateTimeLabelIdMatchesFirstInput() {
$result = $this->Form->input('Model.date', array('type' => 'date'));
$this->assertContains('label for="ModelDateMonth"', $result);

$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
$this->assertContains('label for="ModelDateDay"', $result);

$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
$this->assertContains('label for="ModelDateYear"', $result);
}


/**
* testMonth method
*
Expand Down
48 changes: 35 additions & 13 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1149,29 +1149,51 @@ protected function _extractOption($name, $options, $default = null) {
/**
* Generate a label for an input() call.
*
* $options can contain a hash of id overrides. These overrides will be
* used instead of the generated values if present.
*
* @param string $fieldName
* @param string $label
* @param array $options Options for the label element.
* @return string Generated label element
*/
protected function _inputLabel($fieldName, $label, $options) {
$labelAttributes = $this->domId(array(), 'for');
$idKey = null;
if ($options['type'] === 'date' || $options['type'] === 'datetime') {
if (isset($options['dateFormat']) && $options['dateFormat'] === 'NONE') {
$labelAttributes['for'] .= 'Hour';
$idKey = 'hour';
} else {
$labelAttributes['for'] .= 'Month';
$idKey = 'month';
}
if (isset($options['id']) && isset($options['id'][$idKey])) {
$labelAttributes['for'] = $options['id'][$idKey];
$firstInput = 'M';
if (
array_key_exists('dateFormat', $options) &&
($options['dateFormat'] === null || $options['dateFormat'] === 'NONE')
) {
$firstInput = 'H';
} elseif (!empty($options['dateFormat'])) {
$firstInput = substr($options['dateFormat'], 0, 1);
}
switch ($firstInput) {
case 'D':
$idKey = 'day';
$labelAttributes['for'] .= 'Day';
break;
case 'Y':
$idKey = 'year';
$labelAttributes['for'] .= 'Year';
break;
case 'M':
$idKey = 'month';
$labelAttributes['for'] .= 'Month';
break;
case 'H':
$idKey = 'hour';
$labelAttributes['for'] .= 'Hour';
}
} elseif ($options['type'] === 'time') {
}
if ($options['type'] === 'time') {
$labelAttributes['for'] .= 'Hour';
if (isset($options['id']) && isset($options['id']['hour'])) {
$labelAttributes['for'] = $options['id']['hour'];
}
$idKey = 'hour';
}
if (isset($idKey) && isset($options['id']) && isset($options['id'][$idKey])) {
$labelAttributes['for'] = $options['id'][$idKey];
}

if (is_array($label)) {
Expand Down

0 comments on commit c13e658

Please sign in to comment.