Question

So far this is what I know to be the way to pass the data from a post into the form.

$form->setData( $this->getRequest()->getPost() );

I thought that this might work

$form
    ->setData( $this->getRequest()->getPost() )
    ->setData( $this->getRequest()->getFiles() );

Which it does not. Looking through the framework source I confirmed that it shouldn't. So I was thinking about merging the file data into post data. Surely this cannot be the desired solution? It's not as if getPost() and getFiles() return easily mergeable arrays, they return Parameter objects.

Please note this is Zend Framework 2 specific.

Was it helpful?

Solution

Have you tried getFileInfo knowing now or paying mind to the fact that your using Zend. Typically on a per file basis $_FILE is an array based on the information of the file being uploaded. Filename, extension, etc.. Zends getFileInfo outputs that information in a similar fashion. Though I haven't played with it in sometime, its worth looking into

Example concept (more for multiple file uploads I know, but works with one, good concept to leave in tact just incase you wanna add a second or more files down the road)

$uploads = new Zend_File_Transfer_Adapter_Http();
$files  = $uploads->getFileInfo();

foreach($files as $file => $fileInfo) {
    if ($uploads->isUploaded($file)) {
        if ($uploads->isValid($file)) {
            if ($uploads->receive($file)) {
                $info = $uploads->getFileInfo($file);
                $tmp  = $info[$file]['tmp_name'];
                $data = file_get_contents($tmp);
                // here $tmp is the location of the uploaded file on the server
                // var_dump($info); to see all the fields you can use
            }
         }
     }
}

OTHER TIPS

After attempting to use Zend's file transfer adapter I went with a workaround in the controller. I think that the setData() in the form class should merge the items into the data instead of replacing them. (IMHO)

protected function getPostedData()
{
    if ( is_null($this->postedData) )
    {
        $this->postedData = array_merge(
            (array) $this->getRequest()->getPost(),
            (array) $this->getRequest()->getFiles()
        );
    }
    return $this->postedData;
}

I am using array_merge:

    $form    = $this->getForm('my_form');
    $request = $this->getRequest();

    if($request->isPost())
    {

        $file    = $this->params()->fromFiles('name_of_file');
        $form->setData(array_merge(
            $request->getPost()->toArray(),
            array('arquivo' => $file['name'])
        ));

        if ($form->isValid()) {
        // now i can validate the form field

I use variable variables like this article explains to create variables and then echo them as the values for each form entry.

example:

// create array of GET/POST variables then convert each variable to a local variable
$fields = array_keys($_REQUEST);
foreach ($fields as $field) {
   $$field = $_REQUEST[$field];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top