We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Validation File

form.php

            $attachment = new FileAttachment('attachment');
            $attachment->addValidators(array(
                new FileValidator(array(
                    'maxSize' => '2M',
                    'messageSize' => ':field exceeds the max filesize (:max)',
                    'allowedTypes' => array(
                        'application/pdf',
                        'application/msword',
                        'application/vnd.ms-excel',
                        'text/plain',
                        'image/gif',
                        'image/jpg',
                        'image/png',
                        'image/bmp',
                        'image/jpeg',
                        'application/x-compressed',
                        'application/x-zip-compressed',
                        'application/zip',
                        'multipart/x-zip',
                        'application/x-rar-compressed',
                    ),
                    'messageType' => 'Allowed file types are :types',
                    'allowEmpty' => true
                ))
            ));
            $this->add($attachment);

action.php

            $submitRequestForm = new submitRequestForm();
            if (!$submitRequestForm->isValid($_POST)) {
                foreach ($submitRequestForm->getMessages() as $message) {
                    $messages .= '<li>'.$message.'</li>';
                }
                $app->flashSession->error($messages);
                $app->session->set('requestForm', $_POST);
                return $this->response->redirect($this->request->getServer('HTTP_REFERER'));
                exit;
            }

what would a file in a form is not loaded, always answers: "Field attachment must not be empty".

tried different files, always says the same thing

Hi,

In my app for validation we had to do something like this:

if(!$form->isValid(array_merge($this->request->getPost(),$_FILES)))

meaning, you should give your validator the POST and FILES global variables, because in the POST var you won't find the uploaded files.

And after the form has been validated you should proced to upload the file, from the tmp to a folder of your choosing.

Something like this:

if ($this->request->hasFiles(true)) {
                            foreach ($this->request->getUploadedFiles(true) as $file) {
                                $file_name = $token."_".$file->getName();
                                $par=$file->getKey();
                                if ($file->moveTo($this->config->form->uploaddir. $file_name)) {
                                    // ho fatto correttamente l'upload e metto nello stato il nome del file copiato (in un elemento che inizia con _
                                    $_POST["_".$par]=$file_name;
                                } else {
                                    $this->flashSession->message($par,"Errore nell'upload del file");
                                }
                            }
                        }

I'm giving you examples from my app, I got to your post because I'm having problems with File Validation but It's different than yours.

Hope it was helpful.

Auri.