mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Datetime labels should point at the first input.
Label elements generated for datetime/date/time inputs should point at the first generated input. Apply + update patch from MrRio. Fixes #427
This commit is contained in:
parent
99e9f62305
commit
c13e6588f5
2 changed files with 52 additions and 12 deletions
|
@ -5185,6 +5185,24 @@ class FormHelperTest extends CakeTestCase {
|
|||
$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
|
||||
*
|
||||
|
|
|
@ -1149,6 +1149,9 @@ class FormHelper extends AppHelper {
|
|||
/**
|
||||
* 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.
|
||||
|
@ -1156,22 +1159,41 @@ class FormHelper extends AppHelper {
|
|||
*/
|
||||
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';
|
||||
$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);
|
||||
}
|
||||
if (isset($options['id']) && isset($options['id'][$idKey])) {
|
||||
$labelAttributes['for'] = $options['id'][$idKey];
|
||||
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)) {
|
||||
|
|
Loading…
Add table
Reference in a new issue