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
4f1efc74
Commit
4f1efc74
authored
Jan 21, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
validator cleanup.
parent
78396afb
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
42 additions
and
64 deletions
+42
-64
EmailValidator.php
framework/validators/EmailValidator.php
+5
-4
ExistValidator.php
framework/validators/ExistValidator.php
+6
-6
FilterValidator.php
framework/validators/FilterValidator.php
+3
-2
InlineValidator.php
framework/validators/InlineValidator.php
+3
-0
RangeValidator.php
framework/validators/RangeValidator.php
+5
-4
RegularExpressionValidator.php
framework/validators/RegularExpressionValidator.php
+6
-6
RequiredValidator.php
framework/validators/RequiredValidator.php
+4
-3
SafeValidator.php
framework/validators/SafeValidator.php
+0
-30
UniqueValidator.php
framework/validators/UniqueValidator.php
+1
-2
Validator.php
framework/validators/Validator.php
+1
-0
todo.md
todo.md
+8
-7
No files found.
framework/validators/EmailValidator.php
View file @
4f1efc74
...
...
@@ -29,7 +29,7 @@ class EmailValidator extends Validator
*/
public
$fullPattern
=
'/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/'
;
/**
* @var boolean whether to allow name in the email address (e.g. "
Qiang Xue <qiang.xue@gmail
.com>"). Defaults to false.
* @var boolean whether to allow name in the email address (e.g. "
John Smith <john.smith@example
.com>"). Defaults to false.
* @see fullPattern
*/
public
$allowName
=
false
;
...
...
@@ -78,16 +78,17 @@ class EmailValidator extends Validator
public
function
validateValue
(
$value
)
{
// make sure string length is limited to avoid DOS attacks
$valid
=
is_string
(
$value
)
&&
strlen
(
$value
)
<=
254
&&
(
preg_match
(
$this
->
pattern
,
$value
)
||
$this
->
allowName
&&
preg_match
(
$this
->
fullPattern
,
$value
));
$valid
=
is_string
(
$value
)
&&
strlen
(
$value
)
<=
254
&&
(
preg_match
(
$this
->
pattern
,
$value
)
||
$this
->
allowName
&&
preg_match
(
$this
->
fullPattern
,
$value
));
if
(
$valid
)
{
$domain
=
rtrim
(
substr
(
$value
,
strpos
(
$value
,
'@'
)
+
1
),
'>'
);
}
if
(
$valid
&&
$this
->
checkMX
&&
function_exists
(
'checkdnsrr'
))
{
if
(
$this
->
checkMX
&&
function_exists
(
'checkdnsrr'
))
{
$valid
=
checkdnsrr
(
$domain
,
'MX'
);
}
if
(
$valid
&&
$this
->
checkPort
&&
function_exists
(
'fsockopen'
))
{
$valid
=
fsockopen
(
$domain
,
25
)
!==
false
;
}
}
return
$valid
;
}
...
...
framework/validators/ExistValidator.php
View file @
4f1efc74
...
...
@@ -8,6 +8,7 @@
*/
namespace
yii\validators
;
use
yii\base\InvalidConfigException
;
/**
* ExistValidator validates that the attribute value exists in a table.
...
...
@@ -21,9 +22,9 @@ namespace yii\validators;
class
ExistValidator
extends
Validator
{
/**
* @var string the
yii\db\
ActiveRecord class name or alias of the class
* @var string the ActiveRecord class name or alias of the class
* that should be used to look for the attribute value being validated.
* Defaults to null, meaning using the
yii\db\
ActiveRecord class of
* Defaults to null, meaning using the ActiveRecord class of
* the attribute being validated.
* @see attributeName
*/
...
...
@@ -47,8 +48,7 @@ class ExistValidator extends Validator
*
* @param \yii\db\ActiveRecord $object the object being validated
* @param string $attribute the attribute being validated
*
* @throws \yii\base\Exception if table doesn't have column specified
* @throws InvalidConfigException if table doesn't have column specified
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -62,14 +62,14 @@ class ExistValidator extends Validator
$attributeName
=
(
$this
->
attributeName
===
null
)
?
$attribute
:
$this
->
attributeName
;
$table
=
$className
::
getTableSchema
();
if
((
$column
=
$table
->
getColumn
(
$attributeName
))
===
null
)
{
throw
new
\yii\base\
Exception
(
'Table "'
.
$table
->
name
.
'" does not have a column named "'
.
$attributeName
.
'"'
);
throw
new
InvalidConfig
Exception
(
'Table "'
.
$table
->
name
.
'" does not have a column named "'
.
$attributeName
.
'"'
);
}
$query
=
$className
::
find
();
$query
->
where
(
array
(
$column
->
name
=>
$value
));
if
(
!
$query
->
exists
())
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii'
,
'{attribute} "{value}" is invalid.'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{value}'
=>
$value
)
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
...
...
framework/validators/FilterValidator.php
View file @
4f1efc74
...
...
@@ -8,6 +8,7 @@
*/
namespace
yii\validators
;
use
yii\base\InvalidConfigException
;
/**
* FilterValidator converts the attribute value according to a filter.
...
...
@@ -45,12 +46,12 @@ class FilterValidator extends Validator
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @throws
\yii\base\
Exception if filter property is not a valid callback
* @throws
InvalidConfig
Exception if filter property is not a valid callback
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
if
(
$this
->
filter
===
null
)
{
throw
new
\yii\base\
Exception
(
'The "filter" property must be specified with a valid callback.'
);
throw
new
InvalidConfig
Exception
(
'The "filter" property must be specified with a valid callback.'
);
}
$object
->
$attribute
=
call_user_func
(
$this
->
filter
,
$object
->
$attribute
);
}
...
...
framework/validators/InlineValidator.php
View file @
4f1efc74
...
...
@@ -85,6 +85,8 @@ class InlineValidator extends Validator
if
(
$this
->
clientValidate
!==
null
)
{
$method
=
$this
->
clientValidate
;
return
$object
->
$method
(
$attribute
);
}
else
{
return
null
;
}
}
}
\ No newline at end of file
framework/validators/RangeValidator.php
View file @
4f1efc74
...
...
@@ -8,6 +8,7 @@
*/
namespace
yii\validators
;
use
yii\base\InvalidConfigException
;
/**
* RangeValidator validates that the attribute value is among a list of values.
...
...
@@ -45,7 +46,7 @@ class RangeValidator extends Validator
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @throws
\yii\base\
Exception if the "range" property is not an array
* @throws
InvalidConfig
Exception if the "range" property is not an array
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -54,7 +55,7 @@ class RangeValidator extends Validator
return
;
}
if
(
!
is_array
(
$this
->
range
))
{
throw
new
\yii\base\
Exception
(
'The "range" property must be specified as an array.'
);
throw
new
InvalidConfig
Exception
(
'The "range" property must be specified as an array.'
);
}
if
(
!
$this
->
not
&&
!
in_array
(
$value
,
$this
->
range
,
$this
->
strict
))
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii'
,
'{attribute} should be in the list.'
);
...
...
@@ -70,12 +71,12 @@ class RangeValidator extends Validator
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
* @throws
\yii\base\
Exception if the "range" property is not an array
* @throws
InvalidConfig
Exception if the "range" property is not an array
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
if
(
!
is_array
(
$this
->
range
))
{
throw
new
\yii\base\
Exception
(
'The "range" property must be specified as an array.'
);
throw
new
InvalidConfig
Exception
(
'The "range" property must be specified as an array.'
);
}
if
((
$message
=
$this
->
message
)
===
null
)
{
...
...
framework/validators/RegularExpressionValidator.php
View file @
4f1efc74
...
...
@@ -77,13 +77,13 @@ class RegularExpressionValidator extends Validator
$pattern
=
$this
->
pattern
;
$pattern
=
preg_replace
(
'/\\\\x\{?([0-9a-fA-F]+)\}?/'
,
'\u$1'
,
$pattern
);
$delim
=
substr
(
$pattern
,
0
,
1
);
$
endpos
=
strrpos
(
$pattern
,
$delim
,
1
);
$flag
=
substr
(
$pattern
,
$
end
pos
+
1
);
if
(
$delim
!==
'/'
)
{
$pattern
=
'/'
.
str_replace
(
'/'
,
'\\/'
,
substr
(
$pattern
,
1
,
$
end
pos
-
1
))
.
'/'
;
$delim
inator
=
substr
(
$pattern
,
0
,
1
);
$
pos
=
strrpos
(
$pattern
,
$deliminator
,
1
);
$flag
=
substr
(
$pattern
,
$pos
+
1
);
if
(
$delim
inator
!==
'/'
)
{
$pattern
=
'/'
.
str_replace
(
'/'
,
'\\/'
,
substr
(
$pattern
,
1
,
$pos
-
1
))
.
'/'
;
}
else
{
$pattern
=
substr
(
$pattern
,
0
,
$
end
pos
+
1
);
$pattern
=
substr
(
$pattern
,
0
,
$pos
+
1
);
}
if
(
!
empty
(
$flag
))
{
$pattern
.=
preg_replace
(
'/[^igm]/'
,
''
,
$flag
);
...
...
framework/validators/RequiredValidator.php
View file @
4f1efc74
...
...
@@ -52,9 +52,10 @@ class RequiredValidator extends Validator
}
}
else
{
if
(
!
$this
->
strict
&&
$value
!=
$this
->
requiredValue
||
$this
->
strict
&&
$value
!==
$this
->
requiredValue
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii'
,
'{attribute} must be "{requiredValue}".'
,
array
(
'{requiredValue}'
=>
$this
->
requiredValue
));
$this
->
addError
(
$object
,
$attribute
,
$message
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii'
,
'{attribute} must be "{requiredValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{requiredValue}'
=>
$this
->
requiredValue
,
));
}
}
}
...
...
framework/validators/SafeValidator.php
deleted
100644 → 0
View file @
78396afb
<?php
/**
* SafeValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\validators
;
/**
* SafeValidator marks the associated attributes to be safe for massive assignments.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
SafeValidator
extends
Validator
{
/**
* Validates the attribute of 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 string $attribute the attribute being validated
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
}
}
framework/validators/UniqueValidator.php
View file @
4f1efc74
...
...
@@ -64,8 +64,7 @@ class UniqueValidator extends Validator
$query
->
where
(
array
(
$column
->
name
=>
$value
));
if
(
$object
->
getIsNewRecord
())
{
// if current $object isn't in the database yet then it's OK just
// to call exists()
// if current $object isn't in the database yet then it's OK just to call exists()
$exists
=
$query
->
exists
();
}
else
{
// if current $object is in the database already we can't use exists()
...
...
framework/validators/Validator.php
View file @
4f1efc74
...
...
@@ -195,6 +195,7 @@ abstract class Validator extends Component
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
return
null
;
}
/**
...
...
todo.md
View file @
4f1efc74
...
...
@@ -6,10 +6,16 @@
*
key-value-based (should allow storage-specific methods additionally to generic ones)
*
redis (put it under framework/db/redis or perhaps framework/caching?)
-
logging
*
WebTarget
*
ProfileTarget
*
WebTarget
(TBD after web is in place): should consider using javascript and make it into a toolbar
*
ProfileTarget
(TBD after web is in place): should consider using javascript and make it into a toolbar
-
caching
*
a console command to clear cached data
-
validators
*
FileValidator: depends on CUploadedFile
*
CaptchaValidator: depends on CaptchaAction
*
type conversion rules
*
CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
*
DateValidator: TBD
---
...
...
@@ -18,11 +24,6 @@
-
Module should be able to define its own configuration including routes. Application should be able to overwrite it.
*
application
*
security
-
validators
*
type conversion rules
*
CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
*
FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
*
when getting errors from getErrors it will be good to have which validator (at least type) failed exactly.
-
built-in console commands
+
api doc builder
*
support for markdown syntax
...
...
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