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 strange behavior

I am using "Validation\Validator\File" to validate an uploaded file:

    $validator->add(
            'image',
            new Validation\Validator\File([
                'maxSize' => $this->getValidationConfig()->maxSize,
                'allowedTypes' => $this->getValidationConfig()->allowedTypes,
                'maxResolution' => $this->getValidationConfig()->maxResolution,
                'messageSize' => ':field exceeds ' . DigitalUnitsConverterUtil::bytesToMb(
                    $this->getValidationConfig()->maxSize) . ' Mb',
                'allowEmpty' => false
            ])
        );

The value being validated is:

    $validator->validate([
        'image' => $this->image,
    ])

And $this->image is being set inside method "onConstruct()":

    public function onConstruct()
    {
        $this->image = $this->request->getUploadedFiles('image')[0];
    }

But, always the validator says that value is empty. I used xdebug to check, but the value is not empty and contains "Phalcon\Http\Request\File".

https://i.imgur.com/dKKUy7T.png What is going wrong ?



7.0k
Accepted
answer

I think the error is in fields names, as per file.zep code, it checks for "name, size, type, ...etc", but the "Phalcon\Http\Request\File" does not contain this fields. The value should be set again to be validate, like:

    $valueToBeValidated = [
        'name' => $this->image->getName().
        'type' => $this->image->getType(),
        ......
    ]