Browse Source

Merge pull request #11901 from cakephp/3.next-form-group-types

Allow configuring grouped input types.
Mark Story 8 years ago
parent
commit
98ba1cfeb0
2 changed files with 29 additions and 2 deletions
  1. 13 2
      src/View/Helper/FormHelper.php
  2. 16 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 13 - 2
src/View/Helper/FormHelper.php

@@ -254,6 +254,13 @@ class FormHelper extends Helper
     protected $_valueSources = ['context'];
 
     /**
+     * Grouped input types.
+     *
+     * @var array
+     */
+    protected $_groupedInputTypes = ['radio', 'multicheckbox', 'date', 'time', 'datetime'];
+
+    /**
      * Construct the widgets and binds the default context providers
      *
      * @param \Cake\View\View $View The View this helper is being attached to.
@@ -280,6 +287,11 @@ class FormHelper extends Helper
             unset($config['widgets']);
         }
 
+        if (isset($config['groupedInputTypes'])) {
+            $this->_groupedInputTypes = $config['groupedInputTypes'];
+            unset($config['groupedInputTypes']);
+        }
+
         parent::__construct($View, $config);
 
         if (!$locator) {
@@ -1550,8 +1562,7 @@ class FormHelper extends Helper
         }
 
         $labelAttributes['for'] = $options['id'];
-        $groupTypes = ['radio', 'multicheckbox', 'date', 'time', 'datetime'];
-        if (in_array($options['type'], $groupTypes, true)) {
+        if (in_array($options['type'], $this->_groupedInputTypes, true)) {
             $labelAttributes['for'] = false;
         }
         if ($options['nestedInput']) {

+ 16 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -260,6 +260,22 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * Test overridding grouped input types which controls generation of "for"
+     * attribute of labels.
+     *
+     * @return void
+     */
+    public function testConstructWithGroupedInputTypes()
+    {
+        $helper = new FormHelper($this->View, [
+            'groupedInputTypes' => ['radio'],
+        ]);
+
+        $result = $helper->control('when', ['type' => 'datetime']);
+        $this->assertContains('<label for="when">When</label>', $result);
+    }
+
+    /**
      * Test registering a new widget class and rendering it.
      *
      * @return void