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
41b127b9
Commit
41b127b9
authored
May 02, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished js validation for compare validator.
parent
fe98f60f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
18 deletions
+73
-18
yii.validation.js
framework/assets/yii.validation.js
+41
-0
BooleanValidator.php
framework/validators/BooleanValidator.php
+1
-1
CompareValidator.php
framework/validators/CompareValidator.php
+26
-13
Validator.php
framework/validators/Validator.php
+5
-4
No files found.
framework/assets/yii.validation.js
View file @
41b127b9
...
...
@@ -30,6 +30,47 @@ yii.validation = (function ($) {
valid
||
messages
.
push
(
options
.
message
);
},
compare
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
}
var
compareValue
,
valid
=
true
;
if
(
options
.
compareAttribute
===
undefined
)
{
compareValue
=
options
.
compareValue
;
}
else
{
compareValue
=
$
(
'#'
+
options
.
compareAttribute
).
val
();
}
switch
(
options
.
operator
)
{
case
'=='
:
valid
=
value
==
compareValue
;
break
;
case
'==='
:
valid
=
value
===
compareValue
;
break
;
case
'!='
:
valid
=
value
!=
compareValue
;
break
;
case
'!=='
:
valid
=
value
!==
compareValue
;
break
;
case
'>'
:
valid
=
value
>
compareValue
;
break
;
case
'>='
:
valid
=
value
>=
compareValue
;
break
;
case
'<'
:
valid
=
value
<
compareValue
;
break
;
case
'<='
:
valid
=
value
<=
compareValue
;
break
;
}
valid
||
messages
.
push
(
options
.
message
);
},
boolean
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
...
...
framework/validators/BooleanValidator.php
View file @
41b127b9
...
...
@@ -83,7 +83,7 @@ class BooleanValidator extends Validator
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
$options
[
'message'
]
=
array
(
$options
=
array
(
'trueValue'
=>
$this
->
trueValue
,
'falseValue'
=>
$this
->
falseValue
,
'message'
=>
Html
::
encode
(
strtr
(
$this
->
message
,
array
(
...
...
framework/validators/CompareValidator.php
View file @
41b127b9
...
...
@@ -9,6 +9,7 @@ namespace yii\validators;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\helpers\Html
;
/**
* CompareValidator compares the specified attribute value with another value and validates if they are equal.
...
...
@@ -58,6 +59,15 @@ class CompareValidator extends Validator
* - `<=`: validates to see if the value being validated is less than or equal to the value being compared with.
*/
public
$operator
=
'='
;
/**
* @var string the user-defined error message. It may contain the following placeholders which
* will be replaced accordingly by the validator:
*
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
* - `{compareValue}`: the value or the attribute label to be compared with
*/
public
$message
;
/**
...
...
@@ -172,24 +182,27 @@ class CompareValidator extends Validator
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
$options
=
array
(
'operator'
=>
$this
->
operator
);
if
(
$this
->
compareValue
!==
null
)
{
$
compareLabel
=
$this
->
compareValue
;
$compareValue
=
json_encode
(
$this
->
compareValue
)
;
$
options
[
'compareValue'
]
=
$this
->
compareValue
;
$compareValue
=
$this
->
compareValue
;
}
else
{
$compareAttribute
=
$this
->
compareAttribute
===
null
?
$attribute
.
'_repeat'
:
$this
->
compareAttribute
;
$compareValue
=
"
\$
('#"
.
(
CHtml
::
activeId
(
$object
,
$compareAttribute
))
.
"').val()"
;
$
compareLabel
=
$object
->
getAttributeLabel
(
$compareAttribute
);
$compareValue
=
$object
->
getAttributeLabel
(
$compareAttribute
)
;
$
options
[
'compareAttribute'
]
=
Html
::
getInputId
(
$object
,
$compareAttribute
);
}
$condition
=
"value
{
$this
->
operator
}
$compareValue
"
;
$message
=
strtr
(
$this
->
message
,
array
(
if
(
$this
->
skipOnEmpty
)
{
$options
[
'skipOnEmpty'
]
=
1
;
}
$options
[
'message'
]
=
Html
::
encode
(
strtr
(
$options
[
'message'
],
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{compareValue}'
=>
$compareLabel
,
));
'{value}'
=>
$object
->
$attribute
,
'{compareValue}'
=>
$compareValue
,
)));
return
"
if ("
.
(
$this
->
skipOnEmpty
?
"$.trim(value)!='' && "
:
''
)
.
$condition
.
") {
messages.push("
.
json_encode
(
$message
)
.
");
}
"
;
return
'yii.validation.compare(value, messages, '
.
json_encode
(
$options
)
.
');'
;
}
}
framework/validators/Validator.php
View file @
41b127b9
...
...
@@ -76,10 +76,11 @@ abstract class Validator extends Component
*/
public
$attributes
;
/**
* @var string the user-defined error message. Error message may contain some placeholders
* that will be replaced with the actual values by the validator.
* The `{attribute}` and `{value}` are placeholders supported by all validators.
* They will be replaced with the attribute label and value, respectively.
* @var string the user-defined error message. It may contain the following placeholders which
* will be replaced accordingly by the validator:
*
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
*/
public
$message
;
/**
...
...
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