Commit b64f6185 by Qiang Xue

refactored file validator.

parent 05e4fb0b
...@@ -167,7 +167,7 @@ class FileValidator extends Validator ...@@ -167,7 +167,7 @@ class FileValidator extends Validator
protected function validateValue($file) protected function validateValue($file)
{ {
if (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE) { if (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE) {
return $this->skipOnEmpty ? null : [$this->uploadRequired, []]; return [$this->uploadRequired, []];
} }
switch ($file->error) { switch ($file->error) {
case UPLOAD_ERR_OK: case UPLOAD_ERR_OK:
...@@ -225,6 +225,14 @@ class FileValidator extends Validator ...@@ -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 * Converts php.ini style size to bytes
* *
* @param string $sizeStr $sizeStr * @param string $sizeStr $sizeStr
......
...@@ -144,12 +144,8 @@ class ImageValidator extends FileValidator ...@@ -144,12 +144,8 @@ class ImageValidator extends FileValidator
*/ */
protected function validateValue($file) protected function validateValue($file)
{ {
if ($this->skipOnEmpty && (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE)) { $result = parent::validateValue($file);
return null; return empty($result) ? $this->validateImage($file) : $result;
} else {
$result = parent::validateValue($file);
return empty($result) ? $this->validateImage($file) : $result;
}
} }
/** /**
......
...@@ -170,8 +170,8 @@ class FileValidatorTest extends TestCase ...@@ -170,8 +170,8 @@ class FileValidatorTest extends TestCase
$val->validateAttribute($m, 'attr_files'); $val->validateAttribute($m, 'attr_files');
$this->assertFalse($m->hasErrors()); $this->assertFalse($m->hasErrors());
$val->validateAttribute($m, 'attr_files_empty'); $val->validateAttribute($m, 'attr_files_empty');
$this->assertFalse($m->hasErrors('attr_files_empty')); $this->assertTrue($m->hasErrors('attr_files_empty'));
$m = $this->createModelForAttributeTest(); $this->assertSame($val->uploadRequired, current($m->getErrors('attr_files_empty')));
// single File with skipOnEmpty=false // single File with skipOnEmpty=false
$val = new FileValidator(['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