Browse Source

Fix ZF-6624: Improvement for Zend_View_Helper_FormCheckbox -> Make hidden field optional. Patch by Frank Brückner.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24825 44c647ce-9c0f-0410-b52a-842ac1e357ba
rob 13 năm trước cách đây
mục cha
commit
26d9c3953e

+ 4 - 2
documentation/manual/en/module_specs/Zend_View-Helpers.xml

@@ -117,7 +117,8 @@
                     present, '0' is assumed to be the unchecked value, and '1'
                     the checked value. If a $value is passed, but no $options
                     are present, the checked value is assumed to be the value
-                    passed.
+                    passed. The unchecked value is implemented by rendering a
+                    hidden input element before rendering the checkbox.
                 </para>
 
                 <para>
@@ -125,7 +126,8 @@
                     first value is the checked value, and the second the
                     unchecked value; all other values are ignored. You may also
                     pass an associative array with the keys 'checked' and
-                    'unChecked'.
+                    'unChecked'. The key 'disableHidden' can be set to true to
+                    prevent rendering of the hidden field for the unchecked value.
                 </para>
 
                 <para>

+ 8 - 1
library/Zend/View/Helper/FormCheckbox.php

@@ -83,9 +83,16 @@ class Zend_View_Helper_FormCheckbox extends Zend_View_Helper_FormElement
 
         // build the element
         $xhtml = '';
-        if (!$disable && !strstr($name, '[]')) {
+        if ((!$disable && !strstr($name, '[]'))
+            && (empty($attribs['disableHidden']) || !$attribs['disableHidden'])
+        ) {
             $xhtml = $this->_hidden($name, $checkedOptions['uncheckedValue']);
         }
+
+        if (array_key_exists('disableHidden', $attribs)) {
+            unset($attribs['disableHidden']);
+        }
+
         $xhtml .= '<input type="checkbox"'
                 . ' name="' . $this->view->escape($name) . '"'
                 . ' id="' . $this->view->escape($id) . '"'

+ 40 - 0
tests/Zend/View/Helper/FormCheckboxTest.php

@@ -302,6 +302,46 @@ class Zend_View_Helper_FormCheckboxTest extends PHPUnit_Framework_TestCase
         $test = $this->helper->formCheckbox('foo', 0, array(), array(1,0));
         $this->assertNotContains('checked="checked"', $test);
     }
+
+    /**
+     * @group ZF-6624
+     */
+    public function testRenderingWithoutHiddenElement()
+    {
+        $html = $this->helper->formCheckbox(
+            'foo',
+            'bar',
+            array(
+                 'disableHidden' => true,
+            )
+        );
+        $this->assertSame(
+            '<input type="checkbox" name="foo" id="foo" value="bar">',
+            $html
+        );
+
+        $html = $this->helper->formCheckbox(
+            'foo',
+            'bar');
+
+        $this->assertSame(
+            '<input type="hidden" name="foo" value="0"><input type="checkbox" name="foo" id="foo" value="bar">',
+            $html
+        );
+
+        $html = $this->helper->formCheckbox(
+            'foo',
+            'bar',
+            array(
+                 'disableHidden' => false,
+            )
+        );
+
+        $this->assertSame(
+            '<input type="hidden" name="foo" value="0"><input type="checkbox" name="foo" id="foo" value="bar">',
+            $html
+        );
+    }
 }
 
 // Call Zend_View_Helper_FormCheckboxTest::main() if this source file is executed directly.