Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions library/Zend/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
17 changes: 17 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down