Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b64f6185
Commit
b64f6185
authored
Feb 01, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored file validator.
parent
05e4fb0b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
7 deletions
+11
-7
FileValidator.php
framework/validators/FileValidator.php
+9
-1
ImageValidator.php
framework/validators/ImageValidator.php
+0
-4
FileValidatorTest.php
tests/unit/framework/validators/FileValidatorTest.php
+2
-2
No files found.
framework/validators/FileValidator.php
View file @
b64f6185
...
...
@@ -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
...
...
framework/validators/ImageValidator.php
View file @
b64f6185
...
...
@@ -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.
...
...
tests/unit/framework/validators/FileValidatorTest.php
View file @
b64f6185
...
...
@@ -170,8 +170,8 @@ class FileValidatorTest extends TestCase
$val
->
validateAttribute
(
$m
,
'attr_files'
);
$this
->
assertFalse
(
$m
->
hasErrors
());
$val
->
validateAttribute
(
$m
,
'attr_files_empty'
);
$this
->
assert
Fals
e
(
$m
->
hasErrors
(
'attr_files_empty'
));
$
m
=
$this
->
createModelForAttributeTest
(
);
$this
->
assert
Tru
e
(
$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
]);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment