We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Validation File


            $attachment = new FileAttachment('attachment');
                new FileValidator(array(
                    'maxSize' => '2M',
                    'messageSize' => ':field exceeds the max filesize (:max)',
                    'allowedTypes' => array(
                    'messageType' => 'Allowed file types are :types',
                    'allowEmpty' => true


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

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


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


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();
                                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 _
                                } 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.