ZF-4586: Zend_Form_Element_File does not work without filters

Description

Zend_Form_Element_File does not work without filters.

It dies on 'Fatal error: Unsupported operand types in Zend\File\Transfer\Adapter\Abstract.php on line 796', when no filter is set. It is some kind of array merging problem.

When I add a filter, ->addFilter(new Zend_Filter_File_Rename('XXX', 'XXX')), it works OK

Comments

Is works without filters. Most of the unittests are done without attached filters.

Please give reproducable code so we can investigate the problem in the file element.

Problem is with $form->getValues()


        $form = new Zend_Form();
        $form->setName('upload_form');
        
        $form->setMethod(Zend_Form::METHOD_POST);
        $form->setEnctype('multipart/form-data');
        
        $element = new Zend_Form_Element_File('upload_file');
        $element->setLabel('Upload:')
            ->setDestination('C:\Temp')
            //->addFilter(new Zend_Filter_File_Rename('XXX', 'XXX')) //uncomment to get example working
            ;
        $form->addElement($element);
        
        $form->addElement(new Zend_Form_Element_Submit(array(
            'name' => 'Submit',
        )));
        
        $this->view->form = $form;
        
         if ($this->getRequest()->isPost()) {
            if (!$form->isValid($_POST)) {
                echo "Error";
            } else {
                Zend_Debug::dump($form->getValues()); //Problem is HERE!
            }
        }

Probably fixed with r11936.

It shows "Warning: Invalid argument supplied for foreach() in Zend\Form\Element.php on line 524" now :-)


line 524:
foreach ($this->getFilters() as $filter) {
            $value = $filter->filter($value);
        }

You should probably return some array.

What should getValues() be for a function ? File Elements do not have a value !!!

Calling this method on file elements will return unexpected values.

patch:


Index: Zend/Form/Element/File.php
===================================================================
--- Zend/Form/Element/File.php  (revision 11936)
+++ Zend/Form/Element/File.php  (working copy)
@@ -354,7 +354,10 @@
     public function getFilters()
     {
         $adapter = $this->getTransferAdapter();
-        return $adapter->getFilters($this->getName());
+        $filters = $adapter->getFilters($this->getName());
+       if (!is_array($filters)) {
+           return array();
+       }
     }
 
     /**

Sorry, the right patch:


Index: Zend/Form/Element/File.php
===================================================================
--- Zend/Form/Element/File.php  (revision 11937)
+++ Zend/Form/Element/File.php  (working copy)
@@ -354,7 +354,11 @@
     public function getFilters()
     {
         $adapter = $this->getTransferAdapter();
-        return $adapter->getFilters($this->getName());
+        $filters = $adapter->getFilters($this->getName());
+        if (!is_array($filters)) {
+            $filters = array();
+        }
+        return $filters;
     }
 
     /**

Should now work with r11939

I can confirm that this works for me now! Finally :)

Thanks...

Changing issues in preparation for the 1.7.0 release.