Commit b64f6185 by Qiang Xue

refactored file validator.

parent 05e4fb0b
......@@ -167,7 +167,7 @@ class FileValidator extends Validator
protected function validateValue($file)
{
if (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE) {
return $this->skipOnEmpty ? null : [$this->uploadRequired, []];
return [$this->uploadRequired, []];
}
switch ($file->error) {
case UPLOAD_ERR_OK:
......@@ -225,6 +225,14 @@ class FileValidator extends Validator
}
/**
* @inheritdoc
*/
public function isEmpty($value, $trim = false)
{
return !$value instanceof UploadedFile || $value->error == UPLOAD_ERR_NO_FILE;
}
/**
* Converts php.ini style size to bytes
*
* @param string $sizeStr $sizeStr
......
......@@ -144,13 +144,9 @@ class ImageValidator extends FileValidator
*/
protected function validateValue($file)
{
if ($this->skipOnEmpty && (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE)) {
return null;
} else {
$result = parent::validateValue($file);
return empty($result) ? $this->validateImage($file) : $result;
}
}
/**
* Validates an image file.
......
......@@ -170,8 +170,8 @@ class FileValidatorTest extends TestCase
$val->validateAttribute($m, 'attr_files');
$this->assertFalse($m->hasErrors());
$val->validateAttribute($m, 'attr_files_empty');
$this->assertFalse($m->hasErrors('attr_files_empty'));
$m = $this->createModelForAttributeTest();
$this->assertTrue($m->hasErrors('attr_files_empty'));
$this->assertSame($val->uploadRequired, current($m->getErrors('attr_files_empty')));
// single File with skipOnEmpty=false
$val = new FileValidator(['skipOnEmpty' => false]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment