Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/2619' into develop
Browse files Browse the repository at this point in the history
Forward port #2619
  • Loading branch information
weierophinney committed Oct 2, 2012
2 parents 760feb2 + 1fb9ca3 commit 90dda04
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
16 changes: 15 additions & 1 deletion library/Zend/Form/Element/File.php
Expand Up @@ -22,6 +22,8 @@
namespace Zend\Form\Element;

use Zend\Form\Element;
use Zend\Form\ElementPrepareAwareInterface;
use Zend\Form\Form;

/**
* @category Zend
Expand All @@ -30,7 +32,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class File extends Element
class File extends Element implements ElementPrepareAwareInterface
{
/**
* Seed attributes
Expand All @@ -40,4 +42,16 @@ class File extends Element
protected $attributes = array(
'type' => 'file',
);

/**
* Prepare the form element (mostly used for rendering purposes)
*
* @param Form $form
* @return mixed
*/
public function prepareElement(Form $form)
{
// Ensure the form is using correct enctype
$form->setAttribute('enctype', 'multipart/form-data');
}
}
6 changes: 0 additions & 6 deletions library/Zend/Form/Form.php
Expand Up @@ -137,12 +137,6 @@ public function add($elementOrFieldset, array $flags = array())
$elementOrFieldset = $factory->create($elementOrFieldset);
}

// check if the element is a file and add enctype attribute if so
$type = $elementOrFieldset->getAttribute('type');
if (isset($type) && $type === 'file') {
$this->attributes['enctype'] = 'multipart/form-data';
}

parent::add($elementOrFieldset, $flags);

if ($elementOrFieldset instanceof Fieldset && $elementOrFieldset->useAsBaseFieldset()) {
Expand Down
37 changes: 31 additions & 6 deletions tests/ZendTest/Form/FormTest.php
Expand Up @@ -310,13 +310,38 @@ public function testCanRetrieveDataWithoutErrorsFollowingValidation()
*/
public function testCanAddFileEnctypeAttribute()
{
$this->form->add(array(
'name' => 'file_resource',
'attributes' => array(
'label' => 'This is a file',
'type' => 'file',
)));
$file = new Element\File('file_resource');
$file
->setOptions(array())
->setLabel('File');
$this->form->add($file);

$this->form->prepare();
$enctype = $this->form->getAttribute('enctype');
$this->assertNotEmpty($enctype);
$this->assertEquals($enctype, 'multipart/form-data');
}

/**
* @group ZF2-336
*/
public function testCanAddFileEnctypeFromCollectionAttribute()
{
$file = new Element\File('file_resource');
$file
->setOptions(array())
->setLabel('File');

$fileCollection = new Element\Collection('collection');
$fileCollection->setOptions(array(
'count' => 2,
'allow_add' => false,
'allow_remove' => false,
'target_element' => $file,
));
$this->form->add($fileCollection);

$this->form->prepare();
$enctype = $this->form->getAttribute('enctype');
$this->assertNotEmpty($enctype);
$this->assertEquals($enctype, 'multipart/form-data');
Expand Down

0 comments on commit 90dda04

Please sign in to comment.