Browse Source

Allow auto generating "id" attribute when generating individual inputs.

Closes #8189
ADmad 10 years ago
parent
commit
5c21808aad
2 changed files with 64 additions and 0 deletions
  1. 5 0
      src/View/Helper/FormHelper.php
  2. 59 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 5 - 0
src/View/Helper/FormHelper.php

@@ -2338,6 +2338,7 @@ class FormHelper extends Helper
      * - `default` - mixed - The value to use if there is no value in the form's context.
      * - `disabled` - mixed - Either a boolean indicating disabled state, or the string in
      *   a numerically indexed value.
+     * - `id` - mixed - If `true` it will be auto generated based on field name.
      *
      * This method will convert a numerically indexed 'disabled' into an associative
      * array value. FormHelper's internals expect associative options.
@@ -2356,6 +2357,10 @@ class FormHelper extends Helper
         }
         $context = $this->_getContext();
 
+        if (isset($options['id']) && $options['id'] === true) {
+            $options['id'] = $this->_domId($field);
+        }
+
         $disabledIndex = array_search('disabled', $options, true);
         if (is_int($disabledIndex)) {
             unset($options[$disabledIndex]);

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

@@ -7442,4 +7442,63 @@ class FormHelperTest extends TestCase
         $this->assertSame($mock, $this->Form->context($mock));
         $this->assertSame($mock, $this->Form->context());
     }
+
+    /**
+     * testAutoDomId
+     *
+     * @return void
+     */
+    public function testAutoDomId()
+    {
+        $result = $this->Form->text('field', ['id' => true]);
+        $expected = [
+            'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
+        ];
+        $this->assertHtml($expected, $result);
+
+        // Ensure id => doesn't cause problem when multiple inputs are generated.
+        $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
+        $expected = [
+            'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
+            ['label' => ['for' => 'field-0']],
+            ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
+            'option A',
+            '/label',
+            ['label' => ['for' => 'field-1']],
+            ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
+            'option B',
+            '/label',
+        ];
+        $this->assertHtml($expected, $result);
+
+        $result = $this->Form->select(
+            'multi_field',
+            ['first', 'second'],
+            ['multiple' => 'checkbox', 'id' => true]
+        );
+        $expected = [
+            'input' => [
+                'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
+            ],
+            ['div' => ['class' => 'checkbox']],
+                ['label' => ['for' => 'multi-field-0']],
+                    ['input' => [
+                        'type' => 'checkbox', 'name' => 'multi_field[]',
+                        'value' => '0', 'id' => 'multi-field-0'
+                    ]],
+                    'first',
+                '/label',
+            '/div',
+            ['div' => ['class' => 'checkbox']],
+                ['label' => ['for' => 'multi-field-1']],
+                    ['input' => [
+                        'type' => 'checkbox', 'name' => 'multi_field[]',
+                        'value' => '1', 'id' => 'multi-field-1'
+                    ]],
+                    'second',
+                '/label',
+            '/div',
+        ];
+        $this->assertHtml($expected, $result);
+    }
 }