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
32f27c88
Commit
32f27c88
authored
Jul 25, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
a5b3932a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
32 deletions
+70
-32
IntegerValidator.php
framework/validators/IntegerValidator.php
+52
-0
NumberValidator.php
framework/validators/NumberValidator.php
+12
-24
Validator.php
framework/validators/Validator.php
+3
-6
todo.txt
todo.txt
+3
-2
No files found.
framework/validators/IntegerValidator.php
0 → 100644
View file @
32f27c88
<?php
/**
* IntegerValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 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 @
32f27c88
...
...
@@ -12,16 +12,16 @@ namespace yii\validators;
/**
* NumberValidator validates that the attribute value is a number.
*
* The format of the number must match the regular expression specified in [[pattern]].
* Optionally, you may configure the [[max]] and [[min]] properties to ensure the number
* is within certain range.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
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,
* meaning that if the attribute is empty, it is considered valid.
*/
...
...
@@ -43,13 +43,10 @@ class NumberValidator extends Validator
*/
public
$tooSmall
;
/**
* @var string the regular expression for matching integers.
* @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).
*/
public
$integerPattern
=
'/^\s*[+-]?\d+\s*$/'
;
/**
* @var string the regular expression for matching numbers.
*/
public
$numberPattern
=
'/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'
;
public
$pattern
=
'/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'
;
/**
...
...
@@ -64,17 +61,9 @@ class NumberValidator extends Validator
if
(
$this
->
allowEmpty
&&
$this
->
isEmpty
(
$value
))
{
return
;
}
if
(
$this
->
integerOnly
)
{
if
(
!
preg_match
(
$this
->
integerPattern
,
"
$value
"
))
{
$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
(
!
preg_match
(
$this
->
pattern
,
"
$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
)
{
$message
=
$this
->
tooSmall
!==
null
?
$this
->
tooSmall
:
Yii
::
t
(
'yii'
,
'{attribute} is too small (minimum is {min}).'
);
...
...
@@ -98,7 +87,7 @@ class NumberValidator extends Validator
$value
=
$object
->
$attribute
;
if
((
$message
=
$this
->
message
)
===
null
)
{
$message
=
$this
->
integerOnly
?
Yii
::
t
(
'yii'
,
'{attribute} must be an integer.'
)
:
Yii
::
t
(
'yii'
,
'{attribute} must be a number.'
);
$message
=
Yii
::
t
(
'yii'
,
'{attribute} must be a number.'
);
}
$message
=
strtr
(
$message
,
array
(
'{attribute}'
=>
$label
,
...
...
@@ -123,9 +112,8 @@ class NumberValidator extends Validator
'{min}'
=>
$this
->
min
,
));
$pattern
=
$this
->
integerOnly
?
$this
->
integerPattern
:
$this
->
numberPattern
;
$js
=
"
if(!value.match(
$pattern
)) {
if(!value.match(
{
$this
->
pattern
}
)) {
messages.push("
.
json_encode
(
$message
)
.
");
}
"
;
...
...
framework/validators/Validator.php
View file @
32f27c88
...
...
@@ -66,10 +66,7 @@ abstract class Validator extends \yii\base\Component
'in'
=>
'\yii\validators\RangeValidator'
,
'boolean'
=>
'\yii\validators\BooleanValidator'
,
'string'
=>
'\yii\validators\StringValidator'
,
'integer'
=>
array
(
'class'
=>
'\yii\validators\NumberValidator'
,
'integerOnly'
=>
true
,
),
'integer'
=>
'\yii\validators\IntegerValidator'
,
'double'
=>
'\yii\validators\NumberValidator'
,
'compare'
=>
'\yii\validators\CompareValidator'
,
...
...
@@ -166,10 +163,10 @@ abstract class Validator extends \yii\base\Component
'attributes'
=>
$attributes
,
);
}
$validator
=
\Yii
::
createComponent
(
$config
);
foreach
(
$params
as
$name
=>
$value
)
{
$
validator
->
$name
=
$value
;
$
config
[
$name
]
=
$value
;
}
$validator
=
\Yii
::
createComponent
(
$config
);
return
$validator
;
}
...
...
todo.txt
View file @
32f27c88
- CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
\ No newline at end of file
- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
- Can consider merging UniqueValidator and ExistValidator and using a NOT property.
\ No newline at end of file
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