From 0fdb8b7c17b7af22f45ee159680bc82be415c8dc Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 15 Jan 2014 23:16:13 -0500 Subject: [PATCH] Use label widget in multi-checkbox widget. --- src/View/Input/MultiCheckbox.php | 29 ++++++++++++++----- .../TestCase/View/Input/MultiCheckboxTest.php | 16 ++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/View/Input/MultiCheckbox.php b/src/View/Input/MultiCheckbox.php index 6d0788fb7fb..e5eb6e45bc4 100644 --- a/src/View/Input/MultiCheckbox.php +++ b/src/View/Input/MultiCheckbox.php @@ -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; } /** @@ -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, diff --git a/tests/TestCase/View/Input/MultiCheckboxTest.php b/tests/TestCase/View/Input/MultiCheckboxTest.php index 3451a77ee1c..95231c9fc74 100644 --- a/tests/TestCase/View/Input/MultiCheckboxTest.php +++ b/tests/TestCase/View/Input/MultiCheckboxTest.php @@ -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; @@ -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' => [ @@ -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' => [ @@ -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' => [ @@ -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' => [ @@ -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' => [