Procházet zdrojové kódy

ZF-4191
Zend_View
Zend_View_Helper_FormRadio generates improper id attributes for certain values


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24059 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan před 14 roky
rodič
revize
9288eb723d

+ 7 - 2
library/Zend/View/Helper/FormRadio.php

@@ -129,9 +129,14 @@ class Zend_View_Helper_FormRadio extends Zend_View_Helper_FormElement
             $endTag= '>';
         }
 
+        // Set up the filter - Alnum + hyphen + underscore
+        require_once 'Zend/Filter/PregReplace.php';
+        $pattern = @preg_match('/\pL/u', 'a') 
+            ? '/[^\p{L}\p{N}\-\_]/u'    // Unicode
+            : '/[^a-zA-Z0-9\-\_]/';     // No Unicode
+        $filter = new Zend_Filter_PregReplace($pattern, "");
+        
         // add radio buttons to the list.
-        require_once 'Zend/Filter/Alnum.php';
-        $filter = new Zend_Filter_Alnum();
         foreach ($options as $opt_value => $opt_label) {
 
             // Should the label be escaped?

+ 26 - 0
tests/Zend/View/Helper/FormRadioTest.php

@@ -403,6 +403,32 @@ class Zend_View_Helper_FormRadioTest extends PHPUnit_Framework_TestCase
             $this->assertRegexp('/<label([^>]*)(for="' . $id . '")/', $html);
         }
     }
+    
+    /**
+     * @group ZF-4191
+     */
+    public function testDashesShouldNotBeFilteredFromId()
+    {
+        $name = "Foo";
+        $options = array(
+            -1 => 'Test -1',
+             0 => 'Test 0',
+             1 => 'Test 1'
+        );
+        
+        $formRadio = new Zend_View_Helper_FormRadio();
+        $formRadio->setView(new Zend_View());
+        $html = $formRadio->formRadio($name, -1, null, $options);
+        foreach ( $options as $key=>$value ) {
+            $fid = "{$name}-{$key}";
+            $this->assertRegExp('/<label([^>]*)(for="'.$fid.'")/', $html);
+            $this->assertRegExp('/<input([^>]*)(id="'.$fid.'")/', $html);
+        }
+        
+        // Assert that radio for value -1 is the selected one
+        $this->assertRegExp('/<input([^>]*)(id="'.$name.'--1")([^>]*)(checked="checked")/', $html);
+    }
+    
 }
 
 // Call Zend_View_Helper_FormRadioTest::main() if this source file is executed directly.