Skip to content
Permalink
Browse files

Add support for custom labels.

You can both disable and customize all of the labels with additional
properties.
  • Loading branch information...
markstory committed Jan 9, 2014
1 parent 8b53be5 commit 7c2e902316968b56279fdbc423d69a15641c6c12
Showing with 74 additions and 6 deletions.
  1. +14 −5 Cake/View/Input/Radio.php
  2. +60 −1 Test/TestCase/View/Input/RadioTest.php
@@ -91,11 +91,7 @@ public function render($data) {
$radio['disabled'] = true;
}
$labelAttrs = ['for' => $radio['id'], 'escape' => $escape];
$label = $this->_templates->format('label', [
'text' => $escape ? h($radio['text']) : $radio['text'],
'attrs' => $this->_templates->formatAttributes($labelAttrs),
]);
$label = $this->_renderLabel($radio, $data['label'], $escape);
$input = $this->_templates->format('radio', [
'name' => $radio['name'],
@@ -129,4 +125,17 @@ protected function _isDisabled($radio, $disabled) {
return (!is_array($disabled) || in_array((string)$radio['value'], $disabled, !$isNumeric));
}
protected function _renderLabel($radio, $label, $escape) {
if (!$label) {
return false;
}
$labelAttrs = is_array($label) ? $label : [];
$labelAttrs += ['for' => $radio['id'], 'escape' => $escape];
return $this->_templates->format('label', [
'text' => $escape ? h($radio['text']) : $radio['text'],
'attrs' => $this->_templates->formatAttributes($labelAttrs),
]);
}
}
@@ -231,7 +231,66 @@ public function testRenderDisabled() {
* @return void
*/
public function testRenderLabelOptions() {
$this->markTestIncomplete();
$radio = new Radio($this->templates);
$data = [
'name' => 'Versions[ver]',
'options' => [
1 => 'one',
'1x' => 'one x',
'2' => 'two',
],
'label' => false,
];
$result = $radio->render($data);
$expected = [
['input' => [
'id' => 'Versions_ver_1',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1',
]],
['input' => [
'id' => 'Versions_ver_1x',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1x',
]],
];
$this->assertTags($result, $expected);
$data = [
'name' => 'Versions[ver]',
'options' => [
1 => 'one',
'1x' => 'one x',
'2' => 'two',
],
'label' => [
'class' => 'my-class',
]
];
$result = $radio->render($data);
$expected = [
['input' => [
'id' => 'Versions_ver_1',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1',
]],
['label' => ['class' => 'my-class', 'for' => 'Versions_ver_1']],
'one',
'/label',
['input' => [
'id' => 'Versions_ver_1x',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1x',
]],
['label' => ['class' => 'my-class', 'for' => 'Versions_ver_1x']],
'one x',
'/label',
];
$this->assertTags($result, $expected);
}
/**

0 comments on commit 7c2e902

Please sign in to comment.
You can’t perform that action at this time.