Skip to content

Commit

Permalink
Refactoring label generation into a method.
Browse files Browse the repository at this point in the history
Fixing issues with labels having a for attribute = Array when an array of id's is provided.
Fixing issues where a date input would require you to provide id keys for hour, minute, and meridian even if you were not using them.
Fixes #411
  • Loading branch information
markstory committed Mar 5, 2010
1 parent 38d53b9 commit 1d449e3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
77 changes: 50 additions & 27 deletions cake/libs/view/helpers/form.php
Expand Up @@ -803,32 +803,7 @@ function input($fieldName, $options = array()) {
}

if ($label !== false) {
$labelAttributes = $this->domId(array(), 'for');
if ($options['type'] === 'date' || $options['type'] === 'datetime') {
if (isset($options['dateFormat']) && $options['dateFormat'] === 'NONE') {
$labelAttributes['for'] .= 'Hour';
} else {
$labelAttributes['for'] .= 'Month';
}
} elseif ($options['type'] === 'time') {
$labelAttributes['for'] .= 'Hour';
}

if (is_array($label)) {
$labelText = null;
if (isset($label['text'])) {
$labelText = $label['text'];
unset($label['text']);
}
$labelAttributes = array_merge($labelAttributes, $label);
} else {
$labelText = $label;
}

if (isset($options['id'])) {
$labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
}
$label = $this->label($fieldName, $labelText, $labelAttributes);
$label = $this->_inputLabel($fieldName, $label, $options);
}

$error = $this->_extractOption('error', $options, null);
Expand Down Expand Up @@ -940,6 +915,50 @@ function _extractOption($name, $options, $default = null) {
return $default;
}

/**
* Generate a label for an input() call.
*
* @param array $options Options for the label element.
* @return string Generated label element
* @access protected
*/
function _inputLabel($fieldName, $label, $options) {
$labelAttributes = $this->domId(array(), 'for');
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];
}
} elseif ($options['type'] === 'time') {
$labelAttributes['for'] .= 'Hour';
if (isset($options['id']) && isset($options['id']['hour'])) {
$labelAttributes['for'] = $options['id']['hour'];
}
}

if (is_array($label)) {
$labelText = null;
if (isset($label['text'])) {
$labelText = $label['text'];
unset($label['text']);
}
$labelAttributes = array_merge($labelAttributes, $label);
} else {
$labelText = $label;
}

if (isset($options['id']) && is_string($options['id'])) {
$labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
}
return $this->label($fieldName, $labelText, $labelAttributes);
}

/**
* Creates a checkbox input widget.
*
Expand Down Expand Up @@ -1752,7 +1771,7 @@ function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected
}
}

$elements = array('Day','Month','Year','Hour','Minute','Meridian');
$elements = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
$defaults = array(
'minYear' => null, 'maxYear' => null, 'separator' => '-',
'interval' => 1, 'monthNames' => true
Expand All @@ -1779,6 +1798,10 @@ function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected
}
} elseif (is_array($attributes['id'])) {
// check for missing ones and build selectAttr for each element
$attributes['id'] += array(
'month' => '', 'year' => '', 'day' => '',
'hour' => '', 'minute' => '', 'meridian' => ''
);
foreach ($elements as $element) {
$selectAttrName = 'select' . $element . 'Attr';
${$selectAttrName} = $attributes;
Expand Down
7 changes: 7 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -1880,6 +1880,13 @@ function testInputDatetime() {
$this->Form->data = array('Contact' => array('created' => null));
$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
$this->assertPattern('/for\="ContactCreatedMonth"/', $result);

$result = $this->Form->input('Contact.created', array(
'type' => 'date',
'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
'timeFormat' => 'NONE'
));
$this->assertPattern('/for\="created-month"/', $result);
}
/**
* Test generating checkboxes in a loop.
Expand Down

0 comments on commit 1d449e3

Please sign in to comment.