Browse Source

Fixes #247 - Add id attribute to file elements

Frank Brückner 12 năm trước cách đây
mục cha
commit
3a97e02a77
2 tập tin đã thay đổi với 58 bổ sung8 xóa
  1. 15 8
      library/Zend/Form/Element/File.php
  2. 43 0
      tests/Zend/Form/Element/FileTest.php

+ 15 - 8
library/Zend/Form/Element/File.php

@@ -79,14 +79,19 @@ class Zend_Form_Element_File extends Zend_Form_Element_Xhtml
             return $this;
         }
 
-        $decorators = $this->getDecorators();
-        if (empty($decorators)) {
-            $this->addDecorator('File')
-                 ->addDecorator('Errors')
-                 ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
-                 ->addDecorator('HtmlTag', array('tag' => 'dd'))
-                 ->addDecorator('Label', array('tag' => 'dt'));
+        parent::loadDefaultDecorators();
+
+        // This element needs the File decorator and not the ViewHelper decorator
+        if (false !== $this->getDecorator('ViewHelper')) {
+            $this->removeDecorator('ViewHelper');
+        }
+        if (false === $this->getDecorator('File')) {
+            // Add File decorator to the beginning
+            $decorators = $this->getDecorators();
+            array_unshift($decorators, 'File');
+            $this->setDecorators($decorators);
         }
+
         return $this;
     }
 
@@ -168,6 +173,7 @@ class Zend_Form_Element_File extends Zend_Form_Element_Xhtml
      *
      * @param  string|Zend_File_Transfer_Adapter_Abstract $adapter
      * @return Zend_Form_Element_File
+     * @throws Zend_Form_Element_Exception
      */
     public function setTransferAdapter($adapter)
     {
@@ -341,7 +347,7 @@ class Zend_Form_Element_File extends Zend_Form_Element_Xhtml
     /**
      * Sets a filter for the class, erasing all previous set; proxy to adapter
      *
-     * @param  string|array $filter Filter to set
+     * @param  array $filters Filters to set
      * @return Zend_Form_Element_File
      */
     public function setFilters(array $filters)
@@ -856,6 +862,7 @@ class Zend_Form_Element_File extends Zend_Form_Element_Xhtml
      *
      * @param  Zend_View_Interface $view
      * @return string
+     * @throws Zend_Form_Element_Exception
      */
     public function render(Zend_View_Interface $view = null)
     {

+ 43 - 0
tests/Zend/Form/Element/FileTest.php

@@ -45,6 +45,14 @@ require_once 'Zend/View.php';
  */
 class Zend_Form_Element_FileTest extends PHPUnit_Framework_TestCase
 {
+    /**
+     * @var Zend_Form_Element_File
+     */
+    protected $element;
+
+    /**
+     * @var bool
+     */
     protected $_errorOccurred = false;
 
     /**
@@ -535,6 +543,41 @@ class Zend_Form_Element_FileTest extends PHPUnit_Framework_TestCase
             $this->element->getValidator('NotEmpty') instanceof Zend_Validate_NotEmpty
         );
     }
+
+    /**
+     * @group GH-247
+     */
+    public function testCallbackFunctionAtHtmlTag()
+    {
+        $this->assertEquals(
+            array(
+                 'callback' => array(
+                     'Zend_Form_Element_File',
+                     'resolveElementId',
+                 ),
+            ),
+            $this->element->getDecorator('HtmlTag')->getOption('id')
+        );
+    }
+
+    /**
+     * @group GH-247
+     */
+    public function testDefaultDecoratorOrder()
+    {
+        $expected = array(
+            'Zend_Form_Decorator_File',
+            'Zend_Form_Decorator_Errors',
+            'Zend_Form_Decorator_Description',
+            'Zend_Form_Decorator_HtmlTag',
+            'Zend_Form_Decorator_Label',
+        );
+
+        $this->assertEquals(
+            $expected,
+            array_keys($this->element->getDecorators())
+        );
+    }
 }
 
 class Zend_Form_Element_FileTest_MockAdapter extends Zend_File_Transfer_Adapter_Abstract