Skip to content
Permalink
Browse files

Make missing files pass validation when optional flag is used.

  • Loading branch information...
markstory committed Sep 7, 2014
1 parent 3de7d38 commit 4938c4e5f66a86295ec52ec975fd95a58599a81d
Showing with 30 additions and 0 deletions.
  1. +4 −0 src/Validation/Validation.php
  2. +26 −0 tests/TestCase/Validation/ValidationTest.php
@@ -967,6 +967,7 @@ public static function uploadError($check, $allowNoFile = false) {
* - `minSize` - The minimum file size. Defaults to not checking.
* - `maxSize` - The maximum file size. Defaults to not checking.
* - `optional` - Whether or not this file is optional. Defaults to false.
* If true a missing file will pass the validator regardless of other constraints.
*
* @param array $file The uploaded file data from PHP.
* @param array $options An array of options for the validation.
@@ -989,6 +990,9 @@ public static function uploadedFile($file, $options = []) {
if (!static::uploadError($file, $options['optional'])) {
return false;
}
if ($options['optional'] && (int)$file['error'] === UPLOAD_ERR_NO_FILE) {
return true;
}
if (isset($options['minSize']) && !static::fileSize($file, '>=', $options['minSize'])) {
return false;
}
@@ -2421,4 +2421,30 @@ public function testUploadedFileSize() {
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too big');
}
/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileNoFile() {
$file = [
'name' => '',
'tmp_name' => TEST_APP . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_NO_FILE,
'type' => '',
'size' => 0
];
$options = [
'optional' => true,
'minSize' => 500,
'types' => ['image/gif', 'image/png']
];
$this->assertTrue(Validation::uploadedFile($file, $options), 'No file should be ok.');
$options = [
'optional' => false
];
$this->assertFalse(Validation::uploadedFile($file, $options), 'File is required.');
}
}

0 comments on commit 4938c4e

Please sign in to comment.
You can’t perform that action at this time.