diff --git a/library/Zend/Form/Form.php b/library/Zend/Form/Form.php index 3ae915b7a26..9be3f978d8f 100644 --- a/library/Zend/Form/Form.php +++ b/library/Zend/Form/Form.php @@ -137,6 +137,12 @@ 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()) { diff --git a/tests/ZendTest/Form/FormTest.php b/tests/ZendTest/Form/FormTest.php index 34f1d1f8104..fc9a4d5f75f 100644 --- a/tests/ZendTest/Form/FormTest.php +++ b/tests/ZendTest/Form/FormTest.php @@ -286,6 +286,23 @@ public function testCanRetrieveDataWithoutErrorsFollowingValidation() $this->assertInternalType('array', $data); } + /** + * @group ZF2-336 + */ + public function testCanAddFileEnctypeAttribute() + { + $this->form->add(array( + 'name' => 'file_resource', + 'attributes' => array( + 'label' => 'This is a file', + 'type' => 'file', + ))); + + $enctype = $this->form->getAttribute('enctype'); + $this->assertNotEmpty($enctype); + $this->assertEquals($enctype, 'multipart/form-data'); + } + public function testCallingGetDataReturnsNormalizedDataByDefault() { $this->populateForm();