3

I use Slim 3 Framework on my project, and i have a form with 3 inputs : 'file', 'name', 'firstname'.

Assuming that $request has the data inputs of my form, to get the uploaded file, i use this code

$files = $request->getUploadedFiles();
    if (empty($files['file'])) {
        throw new Exception('Invalid image');
    }
$newfile = $files['file'];

Then, to validate my input forms, i use the Respect\Validation Library

$validation = $this->validator->validate($request, [
        'file' => v::file()->mimetype('application/pdf'),
        'name' => v::stringType()->notEmpty()->length(2, 50)->alpha(),
        'firstname' => v::stringType()->notEmpty()->length(2, 50)->alpha()
]);


if($validation->failed() {
     //...
}

The fact is the file validation always fails :

var_dump($request->getParam('file')); //return NULL
var_dump($newfile); //return the following

$newfile content

object(Slim\Http\UploadedFile)#59 (8) { 
    ["file"]=> string(14) "/tmp/php409Uje" 
    ["name":protected]=> string(47) "Myfile.pdf" 
    ["type":protected]=> string(15) "application/pdf" 
    ["size":protected]=> int(1404476) ["error":protected]=> int(0) 
    ["sapi":protected]=> bool(true) ["stream":protected]=> NULL 
    ["moved":protected]=> bool(false) 
}

So now I'm stuck. I really can't figure out how can i tell the validator to validate $newfile MIMETYPE for the 'file' input.

  • according to the documentation (https://github.com/Respect/Validation/blob/1.1/docs/Mimetype.md), this should work: `'file' => v::mimetype('application/pdf')` – spielerds Jun 12 '18 at 19:58
  • Yes of course, I checked the docs before posting, and I tried that, but as I mentionned in my post, `file` returns NULL since i need -i think- to get files with getUploadedFiles(), so the validation will always fail because NULL is not `application/pdf` type –  Jun 12 '18 at 20:33
  • can you show me what is `$this->validator`? or the code, where you intialize it in the `$container`? – spielerds Jun 13 '18 at 06:22
  • `$container['validator'] = function ($container) { return new App\Validation\Validator; };` –  Jun 14 '18 at 20:59
  • I don't think that's the problem, eveything is working well, i'm just stuck on that file validation –  Jun 14 '18 at 21:00

2 Answers2

0

You can validate the mimetype using the file attribute of UploadedFile class.

For example:

$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['file'];

$validationResult = v::file()->mimetype('application/pdf')->validate($uploadedFile->file);
if($validationResult){
    echo "Validation ok";
} else {
    echo "Validation error";
}

Files and other parameters are handled in different ways so you can't access files using the classic getParam($name) method. The bad news about it is that you can't use a single validation step, but you have to use one for all the POST parameters and another one for the files. This means in your specific case that you have to do something like this:

$validation = $this->validator->validate($request, [
        'file' => v::file()->mimetype('application/pdf'),
        'name' => v::stringType()->notEmpty()->length(2, 50)->alpha(),
        'firstname' => v::stringType()->notEmpty()->length(2, 50)->alpha()
]);

$uploadedFiles = $request->getUploadedFiles();

if (empty($uploadedFiles['file'])) {
    throw new Exception('Invalid image');
}

$uploadedFile = $uploadedFiles['file'];

$fileValidationResult = v::file()->mimetype('application/pdf')->validate($uploadedFile->file);

if($validation->failed() || !$fileValidationResult) {
     //...
}
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
0

Maybe too late but you can try

v::objectType()->attribute('file', v::oneOf(
    v::mimetype('application/pdf'),
    v::mimetype('image/png')
))->validate($uploadedFile);

Where $uploadedFile is an instance of \Slim\Http\UploadedFile

You can as many validators as you want in "oneOf"

Tuan
  • 1
  • 1