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
ae6c69fc
Commit
ae6c69fc
authored
Apr 05, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refacotring validators.
parent
86f947e9
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
162 additions
and
77 deletions
+162
-77
BooleanValidator.php
framework/validators/BooleanValidator.php
+14
-5
CaptchaValidator.php
framework/validators/CaptchaValidator.php
+14
-5
CompareValidator.php
framework/validators/CompareValidator.php
+1
-1
DateValidator.php
framework/validators/DateValidator.php
+16
-2
EmailValidator.php
framework/validators/EmailValidator.php
+16
-5
ExistValidator.php
framework/validators/ExistValidator.php
+14
-3
NumberValidator.php
framework/validators/NumberValidator.php
+1
-1
RangeValidator.php
framework/validators/RangeValidator.php
+9
-8
RegularExpressionValidator.php
framework/validators/RegularExpressionValidator.php
+7
-5
RequiredValidator.php
framework/validators/RequiredValidator.php
+17
-13
StringValidator.php
framework/validators/StringValidator.php
+20
-21
UniqueValidator.php
framework/validators/UniqueValidator.php
+16
-3
UrlValidator.php
framework/validators/UrlValidator.php
+17
-5
No files found.
framework/validators/BooleanValidator.php
View file @
ae6c69fc
...
@@ -36,6 +36,17 @@ class BooleanValidator extends Validator
...
@@ -36,6 +36,17 @@ class BooleanValidator extends Validator
public
$strict
=
false
;
public
$strict
=
false
;
/**
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} must be either "{true}" or "{false}".'
);
}
}
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param \yii\base\Model $object the object being validated
...
@@ -45,8 +56,7 @@ class BooleanValidator extends Validator
...
@@ -45,8 +56,7 @@ class BooleanValidator extends Validator
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
if
(
!
$this
->
validateValue
(
$value
))
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii|{attribute} must be either "{true}" or "{false}".'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
array
(
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{true}'
=>
$this
->
trueValue
,
'{true}'
=>
$this
->
trueValue
,
'{false}'
=>
$this
->
falseValue
,
'{false}'
=>
$this
->
falseValue
,
));
));
...
@@ -72,15 +82,14 @@ class BooleanValidator extends Validator
...
@@ -72,15 +82,14 @@ class BooleanValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii|{attribute} must be either "{true}" or "{false}".'
);
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
'{true}'
=>
$this
->
trueValue
,
'{true}'
=>
$this
->
trueValue
,
'{false}'
=>
$this
->
falseValue
,
'{false}'
=>
$this
->
falseValue
,
));
));
return
"
return
"
if("
.
(
$this
->
allow
Empty
?
"$.trim(value)!='' && "
:
''
)
.
"value!="
.
json_encode
(
$this
->
trueValue
)
.
" && value!="
.
json_encode
(
$this
->
falseValue
)
.
") {
if("
.
(
$this
->
skipOn
Empty
?
"$.trim(value)!='' && "
:
''
)
.
"value!="
.
json_encode
(
$this
->
trueValue
)
.
" && value!="
.
json_encode
(
$this
->
falseValue
)
.
") {
messages.push("
.
json_encode
(
$message
)
.
");
messages.push("
.
json_encode
(
$message
)
.
");
}
}
"
;
"
;
...
...
framework/validators/CaptchaValidator.php
View file @
ae6c69fc
...
@@ -31,6 +31,17 @@ class CaptchaValidator extends Validator
...
@@ -31,6 +31,17 @@ class CaptchaValidator extends Validator
/**
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|The verification code is incorrect.'
);
}
}
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param \yii\base\Model $object the object being validated
...
@@ -40,8 +51,7 @@ class CaptchaValidator extends Validator
...
@@ -40,8 +51,7 @@ class CaptchaValidator extends Validator
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
if
(
!
$this
->
validateValue
(
$value
))
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii|The verification code is incorrect.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
}
...
@@ -83,8 +93,7 @@ class CaptchaValidator extends Validator
...
@@ -83,8 +93,7 @@ class CaptchaValidator extends Validator
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$captcha
=
$this
->
getCaptchaAction
();
$captcha
=
$this
->
getCaptchaAction
();
$message
=
$this
->
message
!==
null
?
$this
->
message
:
\Yii
::
t
(
'yii|The verification code is incorrect.'
);
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
));
));
...
@@ -102,7 +111,7 @@ if(h != hash) {
...
@@ -102,7 +111,7 @@ if(h != hash) {
}
}
"
;
"
;
if
(
$this
->
allow
Empty
)
{
if
(
$this
->
skipOn
Empty
)
{
$js
=
"
$js
=
"
if($.trim(value)!='') {
if($.trim(value)!='') {
$js
$js
...
...
framework/validators/CompareValidator.php
View file @
ae6c69fc
...
@@ -223,7 +223,7 @@ class CompareValidator extends Validator
...
@@ -223,7 +223,7 @@ class CompareValidator extends Validator
));
));
return
"
return
"
if ("
.
(
$this
->
allow
Empty
?
"$.trim(value)!='' && "
:
''
)
.
$condition
.
") {
if ("
.
(
$this
->
skipOn
Empty
?
"$.trim(value)!='' && "
:
''
)
.
$condition
.
") {
messages.push("
.
json_encode
(
$message
)
.
");
messages.push("
.
json_encode
(
$message
)
.
");
}
}
"
;
"
;
...
...
framework/validators/DateValidator.php
View file @
ae6c69fc
...
@@ -32,6 +32,17 @@ class DateValidator extends Validator
...
@@ -32,6 +32,17 @@ class DateValidator extends Validator
public
$timestampAttribute
;
public
$timestampAttribute
;
/**
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|The format of {attribute} is invalid.'
);
}
}
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param \yii\base\Model $object the object being validated
...
@@ -40,10 +51,13 @@ class DateValidator extends Validator
...
@@ -40,10 +51,13 @@ class DateValidator extends Validator
public
function
validateAttribute
(
$object
,
$attribute
)
public
function
validateAttribute
(
$object
,
$attribute
)
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
is_array
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
return
;
}
$date
=
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
$date
=
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
if
(
$date
===
false
)
{
if
(
$date
===
false
)
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii|The format of {attribute} is invalid.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
elseif
(
$this
->
timestampAttribute
!==
false
)
{
}
elseif
(
$this
->
timestampAttribute
!==
false
)
{
$object
->
{
$this
->
timestampAttribute
}
=
$date
->
getTimestamp
();
$object
->
{
$this
->
timestampAttribute
}
=
$date
->
getTimestamp
();
}
}
...
...
framework/validators/EmailValidator.php
View file @
ae6c69fc
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
/**
/**
* EmailValidator validates that the attribute value is a valid email address.
* EmailValidator validates that the attribute value is a valid email address.
*
*
...
@@ -44,6 +46,17 @@ class EmailValidator extends Validator
...
@@ -44,6 +46,17 @@ class EmailValidator extends Validator
public
$checkPort
=
false
;
public
$checkPort
=
false
;
/**
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} is not a valid email address.'
);
}
}
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param \yii\base\Model $object the object being validated
...
@@ -53,8 +66,7 @@ class EmailValidator extends Validator
...
@@ -53,8 +66,7 @@ class EmailValidator extends Validator
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
if
(
!
$this
->
validateValue
(
$value
))
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is not a valid email address.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
}
...
@@ -88,8 +100,7 @@ class EmailValidator extends Validator
...
@@ -88,8 +100,7 @@ class EmailValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is not a valid email address.'
);
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
));
));
...
@@ -100,7 +111,7 @@ class EmailValidator extends Validator
...
@@ -100,7 +111,7 @@ class EmailValidator extends Validator
}
}
return
"
return
"
if("
.
(
$this
->
allow
Empty
?
"$.trim(value)!='' && "
:
''
)
.
$condition
.
") {
if("
.
(
$this
->
skipOn
Empty
?
"$.trim(value)!='' && "
:
''
)
.
$condition
.
") {
messages.push("
.
json_encode
(
$message
)
.
");
messages.push("
.
json_encode
(
$message
)
.
");
}
}
"
;
"
;
...
...
framework/validators/ExistValidator.php
View file @
ae6c69fc
...
@@ -37,6 +37,18 @@ class ExistValidator extends Validator
...
@@ -37,6 +37,18 @@ class ExistValidator extends Validator
*/
*/
public
$attributeName
;
public
$attributeName
;
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} is invalid.'
);
}
}
/**
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
...
@@ -49,7 +61,7 @@ class ExistValidator extends Validator
...
@@ -49,7 +61,7 @@ class ExistValidator extends Validator
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
is_array
(
$value
))
{
if
(
is_array
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
Yii
::
t
(
'yii|{attribute} is invalid.'
)
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
return
;
return
;
}
}
...
@@ -59,8 +71,7 @@ class ExistValidator extends Validator
...
@@ -59,8 +71,7 @@ class ExistValidator extends Validator
$query
=
$className
::
find
();
$query
=
$className
::
find
();
$query
->
where
(
array
(
$attributeName
=>
$value
));
$query
->
where
(
array
(
$attributeName
=>
$value
));
if
(
!
$query
->
exists
())
{
if
(
!
$query
->
exists
())
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii|{attribute} "{value}" is invalid.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
}
...
...
framework/validators/NumberValidator.php
View file @
ae6c69fc
...
@@ -152,7 +152,7 @@ if(value>{$this->max}) {
...
@@ -152,7 +152,7 @@ if(value>{$this->max}) {
"
;
"
;
}
}
if
(
$this
->
allow
Empty
)
{
if
(
$this
->
skipOn
Empty
)
{
$js
=
"
$js
=
"
if(jQuery.trim(value)!='') {
if(jQuery.trim(value)!='') {
$js
$js
...
...
framework/validators/RangeValidator.php
View file @
ae6c69fc
...
@@ -6,6 +6,8 @@
...
@@ -6,6 +6,8 @@
*/
*/
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
/**
/**
...
@@ -44,6 +46,9 @@ class RangeValidator extends Validator
...
@@ -44,6 +46,9 @@ class RangeValidator extends Validator
if
(
!
is_array
(
$this
->
range
))
{
if
(
!
is_array
(
$this
->
range
))
{
throw
new
InvalidConfigException
(
'The "range" property must be set.'
);
throw
new
InvalidConfigException
(
'The "range" property must be set.'
);
}
}
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} is invalid.'
);
}
}
}
/**
/**
...
@@ -55,11 +60,10 @@ class RangeValidator extends Validator
...
@@ -55,11 +60,10 @@ class RangeValidator extends Validator
public
function
validateAttribute
(
$object
,
$attribute
)
public
function
validateAttribute
(
$object
,
$attribute
)
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
$message
=
$this
->
message
!==
null
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is invalid.'
);
if
(
!
$this
->
not
&&
!
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
if
(
!
$this
->
not
&&
!
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
$this
->
addError
(
$object
,
$attribute
,
$message
);
$this
->
addError
(
$object
,
$attribute
,
$
this
->
message
);
}
elseif
(
$this
->
not
&&
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
}
elseif
(
$this
->
not
&&
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
$this
->
addError
(
$object
,
$attribute
,
$message
);
$this
->
addError
(
$object
,
$attribute
,
$
this
->
message
);
}
}
}
}
...
@@ -82,10 +86,7 @@ class RangeValidator extends Validator
...
@@ -82,10 +86,7 @@ class RangeValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
if
((
$message
=
$this
->
message
)
===
null
)
{
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
\Yii
::
t
(
'yii|{attribute} is invalid.'
);
}
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
));
));
...
@@ -97,7 +98,7 @@ class RangeValidator extends Validator
...
@@ -97,7 +98,7 @@ class RangeValidator extends Validator
$range
=
json_encode
(
$range
);
$range
=
json_encode
(
$range
);
return
"
return
"
if ("
.
(
$this
->
allow
Empty
?
"$.trim(value)!='' && "
:
''
)
.
(
$this
->
not
?
"$.inArray(value,
$range
)>=0"
:
"$.inArray(value,
$range
)<0"
)
.
") {
if ("
.
(
$this
->
skipOn
Empty
?
"$.trim(value)!='' && "
:
''
)
.
(
$this
->
not
?
"$.inArray(value,
$range
)>=0"
:
"$.inArray(value,
$range
)<0"
)
.
") {
messages.push("
.
json_encode
(
$message
)
.
");
messages.push("
.
json_encode
(
$message
)
.
");
}
}
"
;
"
;
...
...
framework/validators/RegularExpressionValidator.php
View file @
ae6c69fc
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
/**
/**
...
@@ -40,6 +41,9 @@ class RegularExpressionValidator extends Validator
...
@@ -40,6 +41,9 @@ class RegularExpressionValidator extends Validator
if
(
$this
->
pattern
===
null
)
{
if
(
$this
->
pattern
===
null
)
{
throw
new
InvalidConfigException
(
'The "pattern" property must be set.'
);
throw
new
InvalidConfigException
(
'The "pattern" property must be set.'
);
}
}
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} is invalid.'
);
}
}
}
/**
/**
...
@@ -52,8 +56,7 @@ class RegularExpressionValidator extends Validator
...
@@ -52,8 +56,7 @@ class RegularExpressionValidator extends Validator
{
{
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
if
(
!
$this
->
validateValue
(
$value
))
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is invalid.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
}
...
@@ -78,8 +81,7 @@ class RegularExpressionValidator extends Validator
...
@@ -78,8 +81,7 @@ class RegularExpressionValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is invalid.'
);
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
));
));
...
@@ -99,7 +101,7 @@ class RegularExpressionValidator extends Validator
...
@@ -99,7 +101,7 @@ class RegularExpressionValidator extends Validator
}
}
return
"
return
"
if ("
.
(
$this
->
allow
Empty
?
"$.trim(value)!='' && "
:
''
)
.
(
$this
->
not
?
''
:
'!'
)
.
"value.match(
$pattern
)) {
if ("
.
(
$this
->
skipOn
Empty
?
"$.trim(value)!='' && "
:
''
)
.
(
$this
->
not
?
''
:
'!'
)
.
"value.match(
$pattern
)) {
messages.push("
.
json_encode
(
$message
)
.
");
messages.push("
.
json_encode
(
$message
)
.
");
}
}
"
;
"
;
...
...
framework/validators/RequiredValidator.php
View file @
ae6c69fc
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
/**
/**
* RequiredValidator validates that the specified attribute does not have null or empty value.
* RequiredValidator validates that the specified attribute does not have null or empty value.
*
*
...
@@ -39,6 +41,17 @@ class RequiredValidator extends Validator
...
@@ -39,6 +41,17 @@ class RequiredValidator extends Validator
public
$strict
=
false
;
public
$strict
=
false
;
/**
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
$this
->
requiredValue
===
null
?
Yii
::
t
(
'yii|{attribute} is invalid.'
)
:
Yii
::
t
(
'yii|{attribute} must be "{requiredValue}".'
);
}
}
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param \yii\base\Model $object the object being validated
...
@@ -49,13 +62,11 @@ class RequiredValidator extends Validator
...
@@ -49,13 +62,11 @@ class RequiredValidator extends Validator
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
$this
->
requiredValue
===
null
)
{
if
(
$this
->
requiredValue
===
null
)
{
if
(
$this
->
strict
&&
$value
===
null
||
!
$this
->
strict
&&
$this
->
isEmpty
(
$value
,
true
))
{
if
(
$this
->
strict
&&
$value
===
null
||
!
$this
->
strict
&&
$this
->
isEmpty
(
$value
,
true
))
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} cannot be blank.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
else
{
}
else
{
if
(
!
$this
->
strict
&&
$value
!=
$this
->
requiredValue
||
$this
->
strict
&&
$value
!==
$this
->
requiredValue
)
{
if
(
!
$this
->
strict
&&
$value
!=
$this
->
requiredValue
||
$this
->
strict
&&
$value
!==
$this
->
requiredValue
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} must be "{requiredValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
array
(
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{requiredValue}'
=>
$this
->
requiredValue
,
'{requiredValue}'
=>
$this
->
requiredValue
,
));
));
}
}
...
@@ -87,12 +98,8 @@ class RequiredValidator extends Validator
...
@@ -87,12 +98,8 @@ class RequiredValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$message
=
$this
->
message
;
if
(
$this
->
requiredValue
!==
null
)
{
if
(
$this
->
requiredValue
!==
null
)
{
if
(
$message
===
null
)
{
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
\Yii
::
t
(
'yii|{attribute} must be "{requiredValue}".'
);
}
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
'{requiredValue}'
=>
$this
->
requiredValue
,
'{requiredValue}'
=>
$this
->
requiredValue
,
...
@@ -103,10 +110,7 @@ if (value != " . json_encode($this->requiredValue) . ") {
...
@@ -103,10 +110,7 @@ if (value != " . json_encode($this->requiredValue) . ") {
}
}
"
;
"
;
}
else
{
}
else
{
if
(
$message
===
null
)
{
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
\Yii
::
t
(
'yii|{attribute} cannot be blank.'
);
}
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
));
));
...
...
framework/validators/StringValidator.php
View file @
ae6c69fc
...
@@ -63,6 +63,18 @@ class StringValidator extends Validator
...
@@ -63,6 +63,18 @@ class StringValidator extends Validator
if
(
$this
->
encoding
===
null
)
{
if
(
$this
->
encoding
===
null
)
{
$this
->
encoding
=
Yii
::
$app
->
charset
;
$this
->
encoding
=
Yii
::
$app
->
charset
;
}
}
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} must be a string.'
);
}
if
(
$this
->
min
!==
null
&&
$this
->
tooShort
===
null
)
{
$this
->
tooShort
=
Yii
::
t
(
'yii|{attribute} should contain at least {min} characters.'
);
}
if
(
$this
->
max
!==
null
&&
$this
->
tooLong
===
null
)
{
$this
->
tooLong
=
Yii
::
t
(
'yii|{attribute} should contain at most {max} characters.'
);
}
if
(
$this
->
is
!==
null
&&
$this
->
notEqual
===
null
)
{
$this
->
notEqual
=
Yii
::
t
(
'yii|{attribute} should contain {length} characters.'
);
}
}
}
/**
/**
...
@@ -76,24 +88,20 @@ class StringValidator extends Validator
...
@@ -76,24 +88,20 @@ class StringValidator extends Validator
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
(
!
is_string
(
$value
))
{
if
(
!
is_string
(
$value
))
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii|{attribute} must be a string.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
return
;
return
;
}
}
$length
=
mb_strlen
(
$value
,
$this
->
encoding
);
$length
=
mb_strlen
(
$value
,
$this
->
encoding
);
if
(
$this
->
min
!==
null
&&
$length
<
$this
->
min
)
{
if
(
$this
->
min
!==
null
&&
$length
<
$this
->
min
)
{
$message
=
(
$this
->
tooShort
!==
null
)
?
$this
->
tooShort
:
Yii
::
t
(
'yii|{attribute} should contain at least {min} characters.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooShort
,
array
(
'{min}'
=>
$this
->
min
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{min}'
=>
$this
->
min
));
}
}
if
(
$this
->
max
!==
null
&&
$length
>
$this
->
max
)
{
if
(
$this
->
max
!==
null
&&
$length
>
$this
->
max
)
{
$message
=
(
$this
->
tooLong
!==
null
)
?
$this
->
tooLong
:
Yii
::
t
(
'yii|{attribute} should contain at most {max} characters.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooLong
,
array
(
'{max}'
=>
$this
->
max
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{max}'
=>
$this
->
max
));
}
}
if
(
$this
->
is
!==
null
&&
$length
!==
$this
->
is
)
{
if
(
$this
->
is
!==
null
&&
$length
!==
$this
->
is
)
{
$message
=
(
$this
->
notEqual
!==
null
)
?
$this
->
notEqual
:
Yii
::
t
(
'yii|{attribute} should contain {length} characters.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
notEqual
,
array
(
'{length}'
=>
$this
->
is
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{length}'
=>
$this
->
is
));
}
}
}
}
...
@@ -124,28 +132,19 @@ class StringValidator extends Validator
...
@@ -124,28 +132,19 @@ class StringValidator extends Validator
$label
=
$object
->
getAttributeLabel
(
$attribute
);
$label
=
$object
->
getAttributeLabel
(
$attribute
);
$value
=
$object
->
$attribute
;
$value
=
$object
->
$attribute
;
if
((
$notEqual
=
$this
->
notEqual
)
===
null
)
{
$notEqual
=
strtr
(
$this
->
notEqual
,
array
(
$notEqual
=
Yii
::
t
(
'yii|{attribute} should contain {length} characters.'
);
}
$notEqual
=
strtr
(
$notEqual
,
array
(
'{attribute}'
=>
$label
,
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
'{value}'
=>
$value
,
'{length}'
=>
$this
->
is
,
'{length}'
=>
$this
->
is
,
));
));
if
((
$tooShort
=
$this
->
tooShort
)
===
null
)
{
$tooShort
=
strtr
(
$this
->
tooShort
,
array
(
$tooShort
=
Yii
::
t
(
'yii|{attribute} should contain at least {min} characters.'
);
}
$tooShort
=
strtr
(
$tooShort
,
array
(
'{attribute}'
=>
$label
,
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
'{value}'
=>
$value
,
'{min}'
=>
$this
->
min
,
'{min}'
=>
$this
->
min
,
));
));
if
((
$tooLong
=
$this
->
tooLong
)
===
null
)
{
$tooLong
=
strtr
(
$this
->
tooLong
,
array
(
$tooLong
=
Yii
::
t
(
'yii|{attribute} should contain at most {max} characters.'
);
}
$tooLong
=
strtr
(
$tooLong
,
array
(
'{attribute}'
=>
$label
,
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
'{value}'
=>
$value
,
'{max}'
=>
$this
->
max
,
'{max}'
=>
$this
->
max
,
...
@@ -174,7 +173,7 @@ if(value.length!= {$this->is}) {
...
@@ -174,7 +173,7 @@ if(value.length!= {$this->is}) {
"
;
"
;
}
}
if
(
$this
->
allow
Empty
)
{
if
(
$this
->
skipOn
Empty
)
{
$js
=
"
$js
=
"
if($.trim(value)!='') {
if($.trim(value)!='') {
$js
$js
...
...
framework/validators/UniqueValidator.php
View file @
ae6c69fc
...
@@ -6,6 +6,8 @@
...
@@ -6,6 +6,8 @@
*/
*/
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
/**
/**
...
@@ -31,6 +33,17 @@ class UniqueValidator extends Validator
...
@@ -31,6 +33,17 @@ class UniqueValidator extends Validator
public
$attributeName
;
public
$attributeName
;
/**
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} "{value}" has already been taken.'
);
}
}
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
* @param \yii\db\ActiveRecord $object the object being validated
* @param \yii\db\ActiveRecord $object the object being validated
...
@@ -52,7 +65,7 @@ class UniqueValidator extends Validator
...
@@ -52,7 +65,7 @@ class UniqueValidator extends Validator
$table
=
$className
::
getTableSchema
();
$table
=
$className
::
getTableSchema
();
if
((
$column
=
$table
->
getColumn
(
$attributeName
))
===
null
)
{
if
((
$column
=
$table
->
getColumn
(
$attributeName
))
===
null
)
{
throw
new
InvalidConfigException
(
'Table "'
.
$table
->
name
.
'" does not have a column named "'
.
$attributeName
.
'"'
);
throw
new
InvalidConfigException
(
"Table '
{
$table
->
name
}
' does not have a column named '
$attributeName
'."
);
}
}
$query
=
$className
::
find
();
$query
=
$className
::
find
();
...
@@ -81,8 +94,7 @@ class UniqueValidator extends Validator
...
@@ -81,8 +94,7 @@ class UniqueValidator extends Validator
}
}
if
(
$exists
)
{
if
(
$exists
)
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} "{value}" has already been taken.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
}
}
}
\ No newline at end of file
framework/validators/UrlValidator.php
View file @
ae6c69fc
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
/**
/**
* UrlValidator validates that the attribute value is a valid http or https URL.
* UrlValidator validates that the attribute value is a valid http or https URL.
*
*
...
@@ -33,6 +35,18 @@ class UrlValidator extends Validator
...
@@ -33,6 +35,18 @@ class UrlValidator extends Validator
**/
**/
public
$defaultScheme
;
public
$defaultScheme
;
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
message
===
null
)
{
$this
->
message
=
Yii
::
t
(
'yii|{attribute} is not a valid URL.'
);
}
}
/**
/**
* Validates the attribute of the object.
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* If there is any error, the error message is added to the object.
...
@@ -47,8 +61,7 @@ class UrlValidator extends Validator
...
@@ -47,8 +61,7 @@ class UrlValidator extends Validator
$object
->
$attribute
=
$this
->
defaultScheme
.
'://'
.
$value
;
$object
->
$attribute
=
$this
->
defaultScheme
.
'://'
.
$value
;
}
}
}
else
{
}
else
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is not a valid URL.'
);
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
}
...
@@ -87,8 +100,7 @@ class UrlValidator extends Validator
...
@@ -87,8 +100,7 @@ class UrlValidator extends Validator
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|{attribute} is not a valid URL.'
);
$message
=
strtr
(
$this
->
message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{value}'
=>
$object
->
$attribute
,
'{value}'
=>
$object
->
$attribute
,
));
));
...
@@ -113,7 +125,7 @@ $js
...
@@ -113,7 +125,7 @@ $js
"
;
"
;
}
}
if
(
$this
->
allow
Empty
)
{
if
(
$this
->
skipOn
Empty
)
{
$js
=
"
$js
=
"
if($.trim(value)!='') {
if($.trim(value)!='') {
$js
$js
...
...
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