Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 ?



2.7k
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(),
        ......
    ]