Hello I have the following setup, adjusted to the ZF2FileUpload example on ZF2FileUploadExamples Controller:
public function addAction()
{
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$form = new CreateEventForm($objectManager);
$event = new Event();
$form->bind($event);
if ($this->getRequest()->isPost())
{
// Merge the files
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()
);
$form->setData($post);
if ($form->isValid())
{
return $this->redirectToSuccessPage($form->getData());
}
return new ViewModel(array(
'event' => $event,
'form' => $form
));
}
InputFilter File Filter part:
private function addFile()
{
$name = '/event';
$path = APPLICATION_PATH . '/../data/upload/';
$target = realpath($path) . $name;
// File Input
$input = new FileInput('image');
$input->setRequired(TRUE);
$input->setAllowEmpty(FALSE);
// Filters
$renameUpload = new RenameUpload($target);
$renameUpload->setOverwrite(FALSE);
$renameUpload->setUseUploadExtension(TRUE);
$renameUpload->setUseUploadName(TRUE);
$renameUpload->setRandomize(TRUE);
// File Input
$input->getFilterChain()->attach($renameUpload);
//Validators
$this->add($input);
return $this;
}
in the dump of the success page I see:
File Upload Success
object(Entities\Event)[323]
private 'id' => null
private 'title' => string 'Event10' (length=7)
private 'eventinfo' => string 'Event 10' (length=8)
private 'image' =>
array (size=1)
0 =>
array (size=5)
'name' => string 'test.jpg' (length=38)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string '/tmp/phpjWIZWS' (length=14)
'error' => int 0
'size' => int 257860
private 'eventdate' =>
object(DateTime)[324]
public 'date' => string '2015-04-11 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
private 'webshop' => null
-- Edit -- Wilt you were on the right track, my InputFilter was never called. Now it seems to be called, but I return to the Form and it generates the message No Files Selected
Any suggestions?
I have two questions: