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
7df4a9bd
Commit
7df4a9bd
authored
Jan 21, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished validator cleanup.
parent
4f1efc74
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
116 additions
and
144 deletions
+116
-144
CompareValidator.php
framework/validators/CompareValidator.php
+49
-41
IntegerValidator.php
framework/validators/IntegerValidator.php
+0
-52
NumberValidator.php
framework/validators/NumberValidator.php
+47
-32
Validator.php
framework/validators/Validator.php
+18
-16
todo.md
todo.md
+2
-3
No files found.
framework/validators/CompareValidator.php
View file @
7df4a9bd
...
@@ -8,6 +8,8 @@
...
@@ -8,6 +8,8 @@
*/
*/
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
use
yii\base\InvalidConfigException
;
/**
/**
* CompareValidator compares the specified attribute value with another value and validates if they are equal.
* CompareValidator compares the specified attribute value with another value and validates if they are equal.
...
@@ -30,11 +32,18 @@ namespace yii\validators;
...
@@ -30,11 +32,18 @@ namespace yii\validators;
class
CompareValidator
extends
Validator
class
CompareValidator
extends
Validator
{
{
/**
/**
* @var string the name of the attribute to be compared with
* @var string the name of the attribute to be compared with. When both this property
* and [[compareValue]] are set, the latter takes precedence. If neither is set,
* it assumes the comparison is against another attribute whose name is formed by
* appending '_repeat' to the attribute being validated. For example, if 'password' is
* being validated, then the attribute to be compared would be 'password_repeat'.
* @see compareValue
*/
*/
public
$compareAttribute
;
public
$compareAttribute
;
/**
/**
* @var string the constant value to be compared with
* @var string the constant value to be compared with. When both this property
* and [[compareAttribute]] are set, this property takes precedence.
* @see compareAttribute
*/
*/
public
$compareValue
;
public
$compareValue
;
/**
/**
...
@@ -50,16 +59,15 @@ class CompareValidator extends Validator
...
@@ -50,16 +59,15 @@ class CompareValidator extends Validator
/**
/**
* @var string the operator for comparison. Defaults to '='.
* @var string the operator for comparison. Defaults to '='.
* The followings are valid operators:
* The followings are valid operators:
* <ul>
*
* <li>'=' or '==': validates to see if the two values are equal. If [[strict]] is true, the comparison
* - `=` or `==`: validates to see if the two values are equal. If [[strict]] is true, the comparison
* will be done in strict mode (i.e. checking value type as well).</li>
* will be done in strict mode (i.e. checking value type as well).
* <li>'!=': validates to see if the two values are NOT equal. If [[strict]] is true, the comparison
* - `!=`: validates to see if the two values are NOT equal. If [[strict]] is true, the comparison
* will be done in strict mode (i.e. checking value type as well).</li>
* will be done in strict mode (i.e. checking value type as well).
* <li>'>': validates to see if the value being validated is greater than the value being compared with.</li>
* - `>`: validates to see if the value being validated is greater than the value being compared with.
* <li>'>=': validates to see if the value being validated is greater than or equal to the value being compared with.</li>
* - `>=`: validates to see if the value being validated is greater than or equal to the value being compared with.
* <li>'<': validates to see if the value being validated is less than the value being compared with.</li>
* - `<`: validates to see if the value being validated is less than the value being compared with.
* <li>'<=': validates to see if the value being validated is less than or equal to the value being compared with.</li>
* - `<=`: validates to see if the value being validated is less than or equal to the value being compared with.
* </ul>
*/
*/
public
$operator
=
'='
;
public
$operator
=
'='
;
...
@@ -68,7 +76,7 @@ class CompareValidator extends Validator
...
@@ -68,7 +76,7 @@ class CompareValidator extends Validator
* 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
* @param string $attribute the attribute being validated
* @param string $attribute the attribute being validated
* @throws
\yii\base\
Exception if CompareValidator::operator is invalid
* @throws
InvalidConfig
Exception if CompareValidator::operator is invalid
*/
*/
public
function
validateAttribute
(
$object
,
$attribute
)
public
function
validateAttribute
(
$object
,
$attribute
)
{
{
...
@@ -77,53 +85,53 @@ class CompareValidator extends Validator
...
@@ -77,53 +85,53 @@ class CompareValidator extends Validator
return
;
return
;
}
}
if
(
$this
->
compareValue
!==
null
)
{
if
(
$this
->
compareValue
!==
null
)
{
$compare
To
=
$compareValue
=
$this
->
compareValue
;
$compare
Label
=
$compareValue
=
$this
->
compareValue
;
}
else
{
}
else
{
$compareAttribute
=
(
$this
->
compareAttribute
===
null
)
?
$attribute
.
'_repeat'
:
$this
->
compareAttribute
;
$compareAttribute
=
$this
->
compareAttribute
===
null
?
$attribute
.
'_repeat'
:
$this
->
compareAttribute
;
$compareValue
=
$object
->
$compareAttribute
;
$compareValue
=
$object
->
$compareAttribute
;
$compare
To
=
$object
->
getAttributeLabel
(
$compareAttribute
);
$compare
Label
=
$object
->
getAttributeLabel
(
$compareAttribute
);
}
}
switch
(
$this
->
operator
)
{
switch
(
$this
->
operator
)
{
case
'='
:
case
'='
:
case
'=='
:
case
'=='
:
if
((
$this
->
strict
&&
$value
!==
$compareValue
)
||
(
!
$this
->
strict
&&
$value
!=
$compareValue
))
{
if
((
$this
->
strict
&&
$value
!==
$compareValue
)
||
(
!
$this
->
strict
&&
$value
!=
$compareValue
))
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\
Yii
::
t
(
'yii'
,
'{attribute} must be repeated exactly.'
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be repeated exactly.'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
To
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
Label
));
}
}
break
;
break
;
case
'!='
:
case
'!='
:
if
((
$this
->
strict
&&
$value
===
$compareValue
)
||
(
!
$this
->
strict
&&
$value
==
$compareValue
))
{
if
((
$this
->
strict
&&
$value
===
$compareValue
)
||
(
!
$this
->
strict
&&
$value
==
$compareValue
))
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\
Yii
::
t
(
'yii'
,
'{attribute} must not be equal to "{compareValue}".'
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must not be equal to "{compareValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
To
,
'{compareValue}'
=>
$compareValue
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
Label
,
'{compareValue}'
=>
$compareValue
));
}
}
break
;
break
;
case
'>'
:
case
'>'
:
if
(
$value
<=
$compareValue
)
{
if
(
$value
<=
$compareValue
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\
Yii
::
t
(
'yii'
,
'{attribute} must be greater than "{compareValue}".'
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be greater than "{compareValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
To
,
'{compareValue}'
=>
$compareValue
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
Label
,
'{compareValue}'
=>
$compareValue
));
}
}
break
;
break
;
case
'>='
:
case
'>='
:
if
(
$value
<
$compareValue
)
{
if
(
$value
<
$compareValue
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\
Yii
::
t
(
'yii'
,
'{attribute} must be greater than or equal to "{compareValue}".'
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be greater than or equal to "{compareValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
To
,
'{compareValue}'
=>
$compareValue
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
Label
,
'{compareValue}'
=>
$compareValue
));
}
}
break
;
break
;
case
'<'
:
case
'<'
:
if
(
$value
>=
$compareValue
)
{
if
(
$value
>=
$compareValue
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\
Yii
::
t
(
'yii'
,
'{attribute} must be less than "{compareValue}".'
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be less than "{compareValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
To
,
'{compareValue}'
=>
$compareValue
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
Label
,
'{compareValue}'
=>
$compareValue
));
}
}
break
;
break
;
case
'<='
:
case
'<='
:
if
(
$value
>
$compareValue
)
{
if
(
$value
>
$compareValue
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\
Yii
::
t
(
'yii'
,
'{attribute} must be less than or equal to "{compareValue}".'
);
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be less than or equal to "{compareValue}".'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
To
,
'{compareValue}'
=>
$compareValue
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{compareAttribute}'
=>
$compare
Label
,
'{compareValue}'
=>
$compareValue
));
}
}
break
;
break
;
default
:
default
:
throw
new
\yii\base\Exception
(
'Invalid operator "'
.
$this
->
operator
.
'".'
);
throw
new
InvalidConfigException
(
"Unknown operator:
{
$this
->
operator
}
"
);
}
}
}
}
...
@@ -132,17 +140,17 @@ class CompareValidator extends Validator
...
@@ -132,17 +140,17 @@ class CompareValidator extends Validator
* @param \yii\base\Model $object the data object being validated
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated
* @param string $attribute the name of the attribute to be validated
* @return string the client-side validation script
* @return string the client-side validation script
* @throws
\yii\base\
Exception if CompareValidator::operator is invalid
* @throws
InvalidConfig
Exception if CompareValidator::operator is invalid
*/
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
if
(
$this
->
compareValue
!==
null
)
{
if
(
$this
->
compareValue
!==
null
)
{
$compare
To
=
$this
->
compareValue
;
$compare
Label
=
$this
->
compareValue
;
$compareValue
=
json_encode
(
$this
->
compareValue
);
$compareValue
=
json_encode
(
$this
->
compareValue
);
}
else
{
}
else
{
$compareAttribute
=
(
$this
->
compareAttribute
===
null
)
?
$attribute
.
'_repeat'
:
$this
->
compareAttribute
;
$compareAttribute
=
$this
->
compareAttribute
===
null
?
$attribute
.
'_repeat'
:
$this
->
compareAttribute
;
$compareValue
=
"
\$
('#"
.
(
CHtml
::
activeId
(
$object
,
$compareAttribute
))
.
"').val()"
;
$compareValue
=
"
\$
('#"
.
(
CHtml
::
activeId
(
$object
,
$compareAttribute
))
.
"').val()"
;
$compare
To
=
$object
->
getAttributeLabel
(
$compareAttribute
);
$compare
Label
=
$object
->
getAttributeLabel
(
$compareAttribute
);
}
}
$message
=
$this
->
message
;
$message
=
$this
->
message
;
...
@@ -150,47 +158,47 @@ class CompareValidator extends Validator
...
@@ -150,47 +158,47 @@ class CompareValidator extends Validator
case
'='
:
case
'='
:
case
'=='
:
case
'=='
:
if
(
$message
===
null
)
{
if
(
$message
===
null
)
{
$message
=
\
Yii
::
t
(
'yii'
,
'{attribute} must be repeated exactly.'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must be repeated exactly.'
);
}
}
$condition
=
'value!='
.
$compareValue
;
$condition
=
'value!='
.
$compareValue
;
break
;
break
;
case
'!='
:
case
'!='
:
if
(
$message
===
null
)
{
if
(
$message
===
null
)
{
$message
=
\
Yii
::
t
(
'yii'
,
'{attribute} must not be equal to "{compareValue}".'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must not be equal to "{compareValue}".'
);
}
}
$condition
=
'value=='
.
$compareValue
;
$condition
=
'value=='
.
$compareValue
;
break
;
break
;
case
'>'
:
case
'>'
:
if
(
$message
===
null
)
{
if
(
$message
===
null
)
{
$message
=
\
Yii
::
t
(
'yii'
,
'{attribute} must be greater than "{compareValue}".'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must be greater than "{compareValue}".'
);
}
}
$condition
=
'value<='
.
$compareValue
;
$condition
=
'value<='
.
$compareValue
;
break
;
break
;
case
'>='
:
case
'>='
:
if
(
$message
===
null
)
{
if
(
$message
===
null
)
{
$message
=
\
Yii
::
t
(
'yii'
,
'{attribute} must be greater than or equal to "{compareValue}".'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must be greater than or equal to "{compareValue}".'
);
}
}
$condition
=
'value<'
.
$compareValue
;
$condition
=
'value<'
.
$compareValue
;
break
;
break
;
case
'<'
:
case
'<'
:
if
(
$message
===
null
)
{
if
(
$message
===
null
)
{
$message
=
\
Yii
::
t
(
'yii'
,
'{attribute} must be less than "{compareValue}".'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must be less than "{compareValue}".'
);
}
}
$condition
=
'value>='
.
$compareValue
;
$condition
=
'value>='
.
$compareValue
;
break
;
break
;
case
'<='
:
case
'<='
:
if
(
$message
===
null
)
{
if
(
$message
===
null
)
{
$message
=
\
Yii
::
t
(
'yii'
,
'{attribute} must be less than or equal to "{compareValue}".'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must be less than or equal to "{compareValue}".'
);
}
}
$condition
=
'value>'
.
$compareValue
;
$condition
=
'value>'
.
$compareValue
;
break
;
break
;
default
:
default
:
throw
new
\yii\base\Exception
(
'Invalid operator "'
.
$this
->
operator
.
'".'
);
throw
new
InvalidConfigException
(
"Unknown operator:
{
$this
->
operator
}
"
);
}
}
$message
=
strtr
(
$message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{attribute}'
=>
$object
->
getAttributeLabel
(
$attribute
),
'{compareValue}'
=>
$compare
To
,
'{compareValue}'
=>
$compare
Label
,
));
));
return
"
return
"
...
...
framework/validators/IntegerValidator.php
deleted
100644 → 0
View file @
4f1efc74
<?php
/**
* IntegerValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\validators
;
/**
* IntegerValidator validates that the attribute value is an integer.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
IntegerValidator
extends
NumberValidator
{
/**
* @var string the regular expression for matching integers.
*/
public
$pattern
=
'/^\s*[+-]?\d+\s*$/'
;
/**
* 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
)
{
if
(
$this
->
message
===
null
)
{
$this
->
message
=
\Yii
::
t
(
'yii'
,
'{attribute} must be an integer.'
);
}
parent
::
validateAttribute
(
$object
,
$attribute
);
}
/**
* Returns the JavaScript needed for performing client-side validation.
* @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.
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
if
(
$this
->
message
===
null
)
{
$this
->
message
=
\Yii
::
t
(
'yii'
,
'{attribute} must be an integer.'
);
}
return
parent
::
clientValidateAttribute
(
$object
,
$attribute
);
}
}
framework/validators/NumberValidator.php
View file @
7df4a9bd
...
@@ -9,6 +9,8 @@
...
@@ -9,6 +9,8 @@
namespace
yii\validators
;
namespace
yii\validators
;
use
Yii
;
/**
/**
* NumberValidator validates that the attribute value is a number.
* NumberValidator validates that the attribute value is a number.
*
*
...
@@ -22,6 +24,10 @@ namespace yii\validators;
...
@@ -22,6 +24,10 @@ namespace yii\validators;
class
NumberValidator
extends
Validator
class
NumberValidator
extends
Validator
{
{
/**
/**
* @var boolean whether the attribute value can only be an integer. Defaults to false.
*/
public
$integerOnly
=
false
;
/**
* @var boolean whether the attribute value can be null or empty. Defaults to true,
* @var boolean whether the attribute value can be null or empty. Defaults to true,
* meaning that if the attribute is empty, it is considered valid.
* meaning that if the attribute is empty, it is considered valid.
*/
*/
...
@@ -43,10 +49,14 @@ class NumberValidator extends Validator
...
@@ -43,10 +49,14 @@ class NumberValidator extends Validator
*/
*/
public
$tooSmall
;
public
$tooSmall
;
/**
/**
* @var string the regular expression for matching integers.
*/
public
$integerPattern
=
'/^\s*[+-]?\d+\s*$/'
;
/**
* @var string the regular expression for matching numbers. It defaults to a pattern
* @var string the regular expression for matching numbers. It defaults to a pattern
* that matches floating numbers with optional exponential part (e.g. -1.23e-10).
* that matches floating numbers with optional exponential part (e.g. -1.23e-10).
*/
*/
public
$
p
attern
=
'/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'
;
public
$
numberP
attern
=
'/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'
;
/**
/**
...
@@ -61,16 +71,23 @@ class NumberValidator extends Validator
...
@@ -61,16 +71,23 @@ class NumberValidator extends Validator
if
(
$this
->
allowEmpty
&&
$this
->
isEmpty
(
$value
))
{
if
(
$this
->
allowEmpty
&&
$this
->
isEmpty
(
$value
))
{
return
;
return
;
}
}
if
(
!
preg_match
(
$this
->
pattern
,
"
$value
"
))
{
if
(
$this
->
integerOnly
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii'
,
'{attribute} must be a number.'
);
if
(
!
preg_match
(
$this
->
integerPattern
,
"
$value
"
))
{
$this
->
addError
(
$object
,
$attribute
,
$message
);
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be an integer.'
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
else
{
if
(
!
preg_match
(
$this
->
numberPattern
,
"
$value
"
))
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii'
,
'{attribute} must be a number.'
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
}
}
if
(
$this
->
min
!==
null
&&
$value
<
$this
->
min
)
{
if
(
$this
->
min
!==
null
&&
$value
<
$this
->
min
)
{
$message
=
(
$this
->
tooSmall
!==
null
)
?
$this
->
tooSmall
:
\
Yii
::
t
(
'yii'
,
'{attribute} is too small (minimum is {min}).'
);
$message
=
$this
->
tooSmall
!==
null
?
$this
->
tooSmall
:
Yii
::
t
(
'yii'
,
'{attribute} is too small (minimum is {min}).'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{min}'
=>
$this
->
min
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{min}'
=>
$this
->
min
));
}
}
if
(
$this
->
max
!==
null
&&
$value
>
$this
->
max
)
{
if
(
$this
->
max
!==
null
&&
$value
>
$this
->
max
)
{
$message
=
(
$this
->
tooBig
!==
null
)
?
$this
->
tooBig
:
\
Yii
::
t
(
'yii'
,
'{attribute} is too big (maximum is {max}).'
);
$message
=
$this
->
tooBig
!==
null
?
$this
->
tooBig
:
Yii
::
t
(
'yii'
,
'{attribute} is too big (maximum is {max}).'
);
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{max}'
=>
$this
->
max
));
$this
->
addError
(
$object
,
$attribute
,
$message
,
array
(
'{max}'
=>
$this
->
max
));
}
}
}
}
...
@@ -84,49 +101,46 @@ class NumberValidator extends Validator
...
@@ -84,49 +101,46 @@ class NumberValidator extends Validator
public
function
clientValidateAttribute
(
$object
,
$attribute
)
public
function
clientValidateAttribute
(
$object
,
$attribute
)
{
{
$label
=
$object
->
getAttributeLabel
(
$attribute
);
$label
=
$object
->
getAttributeLabel
(
$attribute
);
$value
=
$object
->
$attribute
;
if
((
$message
=
$this
->
message
)
===
null
)
{
if
((
$message
=
$this
->
message
)
===
null
)
{
$message
=
\Yii
::
t
(
'yii'
,
'{attribute} must be a number.'
);
$message
=
$this
->
integerOnly
?
Yii
::
t
(
'yii'
,
'{attribute} must be an integer.'
)
:
Yii
::
t
(
'yii'
,
'{attribute} must be a number.'
);
}
}
$message
=
strtr
(
$message
,
array
(
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$label
,
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
));
if
((
$tooBig
=
$this
->
tooBig
)
===
null
)
{
$tooBig
=
\Yii
::
t
(
'yii'
,
'{attribute} is too big (maximum is {max}).'
);
}
$tooBig
=
strtr
(
$tooBig
,
array
(
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
'{max}'
=>
$this
->
max
,
));
if
((
$tooSmall
=
$this
->
tooSmall
)
===
null
)
{
$tooSmall
=
\Yii
::
t
(
'yii'
,
'{attribute} is too small (minimum is {min}).'
);
}
$tooSmall
=
strtr
(
$tooSmall
,
array
(
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
'{min}'
=>
$this
->
min
,
));
));
$pattern
=
$this
->
integerOnly
?
$this
->
integerPattern
:
$this
->
numberPattern
;
$js
=
"
$js
=
"
if(!value.match(
{
$this
->
pattern
}
)) {
if(!value.match(
$pattern
)) {
messages.push("
.
json_encode
(
$message
)
.
");
messages.push("
.
json_encode
(
$message
)
.
");
}
}
"
;
"
;
if
(
$this
->
min
!==
null
)
{
if
(
$this
->
min
!==
null
)
{
if
((
$tooSmall
=
$this
->
tooSmall
)
===
null
)
{
$tooSmall
=
Yii
::
t
(
'yii'
,
'{attribute} is too small (minimum is {min}).'
);
}
$tooSmall
=
strtr
(
$tooSmall
,
array
(
'{attribute}'
=>
$label
,
'{min}'
=>
$this
->
min
,
));
$js
.=
"
$js
.=
"
if(value<
{
$this
->
min
}
) {
if(value<
{
$this
->
min
}
) {
messages.push("
.
json_encode
(
$tooSmall
)
.
");
messages.push("
.
json_encode
(
$tooSmall
)
.
");
}
}
"
;
"
;
}
}
if
(
$this
->
max
!==
null
)
{
if
(
$this
->
max
!==
null
)
{
if
((
$tooBig
=
$this
->
tooBig
)
===
null
)
{
$tooBig
=
Yii
::
t
(
'yii'
,
'{attribute} is too big (maximum is {max}).'
);
}
$tooBig
=
strtr
(
$tooBig
,
array
(
'{attribute}'
=>
$label
,
'{max}'
=>
$this
->
max
,
));
$js
.=
"
$js
.=
"
if(value>
{
$this
->
max
}
) {
if(value>
{
$this
->
max
}
) {
messages.push("
.
json_encode
(
$tooBig
)
.
");
messages.push("
.
json_encode
(
$tooBig
)
.
");
}
}
"
;
"
;
...
@@ -134,7 +148,7 @@ if(value> {$this->max}) {
...
@@ -134,7 +148,7 @@ if(value> {$this->max}) {
if
(
$this
->
allowEmpty
)
{
if
(
$this
->
allowEmpty
)
{
$js
=
"
$js
=
"
if(
$
.trim(value)!='') {
if(
jQuery
.trim(value)!='') {
$js
$js
}
}
"
;
"
;
...
@@ -142,4 +156,4 @@ if($.trim(value)!='') {
...
@@ -142,4 +156,4 @@ if($.trim(value)!='') {
return
$js
;
return
$js
;
}
}
}
}
\ No newline at end of file
framework/validators/Validator.php
View file @
7df4a9bd
...
@@ -59,8 +59,12 @@ abstract class Validator extends Component
...
@@ -59,8 +59,12 @@ abstract class Validator extends Component
'file'
=>
'yii\validators\FileValidator'
,
'file'
=>
'yii\validators\FileValidator'
,
'filter'
=>
'yii\validators\FilterValidator'
,
'filter'
=>
'yii\validators\FilterValidator'
,
'in'
=>
'yii\validators\RangeValidator'
,
'in'
=>
'yii\validators\RangeValidator'
,
'integer'
=>
'yii\validators\IntegerValidator'
,
'integer'
=>
array
(
'class'
=>
'yii\validators\NumberValidator'
,
'integerOnly'
=>
true
,
),
'match'
=>
'yii\validators\RegularExpressionValidator'
,
'match'
=>
'yii\validators\RegularExpressionValidator'
,
'number'
=>
'yii\validators\NumberValidator'
,
'required'
=>
'yii\validators\RequiredValidator'
,
'required'
=>
'yii\validators\RequiredValidator'
,
'string'
=>
'yii\validators\StringValidator'
,
'string'
=>
'yii\validators\StringValidator'
,
'unique'
=>
'yii\validators\UniqueValidator'
,
'unique'
=>
'yii\validators\UniqueValidator'
,
...
@@ -120,6 +124,7 @@ abstract class Validator extends Component
...
@@ -120,6 +124,7 @@ abstract class Validator extends Component
if
(
!
is_array
(
$attributes
))
{
if
(
!
is_array
(
$attributes
))
{
$attributes
=
preg_split
(
'/[\s,]+/'
,
$attributes
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
$attributes
=
preg_split
(
'/[\s,]+/'
,
$attributes
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
}
$params
[
'attributes'
]
=
$attributes
;
if
(
isset
(
$params
[
'on'
])
&&
!
is_array
(
$params
[
'on'
]))
{
if
(
isset
(
$params
[
'on'
])
&&
!
is_array
(
$params
[
'on'
]))
{
$params
[
'on'
]
=
preg_split
(
'/[\s,]+/'
,
$params
[
'on'
],
-
1
,
PREG_SPLIT_NO_EMPTY
);
$params
[
'on'
]
=
preg_split
(
'/[\s,]+/'
,
$params
[
'on'
],
-
1
,
PREG_SPLIT_NO_EMPTY
);
...
@@ -131,25 +136,22 @@ abstract class Validator extends Component
...
@@ -131,25 +136,22 @@ abstract class Validator extends Component
if
(
method_exists
(
$object
,
$type
))
{
if
(
method_exists
(
$object
,
$type
))
{
// method-based validator
// method-based validator
$config
=
array
(
$params
[
'class'
]
=
__NAMESPACE__
.
'\InlineValidator'
;
'class'
=>
__NAMESPACE__
.
'\InlineValidator'
,
$params
[
'method'
]
=
$type
;
'method'
=>
$type
,
'attributes'
=>
$attributes
,
);
}
else
{
}
else
{
if
(
is
_string
(
$type
)
&&
is
set
(
self
::
$builtInValidators
[
$type
]))
{
if
(
isset
(
self
::
$builtInValidators
[
$type
]))
{
$type
=
self
::
$builtInValidators
[
$type
];
$type
=
self
::
$builtInValidators
[
$type
];
}
}
$config
=
array
(
if
(
is_array
(
$type
))
{
'class'
=>
$type
,
foreach
(
$type
as
$name
=>
$value
)
{
'attributes'
=>
$attributes
,
$params
[
$name
]
=
$value
;
);
}
}
}
else
{
foreach
(
$params
as
$name
=>
$value
)
{
$params
[
'class'
]
=
$type
;
$config
[
$name
]
=
$value
;
}
}
}
return
\Yii
::
createObject
(
$
config
);
return
\Yii
::
createObject
(
$
params
);
}
}
/**
/**
...
@@ -240,6 +242,6 @@ abstract class Validator extends Component
...
@@ -240,6 +242,6 @@ abstract class Validator extends Component
public
function
isEmpty
(
$value
,
$trim
=
false
)
public
function
isEmpty
(
$value
,
$trim
=
false
)
{
{
return
$value
===
null
||
$value
===
array
()
||
$value
===
''
return
$value
===
null
||
$value
===
array
()
||
$value
===
''
||
$trim
&&
is_scalar
(
$value
)
&&
trim
(
$value
)
===
''
;
||
$trim
&&
is_scalar
(
$value
)
&&
trim
(
$value
)
===
''
;
}
}
}
}
todo.md
View file @
7df4a9bd
...
@@ -13,9 +13,8 @@
...
@@ -13,9 +13,8 @@
-
validators
-
validators
*
FileValidator: depends on CUploadedFile
*
FileValidator: depends on CUploadedFile
*
CaptchaValidator: depends on CaptchaAction
*
CaptchaValidator: depends on CaptchaAction
*
type conversion rules
*
DateValidator: should we use CDateTimeParser, or simply use strtotime()?
*
CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
*
CompareValidator::clientValidateAttribute(): depends on CHtml::activeId()
*
DateValidator: TBD
---
---
...
...
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