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

File Validator Not Working

My form validation doesn't appear to be working. If I add an image, and submit the form, the validator complains that an image hasn't been uploaded. But, when I var_dump() the $_FILES variable, the file is there. And $this->request->getUploadedFiles() shows there's a file.

It's the validator that isn't working I believe.

Here is the code within the controller that validates the form ...

 $form = new CreateSnapForm();

/*
 * Check form inputs
 */
 if(!$form->isValid($this->request->getPost()))
 {
     $this->session->set('old_data', $_POST);
     $this->flashSession->error($form->getMessages()[0]);
     return $this->response->redirect('/');
 }

Here is the validator

/*
 * Image upload
 */
 $image = new File('image');
 $image->addValidator(new FileValidator([
        'maxSize' => '2M',
        'messageSize' => 'Your image is too big. Max file size: 2MB',
        'allowedTypes' => ['image/jpeg', 'image/png'],
        'messageType' => 'Your image must be a JPEG or PNG file',
        'messageEmpty' => 'No image uploaded'
]));

Any ideas what's going wrong?



51.1k

Can we see your file validator ?

FileValidator is an alias:

use Phalcon\Validation\Validator\File as FileValidator;

You can't fix it because .

$this->request->getPost() is post data .

$form->isValid($this->request->getPost()) only valid with data is $_POST

You must validate

  $validation = new \Phalcon\Validation();
  $file = new \Phalcon\Validation\Validator\File(array(
        'maxSize' => '0.1B',
        'messageSize' => 'Your image is too big. Max file size: 2MB',
        'allowedTypes' => array('image/jpeg', 'image/png'),
        'messageType' => 'Your image must be a JPEG or PNG file',
        'messageEmpty' => 'No image uploaded'
  ));
  $validation->add('image',$file);

  $messages = $validation->validate($_FILES);
  if (count($messages)) {
      foreach ($messages as $message) {
      echo $message, '<br>';
    }
  }
edited Jun '15

So then how do you validate$_POST and $_FILE? As they're both in the form. I have already tried:

$validation->validate($_POST, $_FILES);

Do I have to call validate() twice, once with $_FILES and once with $_POST? That seems a bit odd to have to do that.

And why use validate() over isValid()?

edited Jun '15

In forms

$image = new \Phalcon\Forms\Element\File('image');
$this->add($image);

You remove validate on forms

In your controller

Check form validate

 if (!$form->isValid($this->request->getPost()) ) {
    // check validate file
           $validation = new \Phalcon\Validation();
            $file = new \Phalcon\Validation\Validator\File(array(
                'maxSize' => '0.1B',
                'messageSize' => 'Your image is too big. Max file size: 2MB',
                'allowedTypes' => array('image/jpeg', 'image/png'),
                'messageType' => 'Your image must be a JPEG or PNG file',
                'messageEmpty' => 'No image uploaded'
            ));
            $validation->add('image',$file);
            $messages = $validation->validate($_FILES);
            if (count($messages)) { 
                // Fail show messge 
                foreach ($messages as $message) {
                    echo $message, '<br>';   
                }
            } else {
                    // OK
            }
 }