Reputation: 6782
I've been having a problem trying to upload a single file with Symfony2.3. When I try to upload the file I get the following error:
FatalErrorException: Error: Call to a member function move() on a non-object in
I have checked $csvFileForm['csvFile']->getData();
and its a string (the name of the file), also $file = $this->getRequest()->files;
has size zero.
Here is the show action:
/**
* Finds and displays a Project entity.
*
* @Route("/{id}", name="console_project_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$csvFileForm = $this->createFormBuilder()
->add('csvFile', 'file')
->getForm();
return array(
'csvFileForm' => $csvFileForm->createView()
);
}
and the form in the template looks like this:
<form method="POST" action="{{ path('console_project_upload_urls', { 'id': entity.id }) }}" >
{{ form_row(csvFileForm.csvFile) }}
{{ form_row(csvFileForm._token) }}
<input type="submit" name="submit" class="submit"/>
</form>
The upload action is this:
/**
* Lists all Project entities.
*
* @Route("/{id}/upload-urls", name="console_project_upload_urls")
* @Method("POST")
*/
public function uploadUrlsAction(Request $request, $id)
{
$csvFileForm = $this->createFormBuilder()
->add('csvFile', 'file')
->getForm();
$request = $this->getRequest();
$csvFileForm->handleRequest($request);
$data = $csvFileForm['csvFile']->getData();
$data->move(".", "something.csv"); // This is where the exception occurs
$file = $this->getRequest()->files; // I have used this to check the size of the array
return $this->redirect(...);
}
Thanks for any help.
Upvotes: 1
Views: 1433
Reputation: 2116
You have to add the enctype to your form tag.
<form method="POST" action="{{ path('console_project_upload_urls', { 'id': entity.id }) }}" {{ form_enctype(form) }}>
...
</form>
Note: This is deprecated as of Symfony2.3 and will be removed in Symfony3. You should use the new form_start() twig helper.
{{ form_start(form, {'method': 'POST', 'action': path('console_project_upload_urls', { 'id': entity.id })}) }}
...
{{ form_end(form) }}
Upvotes: 9