Browse Source

Use label widget in multi-checkbox widget.

mark_story 12 years ago
parent
commit
0fdb8b7c17
2 changed files with 33 additions and 12 deletions
  1. 22 7
      src/View/Input/MultiCheckbox.php
  2. 11 5
      tests/TestCase/View/Input/MultiCheckboxTest.php

+ 22 - 7
src/View/Input/MultiCheckbox.php

@@ -30,12 +30,29 @@ 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 @@ class MultiCheckbox {
 
 		$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,

+ 11 - 5
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 @@ class MultiCheckboxTest extends TestCase {
  * @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 @@ class MultiCheckboxTest extends TestCase {
  * @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 @@ class MultiCheckboxTest extends TestCase {
  * @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 @@ class MultiCheckboxTest extends TestCase {
  * @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 @@ class MultiCheckboxTest extends TestCase {
  * @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' => [