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
72a81b8e
Commit
72a81b8e
authored
Sep 16, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #3581: Added `yii\validators\CompareValidator::type` to support type…
Fixes #3581: Added `yii\validators\CompareValidator::type` to support type conversion before comparing values
parent
0fc46760
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
4 deletions
+28
-4
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
yii.validation.js
framework/assets/yii.validation.js
+5
-0
CompareValidator.php
framework/validators/CompareValidator.php
+22
-4
No files found.
framework/CHANGELOG.md
View file @
72a81b8e
...
...
@@ -148,6 +148,7 @@ Yii Framework 2 Change Log
-
Enh #3542: Removed requirement to specify
`extensions`
in application config (samdark)
-
Enh #3562: Adding rotateByCopy to yii
\l
og
\F
ileTarget (pawzar)
-
Enh #3574: Add integrity check support for SQLite (zeeke)
-
Enh #3581: Added
`yii\validators\CompareValidator::type`
to support type conversion before comparing values (qiangxue)
-
Enh #3597: Nested array support for HTML5 custom "data-
*
" attributes (armab)
-
Enh #3607: Added support for limit in migrations actions: history, new, redo (Ragazzo)
-
Enh #3631: Added property
`currencyCode`
to
`yii\i18n\Formatter`
(leandrogehlen)
...
...
framework/assets/yii.validation.js
View file @
72a81b8e
...
...
@@ -265,6 +265,11 @@ yii.validation = (function ($) {
}
else
{
compareValue
=
$
(
'#'
+
options
.
compareAttribute
).
val
();
}
if
(
options
.
type
===
'number'
)
{
value
=
parseFloat
(
value
);
compareValue
=
parseFloat
(
compareValue
);
}
switch
(
options
.
operator
)
{
case
'=='
:
valid
=
value
==
compareValue
;
...
...
framework/validators/CompareValidator.php
View file @
72a81b8e
...
...
@@ -45,6 +45,13 @@ class CompareValidator extends Validator
*/
public
$compareValue
;
/**
* @var string the type of the values being compared. The follow types are supported:
*
* - string: the values are being compared as strings. No conversion will be done before comparison.
* - number: the values are being compared as numbers. String values will be converted into numbers before comparison.
*/
public
$type
=
'string'
;
/**
* @var string the operator for comparison. The following operators are supported:
*
* - `==`: check if two values are equal. The comparison is done is non-strict mode.
...
...
@@ -126,7 +133,7 @@ class CompareValidator extends Validator
$compareLabel
=
$object
->
getAttributeLabel
(
$compareAttribute
);
}
if
(
!
$this
->
compareValues
(
$this
->
operator
,
$value
,
$compareValue
))
{
if
(
!
$this
->
compareValues
(
$this
->
operator
,
$
this
->
type
,
$
value
,
$compareValue
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
'compareAttribute'
=>
$compareLabel
,
'compareValue'
=>
$compareValue
,
...
...
@@ -142,7 +149,7 @@ class CompareValidator extends Validator
if
(
$this
->
compareValue
===
null
)
{
throw
new
InvalidConfigException
(
'CompareValidator::compareValue must be set.'
);
}
if
(
!
$this
->
compareValues
(
$this
->
operator
,
$value
,
$this
->
compareValue
))
{
if
(
!
$this
->
compareValues
(
$this
->
operator
,
$
this
->
type
,
$
value
,
$this
->
compareValue
))
{
return
[
$this
->
message
,
[
'compareAttribute'
=>
$this
->
compareValue
,
'compareValue'
=>
$this
->
compareValue
,
...
...
@@ -155,12 +162,20 @@ class CompareValidator extends Validator
/**
* Compares two values with the specified operator.
* @param string $operator the comparison operator
* @param string $type the type of the values being compared
* @param mixed $value the value being compared
* @param mixed $compareValue another value being compared
* @return boolean whether the comparison using the specified operator is true.
*/
protected
function
compareValues
(
$operator
,
$value
,
$compareValue
)
protected
function
compareValues
(
$operator
,
$
type
,
$
value
,
$compareValue
)
{
if
(
$type
===
'number'
)
{
$value
=
floatval
(
$value
);
$compareValue
=
floatval
(
$compareValue
);
}
else
{
$value
=
(
string
)
$value
;
$compareValue
=
(
string
)
$compareValue
;
}
switch
(
$operator
)
{
case
'=='
:
return
$value
==
$compareValue
;
...
...
@@ -188,7 +203,10 @@ class CompareValidator extends Validator
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
$options
=
[
'operator'
=>
$this
->
operator
];
$options
=
[
'operator'
=>
$this
->
operator
,
'type'
=>
$this
->
type
,
];
if
(
$this
->
compareValue
!==
null
)
{
$options
[
'compareValue'
]
=
$this
->
compareValue
;
...
...
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