Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Handle validating failed file uploads #15166

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ class Validator implements ValidatorContract
*/
protected $sizeRules = ['Size', 'Between', 'Min', 'Max'];

/**
* The validation rules that work with files.
*
* @var array
*/
protected $fileRules = ['File', 'Image', 'Mimes', 'Mimetypes', 'Min', 'Max', 'Size', 'Between', 'Dimensions'];

/**
* The numeric related validation rules.
*
Expand Down Expand Up @@ -518,6 +525,15 @@ protected function validateAttribute($attribute, $rule)
// that the attribute is required, rules are not run for missing values.
$value = $this->getValue($attribute);

if (
$value instanceof UploadedFile &&
! $value->isValid() &&
$this->hasRule($attribute, array_merge($this->fileRules, $this->implicitRules))
) {
return $this->addFailure($attribute, 'file_uploaded', []);
}


$validatable = $this->isValidatable($rule, $attribute, $value);

$method = "validate{$rule}";
Expand Down Expand Up @@ -762,6 +778,10 @@ protected function shouldStopValidating($attribute)
return $this->messages->has($attribute);
}

if (isset($this->failedRules[$attribute]) && in_array('file_uploaded', array_keys($this->failedRules[$attribute]))) {
return true;
}

// In case the attribute has any rule that indicates that the field is required
// and that rule already failed then we should stop validation at this point
// as now there is no point in calling other rules with this field empty.
Expand Down
42 changes: 41 additions & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,46 @@ public function testRequiredUnless()
$this->assertEquals('The last field is required unless first is in taylor, sven.', $v->messages()->first('last'));
}

public function testFailedFileUploads()
{
$trans = $this->getRealTranslator();

// If file is not successfully uploaded validation should fail with a
// 'file_uploaded' error message instead of the original rule.
$file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile');
$file->shouldReceive('isValid')->andReturn(false);
$file->shouldNotReceive('getSize');
$v = new Validator($trans, [], ['photo' => 'Max:10']);
$v->setFiles(['photo' => $file]);
$this->assertTrue($v->fails());
$this->assertEquals(['validation.file_uploaded'], $v->errors()->get('photo'));

// Even "required" will not run if the file failed to upload.
$file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile');
$file->shouldReceive('isValid')->once()->andReturn(false);
$v = new Validator($trans, [], ['photo' => 'required']);
$v->setFiles(['photo' => $file]);
$this->assertTrue($v->fails());
$this->assertEquals(['validation.file_uploaded'], $v->errors()->get('photo'));

// It should only fail with that rule if a validation rule implies it's
// a file. Otherwise it should fail with the regular rule.
$file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile');
$file->shouldReceive('isValid')->andReturn(false);
$v = new Validator($trans, [], ['photo' => 'string']);
$v->setFiles(['photo' => $file]);
$this->assertTrue($v->fails());
$this->assertEquals(['validation.string'], $v->errors()->get('photo'));

// Validation shouldn't continue if a file failed to upload.
$file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile');
$file->shouldReceive('isValid')->once()->andReturn(false);
$v = new Validator($trans, [], ['photo' => 'file|mimes:pdf|min:10']);
$v->setFiles(['photo' => $file]);
$this->assertTrue($v->fails());
$this->assertEquals(['validation.file_uploaded'], $v->errors()->get('photo'));
}

public function testValidateInArray()
{
$trans = $this->getRealTranslator();
Expand Down Expand Up @@ -1187,7 +1227,7 @@ public function testValidateMax()
$this->assertFalse($v->passes());

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
$file->expects($this->at(0))->method('isValid')->will($this->returnValue(true));
$file->expects($this->any())->method('isValid')->will($this->returnValue(true));
$file->expects($this->at(1))->method('getSize')->will($this->returnValue(3072));
$v = new Validator($trans, [], ['photo' => 'Max:10']);
$v->setFiles(['photo' => $file]);
Expand Down