Skip to content

Commit

Permalink
Make it easy to put inputs *inside* label elements.
Browse files Browse the repository at this point in the history
This is a frequently requested feature that is much easier to implement
now. Add it so it is easy to integrate bootstrap & older versions of
foundation.
  • Loading branch information
markstory committed Jan 12, 2014
1 parent 0c46ccf commit f179736
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/View/Input/Radio.php
Expand Up @@ -36,6 +36,16 @@ class Radio {
/**
* Constructor
*
* This class uses a few templates:
*
* - `radio` Used to generate the input for a radio button.
* Can use the following variables `name`, `value`, `attrs`.
* - `label` Used to generate the label for a radio button.
* Can use the following variables `attrs`, `text` and `input`.
* - `radioContainer` Used to generate the container element for
* the radio + input element. Can use the `input` and `label`
* variables.
*
* @param Cake\View\StringTemplate $templates
*/
public function __construct($templates) {
Expand Down Expand Up @@ -135,14 +145,19 @@ protected function _renderInput($val, $text, $data) {
$radio['disabled'] = true;
}

$label = $this->_renderLabel($radio, $data['label'], $escape);

$input = $this->_templates->format('radio', [
'name' => $radio['name'],
'value' => $escape ? h($radio['value']) : $radio['value'],
'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']),
]);

$label = $this->_renderLabel(
$radio,
$data['label'],
$input,
$escape
);

return $this->_templates->format('radioContainer', [
'input' => $input,
'label' => $label,
Expand All @@ -157,10 +172,11 @@ protected function _renderInput($val, $text, $data) {
*
* @param array $radio The input properties.
* @param false|string|array $label The properties for a label.
* @param string $input The input widget.
* @param boolean $escape Whether or not to HTML escape the label.
* @return string Generated label.
*/
protected function _renderLabel($radio, $label, $escape) {
protected function _renderLabel($radio, $label, $input, $escape) {
if (!$label) {
return false;
}
Expand All @@ -169,6 +185,7 @@ protected function _renderLabel($radio, $label, $escape) {

return $this->_templates->format('label', [
'text' => $escape ? h($radio['text']) : $radio['text'],
'input' => $input,
'attrs' => $this->_templates->formatAttributes($labelAttrs),
]);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/View/Input/RadioTest.php
Expand Up @@ -176,6 +176,36 @@ public function testRenderEmptyOption() {
$this->assertTags($result, $expected);
}

/**
* Test rendering the input inside the label.
*
* @return void
*/
public function testRenderInputInsideLabel() {
$this->templates->add([
'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
'radioContainer' => '{{label}}',
]);
$radio = new Radio($this->templates);
$data = [
'name' => 'Crayons[color]',
'options' => ['r' => 'Red'],
];
$result = $radio->render($data);
$expected = [
['label' => ['for' => 'Crayons_color_r']],
['input' => [
'type' => 'radio',
'name' => 'Crayons[color]',
'value' => 'r',
'id' => 'Crayons_color_r'
]],
'Red',
'/label',
];
$this->assertTags($result, $expected);
}

/**
* test render() and selected inputs.
*
Expand Down

0 comments on commit f179736

Please sign in to comment.