Skip to content

Commit

Permalink
Use label widget in multi-checkbox widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 16, 2014
1 parent 7be8648 commit 0fdb8b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
29 changes: 22 additions & 7 deletions src/View/Input/MultiCheckbox.php
Expand Up @@ -29,13 +29,30 @@ class MultiCheckbox {
*/
protected $_templates;

/**
* Label widget instance.
*
* @var Cake\View\Input\Label
*/
protected $_label;

/**
* Render multi-checkbox widget.
*
* This class uses the following templates:
*
* - `checkbox` Renders checkbox input controls. Accepts
* the `name`, `value` and `attrs` variables.
* - `checkboxContainer` Renders the containing div/element for
* a checkbox and its label. Accepts the `input`, and `label`
* variables.
*
* @param Cake\View\StringTemplate $templates
* @param Cake\View\Input\Label $label
*/
public function __construct($templates) {
public function __construct($templates, $label) {
$this->_templates = $templates;
$this->_label = $label;
}

/**
Expand Down Expand Up @@ -130,13 +147,11 @@ protected function _renderInput($checkbox) {

$labelAttrs = [
'for' => $checkbox['id'],
'escape' => $checkbox['escape']
];
$label = $this->_templates->format('label', [
'text' => $checkbox['escape'] ? h($checkbox['text']) : $checkbox['text'],
'escape' => $checkbox['escape'],
'text' => $checkbox['text'],
'input' => $input,
'attrs' => $this->_templates->formatAttributes($labelAttrs)
]);
];
$label = $this->_label->render($labelAttrs);

return $this->_templates->format('checkboxContainer', [
'label' => $label,
Expand Down
16 changes: 11 additions & 5 deletions tests/TestCase/View/Input/MultiCheckboxTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\View\Input;

use Cake\TestSuite\TestCase;
use Cake\View\Input\Label;
use Cake\View\Input\MultiCheckbox;
use Cake\View\StringTemplate;

Expand Down Expand Up @@ -44,7 +45,8 @@ public function setUp() {
* @return void
*/
public function testRenderSimple() {
$input = new MultiCheckbox($this->templates);
$label = new Label($this->templates);
$input = new MultiCheckbox($this->templates, $label);
$data = [
'name' => 'Tags[id]',
'options' => [
Expand Down Expand Up @@ -86,7 +88,8 @@ public function testRenderSimple() {
* @return void
*/
public function testRenderComplex() {
$input = new MultiCheckbox($this->templates);
$label = new Label($this->templates);
$input = new MultiCheckbox($this->templates, $label);
$data = [
'name' => 'Tags[id]',
'options' => [
Expand Down Expand Up @@ -130,7 +133,8 @@ public function testRenderComplex() {
* @return void
*/
public function testRenderEscaping() {
$input = new MultiCheckbox($this->templates);
$label = new Label($this->templates);
$input = new MultiCheckbox($this->templates, $label);
$data = [
'name' => 'Tags[id]',
'options' => [
Expand Down Expand Up @@ -160,7 +164,8 @@ public function testRenderEscaping() {
* @return void
*/
public function testRenderSelected() {
$input = new MultiCheckbox($this->templates);
$label = new Label($this->templates);
$input = new MultiCheckbox($this->templates, $label);
$data = [
'name' => 'Tags[id]',
'options' => [
Expand Down Expand Up @@ -213,7 +218,8 @@ public function testRenderSelected() {
* @return void
*/
public function testRenderDisabled() {
$input = new MultiCheckbox($this->templates);
$label = new Label($this->templates);
$input = new MultiCheckbox($this->templates, $label);
$data = [
'name' => 'Tags[id]',
'options' => [
Expand Down

0 comments on commit 0fdb8b7

Please sign in to comment.