Unable to get the file upload working, the below is my controller action.
public function applyJobAction($id)
{
$form = new ApplyJobForm();
if ($this->request->isPost()) {
if ($form->isValid($this->request->getPost()) == false && $this->request->hasFiles() == true) {
foreach ($form->getMessages() as $message) {
$this->flash->error($message);
}
} else {
$job = Jobs::findFirstById($id);
if (!$job) {
$this->flash->danger('There is no such job');
} else {
$applyJob = new ApplyJobs();
$applyJob->name = $this->request->getPost('name', 'striptags');
$applyJob->email = $this->request->getPost('email', 'email');
$applyJob->message = $this->request->getPost('message', 'striptags');
foreach ($this->request->getUploadedFiles() as $file)
$file->moveTo('files/' . $file->getName());
$applyJob->file_name = $file->getName();
$applyJob->jobId = $job->id;
$applyJob->created = date('Y-m-d');
if ($applyJob->save()) {
$this->flash->success('Success! You have successfully applied for this job');
} else {
foreach ($applyJob->getMessages() as $message) {
$this->flash->error($message);
}
}
}
}
}
$this->view->form = new ApplyJobForm($applyJob, array(
'apply' => true
));
}
In my volt template I am using this to render the file upload element.
{{ form.render("upload") }}
and in my form i have defined the below element like this.
$upload = new File('upload', array(
'placeholder' => 'upload',
));
$upload->addValidators(array(
new PresenceOf(array(
'message' => 'We didn\'t see any attachments Please attach the file and try again'
))
));
$this->add($upload);
If I am dumping $_Files global array I am seeing that my form has sent the file along with request. Please advise.