I am unable to get my fileupload collection to work. This is what I\'ve done:
https://gist.github.com/manuakasam/1ac71daf7269616f55f0
Looks like the problem is the Collection of Elements. In the past only Fieldsets were supported to be in a Collection. Maybe Element support is added later, but I'm pretty sure it's buggy and therefore not working for you in this way. I reworked your code to be using Fieldsets and I got it validated and the uploaded files properly renamed and moved. The problem was inside the CollectionInputFilter, that one doesn't seem to be correctly updated to support collection of Elements.
Probably obvious: make sure to prepare the collection before validation.
Here's my modified fork of your code: https://gist.github.com/netiul/efaf8bd4545d216fd26c
Controller
public function indexAction()
{
$request = $this->getRequest();
if($request->isPost())
{
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$this->incidentForm->prepare();
$this->incidentForm->setData($post);
if($this->incidentForm->isValid()) {
\Zend\Debug\Debug::dump($this->incidentForm->getData());
die();
}
}
return [
'form' => $this->incidentForm
];
}
TheFilter (not changed, well a small change)
$singleFileFilter = new InputFilter();
$singleFileFilter->add(
[
'name' => 'attachment',
'required' => false,
'filters' => [
[
'name' => 'filerenameupload',
'options' => [
'target' => 'data/incident_attachments/', /* small change here, removed the slash in front of data to make the path relative */
'randomize' => true,
'use_upload_name' => false,
'overwrite' => false
]
]
]
]
);
$attachedFilesFilter = new CollectionInputFilter();
$attachedFilesFilter->setInputFilter($singleFileFilter);
$this->add($attachedFilesFilter, 'attachedFiles');
TheForm
$this->setAttribute('enctype', "multipart/form-data");
$fileElement = new Form\Element\File('attachment');
$fileElement->setOptions(
[
'label' => 'Add Attachment',
'label_attributes' => [
'class' => 'control-label col-sm-3'
]
]
);
$fileElement->setAttributes([
'class' => 'form-control'
]);
$collectionTarget = new Form\Fieldset('uploadItem');
$collectionTarget->add($fileElement);
$this->add(
[
'name' => 'attachedFiles',
'type' => 'collection',
'options' => [
'count' => 3,
'target_element' => $collectionTarget
]
]
);
Output after validation
array (size=1)
'attachedFiles' =>
array (size=3)
0 =>
array (size=1)
'attachment' =>
array (size=5)
'name' => string '1326459.png' (length=11)
'type' => string 'image/png' (length=9)
'tmp_name' => string 'data/incident_attachments_538c30fe0fb2f' (length=39)
'error' => int 0
'size' => int 791802
1 =>
array (size=1)
'attachment' =>
array (size=5)
'name' => string '1510364_10152488321303345_257207784_n.jpg' (length=41)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'data/incident_attachments_538c30fec55c4' (length=39)
'error' => int 0
'size' => int 53272
2 =>
array (size=1)
'attachment' =>
array (size=5)
'name' => string 'IMG_20140430_095013.jpg' (length=23)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'data/incident_attachments_538c30ff4489d' (length=39)
'error' => int 0
'size' => int 1039118