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 Validation not Working

I don't know why but file validation is not working, it always return "Field :name must not be empty"

Example code:

<?php
use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\File;

if(!$_POST){
    echo '<form method="POST" enctype="multipart/form-data">
    <input type="file" name="cover" />
    <input type="submit" name="test_button" value="Enviar">
    </form>';
} else {
    $validation = new Validation();
    $validation->add('cover', new File([
        "maxSize"              => "2M",
    ]));

    $validation->add('test_button', new PresenceOf([
        'message' => 'The test button is required',
    ]));
    $messages = $validation->validate($_POST);

    if(file_exists($_FILES['cover']['tmp_name'])) {
        echo 'File exists!!!<br>';
    }

    if (count($messages)) {
        foreach ($messages as $message) {
            echo $message, '<br>';
        }
    }
}

It returns:

File exists!!!
Field cover must not be empty

Im currently using PHP Version 5.6.25 and Phalcon 3.2.4...



642

I had something similiar while working on file validation. My solution was that I had to validate on $_FILES


$messages = $validation->validate($_FILES);

Oh i see my error, this is my solution:

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

This one is a buggy bug present for a long time. We haven't took the time to fix it. So workaound is to validate against $_FILES superglobal, as presented here already.

P.S. Upgrade your PHP version.