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
263e8c7f
Commit
263e8c7f
authored
May 02, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished CaptchaValidator.
parent
08c3f90d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
93 deletions
+114
-93
yii.validation.js
framework/assets/yii.validation.js
+88
-57
CaptchaValidator.php
framework/validators/CaptchaValidator.php
+12
-22
RangeValidator.php
framework/validators/RangeValidator.php
+14
-14
No files found.
framework/assets/yii.validation.js
View file @
263e8c7f
...
@@ -30,53 +30,61 @@ yii.validation = (function ($) {
...
@@ -30,53 +30,61 @@ yii.validation = (function ($) {
valid
||
messages
.
push
(
options
.
message
);
valid
||
messages
.
push
(
options
.
message
);
},
},
compare
:
function
(
value
,
messages
,
options
)
{
boolean
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
return
;
}
}
var
valid
=
!
options
.
strict
&&
(
value
==
options
.
trueValue
||
value
==
options
.
falseValue
)
||
options
.
strict
&&
(
value
===
options
.
trueValue
||
value
===
options
.
falseValue
);
var
compareValue
,
valid
=
true
;
valid
||
messages
.
push
(
options
.
message
);
if
(
options
.
compareAttribute
===
undefined
)
{
},
compareValue
=
options
.
compareValue
;
}
else
{
string
:
function
(
value
,
messages
,
options
)
{
compareValue
=
$
(
'#'
+
options
.
compareAttribute
).
val
();
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
}
}
switch
(
options
.
operator
)
{
case
'=='
:
if
(
typeof
value
!==
'string'
)
{
valid
=
value
==
compareValue
;
messages
.
push
(
options
.
message
);
break
;
return
;
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
);
if
(
options
.
min
!==
undefined
&&
value
.
length
<
options
.
min
)
{
messages
.
push
(
options
.
tooShort
);
}
if
(
options
.
max
!==
undefined
&&
value
.
length
>
options
.
max
)
{
messages
.
push
(
options
.
tooLong
);
}
if
(
options
.
is
!==
undefined
&&
value
.
length
!=
options
.
is
)
{
messages
.
push
(
options
.
is
);
}
},
},
boolean
:
function
(
value
,
messages
,
options
)
{
number
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
return
;
}
}
var
valid
=
!
options
.
strict
&&
(
value
==
options
.
trueValue
||
value
==
options
.
falseValue
)
||
options
.
strict
&&
(
value
===
options
.
trueValue
||
value
===
options
.
falseValue
);
if
(
typeof
value
===
'string'
&&
!
value
.
match
(
options
.
pattern
))
{
messages
.
push
(
options
.
message
);
return
;
}
if
(
options
.
min
!==
undefined
&&
value
<
options
.
min
)
{
messages
.
push
(
options
.
tooSmall
);
}
if
(
options
.
max
!==
undefined
&&
value
>
options
.
max
)
{
messages
.
push
(
options
.
tooBig
);
}
},
range
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
}
var
valid
=
!
options
.
not
&&
$
.
inArray
(
value
,
options
.
range
)
||
options
.
not
&&
!
$
.
inArray
(
value
,
options
.
range
);
valid
||
messages
.
push
(
options
.
message
);
valid
||
messages
.
push
(
options
.
message
);
},
},
...
@@ -115,43 +123,66 @@ yii.validation = (function ($) {
...
@@ -115,43 +123,66 @@ yii.validation = (function ($) {
}
}
},
},
string
:
function
(
value
,
messages
,
options
)
{
captcha
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
return
;
}
}
if
(
typeof
value
!==
'string'
)
{
// CAPTCHA may be updated via AJAX and the updated hash is stored in body data
messages
.
push
(
options
.
message
);
var
hash
=
$
(
'body'
).
data
(
options
.
hashKey
);
return
;
if
(
hash
==
null
)
{
}
hash
=
options
.
hash
;
}
else
{
if
(
options
.
min
!==
undefined
&&
value
.
length
<
options
.
min
)
{
hash
=
hash
[
options
.
caseSensitive
?
0
:
1
];
messages
.
push
(
options
.
tooShort
);
}
}
if
(
options
.
max
!==
undefined
&&
value
.
length
>
options
.
max
)
{
var
v
=
options
.
caseSensitive
?
value
:
value
.
toLowerCase
();
messages
.
push
(
options
.
tooLong
);
for
(
var
i
=
v
.
length
-
1
,
h
=
0
;
i
>=
0
;
--
i
)
{
h
+=
v
.
charCodeAt
(
i
);
}
}
if
(
options
.
is
!==
undefined
&&
value
.
length
!=
options
.
is
)
{
if
(
h
!=
hash
)
{
messages
.
push
(
options
.
is
);
messages
.
push
(
options
.
message
);
}
}
},
},
number
:
function
(
value
,
messages
,
options
)
{
compare
:
function
(
value
,
messages
,
options
)
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
if
(
options
.
skipOnEmpty
&&
isEmpty
(
value
))
{
return
;
return
;
}
}
if
(
typeof
value
===
'string'
&&
!
value
.
match
(
options
.
pattern
))
{
var
compareValue
,
valid
=
true
;
messages
.
push
(
options
.
message
);
if
(
options
.
compareAttribute
===
undefined
)
{
return
;
compareValue
=
options
.
compareValue
;
}
}
else
{
compareValue
=
$
(
'#'
+
options
.
compareAttribute
).
val
();
if
(
options
.
min
!==
undefined
&&
value
<
options
.
min
)
{
messages
.
push
(
options
.
tooSmall
);
}
}
if
(
options
.
max
!==
undefined
&&
value
>
options
.
max
)
{
switch
(
options
.
operator
)
{
messages
.
push
(
options
.
tooBig
);
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
);
}
}
};
};
})(
jQuery
);
})(
jQuery
);
framework/validators/CaptchaValidator.php
View file @
263e8c7f
...
@@ -9,6 +9,7 @@ namespace yii\validators;
...
@@ -9,6 +9,7 @@ namespace yii\validators;
use
Yii
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
use
yii\helpers\Html
;
/**
/**
* CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
* CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
...
@@ -94,33 +95,22 @@ class CaptchaValidator extends Validator
...
@@ -94,33 +95,22 @@ class CaptchaValidator extends Validator
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$captcha
=
$this
->
getCaptchaAction
();
$captcha
=
$this
->
getCaptchaAction
();
$message
=
strtr
(
$this
->
message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
));
$code
=
$captcha
->
getVerifyCode
(
false
);
$code
=
$captcha
->
getVerifyCode
(
false
);
$hash
=
$captcha
->
generateValidationHash
(
$this
->
caseSensitive
?
$code
:
strtolower
(
$code
));
$hash
=
$captcha
->
generateValidationHash
(
$this
->
caseSensitive
?
$code
:
strtolower
(
$code
));
$js
=
"
$options
=
array
(
var hash = $('body').data('
{
$this
->
captchaAction
}
.hash');
'hash'
=>
$hash
,
if (hash == null)
'hashKey'
=>
'yiiCaptcha/'
.
$this
->
captchaAction
,
hash =
$hash
;
'caseSensitive'
=>
$this
->
caseSensitive
,
else
'message'
=>
Html
::
encode
(
strtr
(
$this
->
message
,
array
(
hash = hash["
.
(
$this
->
caseSensitive
?
0
:
1
)
.
"];
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
for(var i=value.length-1, h=0; i >= 0; --i) h+=value."
.
(
$this
->
caseSensitive
?
''
:
'toLowerCase().'
)
.
"charCodeAt(i);
'{value}'
=>
$object
->
$attribute
,
if(h != hash) {
))),
messages.push("
.
json_encode
(
$message
)
.
");
);
}
"
;
if
(
$this
->
skipOnEmpty
)
{
if
(
$this
->
skipOnEmpty
)
{
$js
=
"
$options
[
'skipOnEmpty'
]
=
1
;
if($.trim(value)!='') {
$js
}
"
;
}
}
return
$js
;
return
'yii.validation.captcha(value, messages, '
.
json_encode
(
$options
)
.
');'
;
}
}
}
}
framework/validators/RangeValidator.php
View file @
263e8c7f
...
@@ -9,6 +9,7 @@ namespace yii\validators;
...
@@ -9,6 +9,7 @@ namespace yii\validators;
use
Yii
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
use
yii\helpers\Html
;
/**
/**
* RangeValidator validates that the attribute value is among a list of values.
* RangeValidator validates that the attribute value is among a list of values.
...
@@ -60,9 +61,7 @@ class RangeValidator extends Validator
...
@@ -60,9 +61,7 @@ class RangeValidator extends Validator
public
function
validateAttribute
(
$object
,
$attribute
)
public
function
validateAttribute
(
$object
,
$attribute
)
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
!
$this
->
not
&&
!
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
elseif
(
$this
->
not
&&
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
}
}
}
...
@@ -86,21 +85,22 @@ class RangeValidator extends Validator
...
@@ -86,21 +85,22 @@ class RangeValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$message
=
strtr
(
$this
->
message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
));
$range
=
array
();
$range
=
array
();
foreach
(
$this
->
range
as
$value
)
{
foreach
(
$this
->
range
as
$value
)
{
$range
[]
=
(
string
)
$value
;
$range
[]
=
(
string
)
$value
;
}
}
$range
=
json_encode
(
$range
);
$options
=
array
(
'range'
=>
$range
,
'not'
=>
$this
->
not
,
'message'
=>
Html
::
encode
(
strtr
(
$this
->
message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
))),
);
if
(
$this
->
skipOnEmpty
)
{
$options
[
'skipOnEmpty'
]
=
1
;
}
return
"
return
'yii.validation.range(value, messages, '
.
json_encode
(
$options
)
.
');'
;
if ("
.
(
$this
->
skipOnEmpty
?
"$.trim(value)!='' && "
:
''
)
.
(
$this
->
not
?
"$.inArray(value,
$range
)>=0"
:
"$.inArray(value,
$range
)<0"
)
.
") {
messages.push("
.
json_encode
(
$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