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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
10889474
Commit
10889474
authored
May 22, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3549 from creocoder/range-validator-enh
Improve `in` validator to properly support array attributes.
parents
305e8cbd
ca4a228b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
2 deletions
+38
-2
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
RangeValidator.php
framework/validators/RangeValidator.php
+18
-2
RangeValidatorTest.php
tests/unit/framework/validators/RangeValidatorTest.php
+19
-0
No files found.
framework/CHANGELOG.md
View file @
10889474
...
...
@@ -61,6 +61,7 @@ Yii Framework 2 Change Log
-
Enh: Added support for array attributes in
`exist`
validator (creocoder)
-
Enh: Added support for using path alias with
`FileDependency::fileName`
(qiangxue)
-
Enh: Added param
`hideOnSinglePage`
to
`yii\widgets\LinkPager`
(arturf)
-
Enh: Added support for array attributes in
`in`
validator (creocoder)
-
Chg #2913: RBAC
`DbManager`
is now initialized via migration (samdark)
-
Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue)
-
Chg #3175: InvalidCallException, InvalidParamException, UnknownMethodException are now extended from SPL BadMethodCallException (samdark)
...
...
framework/validators/RangeValidator.php
View file @
10889474
...
...
@@ -35,6 +35,10 @@ class RangeValidator extends Validator
* the attribute value should NOT be among the list of values defined via [[range]].
*/
public
$not
=
false
;
/**
* @var boolean whether to allow array type attribute.
*/
public
$allowArray
=
false
;
/**
* @inheritdoc
...
...
@@ -55,8 +59,20 @@ class RangeValidator extends Validator
*/
protected
function
validateValue
(
$value
)
{
$valid
=
!
$this
->
not
&&
in_array
(
$value
,
$this
->
range
,
$this
->
strict
)
||
$this
->
not
&&
!
in_array
(
$value
,
$this
->
range
,
$this
->
strict
);
if
(
!
$this
->
allowArray
&&
is_array
(
$value
))
{
return
[
$this
->
message
,
[]];
}
$valid
=
false
;
foreach
((
array
)
$value
as
$v
)
{
$valid
=
!
$this
->
not
&&
in_array
(
$v
,
$this
->
range
,
$this
->
strict
)
||
$this
->
not
&&
!
in_array
(
$v
,
$this
->
range
,
$this
->
strict
);
if
(
!
$valid
)
{
break
;
}
}
return
$valid
?
null
:
[
$this
->
message
,
[]];
}
...
...
tests/unit/framework/validators/RangeValidatorTest.php
View file @
10889474
...
...
@@ -41,6 +41,17 @@ class RangeValidatorTest extends TestCase
$this
->
assertTrue
(
$val
->
validate
(
"5"
));
}
public
function
testValidateArrayValue
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
)]);
$val
->
allowArray
=
true
;
$this
->
assertTrue
(
$val
->
validate
([
1
,
2
,
3
,
4
,
5
]));
$this
->
assertTrue
(
$val
->
validate
([
6
,
7
,
8
,
9
,
10
]));
$this
->
assertFalse
(
$val
->
validate
([
0
,
1
,
2
]));
$this
->
assertFalse
(
$val
->
validate
([
10
,
11
,
12
]));
$this
->
assertTrue
(
$val
->
validate
([
"1"
,
"2"
,
"3"
,
4
,
5
,
6
]));
}
public
function
testValidateValueStrict
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
),
'strict'
=>
true
]);
...
...
@@ -52,6 +63,14 @@ class RangeValidatorTest extends TestCase
$this
->
assertFalse
(
$val
->
validate
(
"5.5"
));
}
public
function
testValidateArrayValueStrict
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
),
'strict'
=>
true
]);
$val
->
allowArray
=
true
;
$this
->
assertFalse
(
$val
->
validate
([
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
]));
$this
->
assertFalse
(
$val
->
validate
([
"1"
,
"2"
,
"3"
,
4
,
5
,
6
]));
}
public
function
testValidateValueNot
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
),
'not'
=>
true
]);
...
...
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