mirror of
https://github.com/yiisoft/yii2.git
synced 2025-12-16 03:28:56 +08:00
refactored file validator.
This commit is contained in:
@@ -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:
|
||||
@@ -224,6 +224,14 @@ class FileValidator extends Validator
|
||||
return $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function isEmpty($value, $trim = false)
|
||||
{
|
||||
return !$value instanceof UploadedFile || $value->error == UPLOAD_ERR_NO_FILE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts php.ini style size to bytes
|
||||
*
|
||||
|
||||
@@ -144,12 +144,8 @@ 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;
|
||||
}
|
||||
$result = parent::validateValue($file);
|
||||
return empty($result) ? $this->validateImage($file) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user