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
cb4a8c76
Commit
cb4a8c76
authored
Dec 06, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored validators.
parent
1c761753
Hide whitespace changes
Inline
Side-by-side
Showing
34 changed files
with
501 additions
and
673 deletions
+501
-673
validation.md
docs/guide/validation.md
+2
-2
Model.php
framework/yii/base/Model.php
+1
-1
CaptchaValidator.php
framework/yii/captcha/CaptchaValidator.php
+6
-26
BooleanValidator.php
framework/yii/validators/BooleanValidator.php
+11
-27
CompareValidator.php
framework/yii/validators/CompareValidator.php
+35
-40
DateValidator.php
framework/yii/validators/DateValidator.php
+14
-19
DefaultValueValidator.php
framework/yii/validators/DefaultValueValidator.php
+1
-3
EmailValidator.php
framework/yii/validators/EmailValidator.php
+17
-38
ExistValidator.php
framework/yii/validators/ExistValidator.php
+6
-13
FileValidator.php
framework/yii/validators/FileValidator.php
+25
-32
FilterValidator.php
framework/yii/validators/FilterValidator.php
+2
-7
ImageValidator.php
framework/yii/validators/ImageValidator.php
+17
-24
InlineValidator.php
framework/yii/validators/InlineValidator.php
+2
-22
NumberValidator.php
framework/yii/validators/NumberValidator.php
+18
-18
RangeValidator.php
framework/yii/validators/RangeValidator.php
+6
-27
RegularExpressionValidator.php
framework/yii/validators/RegularExpressionValidator.php
+6
-27
RequiredValidator.php
framework/yii/validators/RequiredValidator.php
+13
-33
SafeValidator.php
framework/yii/validators/SafeValidator.php
+0
-6
StringValidator.php
framework/yii/validators/StringValidator.php
+19
-19
UniqueValidator.php
framework/yii/validators/UniqueValidator.php
+2
-6
UrlValidator.php
framework/yii/validators/UrlValidator.php
+12
-24
Validator.php
framework/yii/validators/Validator.php
+47
-12
BooleanValidatorTest.php
tests/unit/framework/validators/BooleanValidatorTest.php
+14
-14
CompareValidatorTest.php
tests/unit/framework/validators/CompareValidatorTest.php
+6
-7
DateValidatorTest.php
tests/unit/framework/validators/DateValidatorTest.php
+9
-10
EmailValidatorTest.php
tests/unit/framework/validators/EmailValidatorTest.php
+47
-47
ExistValidatorTest.php
tests/unit/framework/validators/ExistValidatorTest.php
+7
-8
NumberValidatorTest.php
tests/unit/framework/validators/NumberValidatorTest.php
+54
-55
RangeValidatorTest.php
tests/unit/framework/validators/RangeValidatorTest.php
+21
-22
RegularExpressionValidatorTest.php
...t/framework/validators/RegularExpressionValidatorTest.php
+8
-9
RequiredValidatorTest.php
tests/unit/framework/validators/RequiredValidatorTest.php
+15
-16
StringValidatorTest.php
tests/unit/framework/validators/StringValidatorTest.php
+26
-27
UrlValidatorTest.php
tests/unit/framework/validators/UrlValidatorTest.php
+20
-20
ValidatorTest.php
tests/unit/framework/validators/ValidatorTest.php
+12
-12
No files found.
docs/guide/validation.md
View file @
cb4a8c76
...
...
@@ -172,10 +172,10 @@ operate without model do. In our case to validate an email we can do the followi
```
php
$email
=
'test@example.com'
;
$validator
=
new
yii\validators\EmailValidator
();
if
(
$validator
->
validate
Value
(
$email
))
{
if
(
$validator
->
validate
(
$email
,
$error
))
{
echo
'Email is valid.'
;
}
else
{
echo
'Email is not valid.'
echo
$error
;
}
```
...
...
framework/yii/base/Model.php
View file @
cb4a8c76
...
...
@@ -321,7 +321,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
}
if
(
$this
->
beforeValidate
())
{
foreach
(
$this
->
getActiveValidators
()
as
$validator
)
{
$validator
->
validate
(
$this
,
$attributes
);
$validator
->
validate
Attributes
(
$this
,
$attributes
);
}
$this
->
afterValidate
();
return
!
$this
->
hasErrors
();
...
...
framework/yii/captcha/CaptchaValidator.php
View file @
cb4a8c76
...
...
@@ -38,7 +38,7 @@ class CaptchaValidator extends Validator
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -49,28 +49,13 @@ class CaptchaValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public
function
validateValue
(
$value
)
protected
function
validateValue
(
$value
)
{
$captcha
=
$this
->
createCaptchaAction
();
return
!
is_array
(
$value
)
&&
$captcha
->
validate
(
$value
,
$this
->
caseSensitive
);
$valid
=
!
is_array
(
$value
)
&&
$captcha
->
validate
(
$value
,
$this
->
caseSensitive
);
return
$valid
?
null
:
[
$this
->
message
,
[]];
}
/**
...
...
@@ -93,12 +78,7 @@ class CaptchaValidator extends Validator
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/BooleanValidator.php
View file @
cb4a8c76
...
...
@@ -37,7 +37,7 @@ class BooleanValidator extends Validator
public
$strict
=
false
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -48,40 +48,24 @@ class BooleanValidator 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
* @inheritdoc
*/
p
ublic
function
validateAttribute
(
$object
,
$attribut
e
)
p
rotected
function
validateValue
(
$valu
e
)
{
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
$valid
=
!
$this
->
strict
&&
(
$value
==
$this
->
trueValue
||
$value
==
$this
->
falseValue
)
||
$this
->
strict
&&
(
$value
===
$this
->
trueValue
||
$value
===
$this
->
falseValue
);
if
(
!
$valid
)
{
return
[
$this
->
message
,
[
'true'
=>
$this
->
trueValue
,
'false'
=>
$this
->
falseValue
,
]);
]];
}
else
{
return
null
;
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public
function
validateValue
(
$value
)
{
return
!
$this
->
strict
&&
(
$value
==
$this
->
trueValue
||
$value
==
$this
->
falseValue
)
||
$this
->
strict
&&
(
$value
===
$this
->
trueValue
||
$value
===
$this
->
falseValue
);
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/CompareValidator.php
View file @
cb4a8c76
...
...
@@ -41,7 +41,7 @@ class CompareValidator extends Validator
*/
public
$compareAttribute
;
/**
* @var
string
the constant value to be compared with. When both this property
* @var
mixed
the constant value to be compared with. When both this property
* and [[compareAttribute]] are set, this property takes precedence.
* @see compareAttribute
*/
...
...
@@ -66,12 +66,13 @@ class CompareValidator extends Validator
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
* - `{compareValue}`: the value or the attribute label to be compared with
* - `{compareAttribute}`: the label of the attribute to be compared with
*/
public
$message
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -109,11 +110,7 @@ class CompareValidator 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
* @throws InvalidConfigException if CompareValidator::operator is invalid
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -130,18 +127,7 @@ class CompareValidator extends Validator
$compareLabel
=
$object
->
getAttributeLabel
(
$compareAttribute
);
}
switch
(
$this
->
operator
)
{
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
;
default
:
$valid
=
false
;
break
;
}
if
(
!
$valid
)
{
if
(
!
$this
->
compareValues
(
$this
->
operator
,
$value
,
$compareValue
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
'compareAttribute'
=>
$compareLabel
,
'compareValue'
=>
$compareValue
,
...
...
@@ -150,38 +136,47 @@ class CompareValidator extends Validator
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
* @throws InvalidConfigException if [[compareValue]] is not set.
* @inheritdoc
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
if
(
$this
->
compareValue
===
null
)
{
throw
new
InvalidConfigException
(
'CompareValidator::compareValue must be set.'
);
}
if
(
!
$this
->
compareValues
(
$this
->
operator
,
$value
,
$this
->
compareValue
))
{
return
[
$this
->
message
,
[
'compareAttribute'
=>
$this
->
compareValue
,
'compareValue'
=>
$this
->
compareValue
,
]];
}
else
{
return
null
;
}
}
switch
(
$this
->
operator
)
{
case
'=='
:
return
$value
==
$this
->
compareValue
;
case
'==='
:
return
$value
===
$this
->
compareValue
;
case
'!='
:
return
$value
!=
$this
->
compareValue
;
case
'!=='
:
return
$value
!==
$this
->
compareValue
;
case
'>'
:
return
$value
>
$this
->
compareValue
;
case
'>='
:
return
$value
>=
$this
->
compareValue
;
case
'<'
:
return
$value
<
$this
->
compareValue
;
case
'<='
:
return
$value
<=
$this
->
compareValue
;
/**
* Compares two values with the specified operator.
* @param string $operator the comparison operator
* @param mixed $value the value being compared
* @param mixed $compareValue another value being compared
* @return boolean whether the comparison using the specified operator is true.
*/
protected
function
compareValues
(
$operator
,
$value
,
$compareValue
)
{
switch
(
$operator
)
{
case
'=='
:
return
$value
==
$compareValue
;
case
'==='
:
return
$value
===
$compareValue
;
case
'!='
:
return
$value
!=
$compareValue
;
case
'!=='
:
return
$value
!==
$compareValue
;
case
'>'
:
return
$value
>
$compareValue
;
case
'>='
:
return
$value
>=
$compareValue
;
case
'<'
:
return
$value
<
$compareValue
;
case
'<='
:
return
$value
<=
$compareValue
;
default
:
return
false
;
}
}
/**
* 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
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @throws InvalidConfigException if CompareValidator::operator is invalid
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/DateValidator.php
View file @
cb4a8c76
...
...
@@ -32,7 +32,7 @@ class DateValidator extends Validator
public
$timestampAttribute
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -43,36 +43,31 @@ class DateValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
$value
=
$object
->
$attribute
;
if
(
is_array
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
return
;
}
$date
=
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
$errors
=
DateTime
::
getLastErrors
();
if
(
$date
===
false
||
$errors
[
'error_count'
]
||
$errors
[
'warning_count'
])
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$result
=
$this
->
validateValue
(
$value
);
if
(
!
empty
(
$result
))
{
$this
->
addError
(
$object
,
$attribute
,
$result
[
0
],
$result
[
1
]);
}
elseif
(
$this
->
timestampAttribute
!==
null
)
{
$date
=
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
$object
->
{
$this
->
timestampAttribute
}
=
$date
->
getTimestamp
();
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
* @inheritdoc
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
if
(
is_array
(
$value
))
{
return
[
$this
->
message
,
[]];
}
$date
=
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
$errors
=
DateTime
::
getLastErrors
();
return
$errors
[
'error_count'
]
===
0
&&
$errors
[
'warning_count'
]
===
0
;
$invalid
=
$date
===
false
||
$errors
[
'error_count'
]
||
$errors
[
'warning_count'
];
return
$invalid
?
[
$this
->
message
,
[]]
:
null
;
}
}
framework/yii/validators/DefaultValueValidator.php
View file @
cb4a8c76
...
...
@@ -29,9 +29,7 @@ class DefaultValueValidator extends Validator
public
$skipOnEmpty
=
false
;
/**
* Validates the attribute of the object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
framework/yii/validators/EmailValidator.php
View file @
cb4a8c76
...
...
@@ -53,7 +53,7 @@ class EmailValidator extends Validator
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -67,51 +67,30 @@ class EmailValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public
function
validateValue
(
$value
)
protected
function
validateValue
(
$value
)
{
// make sure string length is limited to avoid DOS attacks
if
(
!
is_string
(
$value
)
||
strlen
(
$value
)
>=
320
)
{
return
false
;
}
if
(
!
preg_match
(
'/^(.*<?)(.*)@(.*)(>?)$/'
,
$value
,
$matches
))
{
return
false
;
}
$domain
=
$matches
[
3
];
if
(
$this
->
enableIDN
)
{
$value
=
$matches
[
1
]
.
idn_to_ascii
(
$matches
[
2
])
.
'@'
.
idn_to_ascii
(
$domain
)
.
$matches
[
4
];
}
$valid
=
preg_match
(
$this
->
pattern
,
$value
)
||
$this
->
allowName
&&
preg_match
(
$this
->
fullPattern
,
$value
);
if
(
$valid
&&
$this
->
checkDNS
)
{
$valid
=
checkdnsrr
(
$domain
,
'MX'
)
||
checkdnsrr
(
$domain
,
'A'
);
$valid
=
false
;
}
elseif
(
!
preg_match
(
'/^(.*<?)(.*)@(.*)(>?)$/'
,
$value
,
$matches
))
{
$valid
=
false
;
}
else
{
$domain
=
$matches
[
3
];
if
(
$this
->
enableIDN
)
{
$value
=
$matches
[
1
]
.
idn_to_ascii
(
$matches
[
2
])
.
'@'
.
idn_to_ascii
(
$domain
)
.
$matches
[
4
];
}
$valid
=
preg_match
(
$this
->
pattern
,
$value
)
||
$this
->
allowName
&&
preg_match
(
$this
->
fullPattern
,
$value
);
if
(
$valid
&&
$this
->
checkDNS
)
{
$valid
=
checkdnsrr
(
$domain
,
'MX'
)
||
checkdnsrr
(
$domain
,
'A'
);
}
}
return
$valid
;
return
$valid
?
null
:
[
$this
->
message
,
[]]
;
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/ExistValidator.php
View file @
cb4a8c76
...
...
@@ -39,7 +39,7 @@ class ExistValidator extends Validator
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -50,11 +50,7 @@ class ExistValidator extends Validator
}
/**
* Validates the attribute of 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 string $attribute the attribute being validated
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -76,15 +72,12 @@ class ExistValidator extends Validator
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
* @throws InvalidConfigException if either [[className]] or [[attributeName]] is not set.
* @inheritdoc
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
if
(
is_array
(
$value
))
{
return
false
;
return
[
$this
->
message
,
[]]
;
}
if
(
$this
->
className
===
null
)
{
throw
new
InvalidConfigException
(
'The "className" property must be set.'
);
...
...
@@ -96,6 +89,6 @@ class ExistValidator extends Validator
$className
=
$this
->
className
;
$query
=
$className
::
find
();
$query
->
where
([
$this
->
attributeName
=>
$value
]);
return
$query
->
exists
();
return
$query
->
exists
()
?
null
:
[
$this
->
message
,
[]]
;
}
}
framework/yii/validators/FileValidator.php
View file @
cb4a8c76
...
...
@@ -95,7 +95,7 @@ class FileValidator extends Validator
public
$tooMany
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -124,9 +124,7 @@ class FileValidator extends Validator
}
/**
* Validates the attribute.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -149,62 +147,57 @@ class FileValidator extends Validator
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooMany
,
[
'limit'
=>
$this
->
maxFiles
]);
}
else
{
foreach
(
$files
as
$file
)
{
$this
->
validateFile
(
$object
,
$attribute
,
$file
);
$result
=
$this
->
validateValue
(
$file
);
if
(
!
empty
(
$result
))
{
$this
->
addError
(
$object
,
$attribute
,
$result
[
0
],
$result
[
1
]);
}
}
}
}
else
{
$file
=
$object
->
$attribute
;
if
(
$file
instanceof
UploadedFile
&&
$file
->
error
!=
UPLOAD_ERR_NO_FILE
)
{
$this
->
validateFile
(
$object
,
$attribute
,
$file
);
}
else
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
uploadRequired
);
$result
=
$this
->
validateValue
(
$object
->
$attribute
);
if
(
!
empty
(
$result
))
{
$this
->
addError
(
$object
,
$attribute
,
$result
[
0
],
$result
[
1
]);
}
}
}
/**
* Internally validates a file object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @param UploadedFile $file uploaded file passed to check against a set of rules
* @inheritdoc
*/
p
ublic
function
validateFile
(
$object
,
$attribute
,
$file
)
p
rotected
function
validateValue
(
$file
)
{
if
(
!
$file
instanceof
UploadedFile
||
$file
->
error
==
UPLOAD_ERR_NO_FILE
)
{
return
[
$this
->
uploadRequired
,
[]];
}
switch
(
$file
->
error
)
{
case
UPLOAD_ERR_OK
:
if
(
$this
->
maxSize
!==
null
&&
$file
->
size
>
$this
->
maxSize
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'file'
=>
$file
->
name
,
'limit'
=>
$this
->
getSizeLimit
()]);
}
if
(
$this
->
minSize
!==
null
&&
$file
->
size
<
$this
->
minSize
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooSmall
,
[
'file'
=>
$file
->
name
,
'limit'
=>
$this
->
minSize
]);
}
if
(
!
empty
(
$this
->
types
)
&&
!
in_array
(
strtolower
(
pathinfo
(
$file
->
name
,
PATHINFO_EXTENSION
)),
$this
->
types
,
true
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
wrongType
,
[
'file'
=>
$file
->
name
,
'extensions'
=>
implode
(
', '
,
$this
->
types
)]);
return
[
$this
->
tooBig
,
[
'file'
=>
$file
->
name
,
'limit'
=>
$this
->
getSizeLimit
()]];
}
elseif
(
$this
->
minSize
!==
null
&&
$file
->
size
<
$this
->
minSize
)
{
return
[
$this
->
tooSmall
,
[
'file'
=>
$file
->
name
,
'limit'
=>
$this
->
minSize
]];
}
elseif
(
!
empty
(
$this
->
types
)
&&
!
in_array
(
strtolower
(
pathinfo
(
$file
->
name
,
PATHINFO_EXTENSION
)),
$this
->
types
,
true
))
{
return
[
$this
->
wrongType
,
[
'file'
=>
$file
->
name
,
'extensions'
=>
implode
(
', '
,
$this
->
types
)]];
}
break
;
case
UPLOAD_ERR_INI_SIZE
:
case
UPLOAD_ERR_FORM_SIZE
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'file'
=>
$file
->
name
,
'limit'
=>
$this
->
getSizeLimit
()]);
break
;
return
[
$this
->
tooBig
,
[
'file'
=>
$file
->
name
,
'limit'
=>
$this
->
getSizeLimit
()]];
case
UPLOAD_ERR_PARTIAL
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
Yii
::
warning
(
'File was only partially uploaded: '
.
$file
->
name
,
__METHOD__
);
break
;
return
[
$this
->
message
,
[]]
;
case
UPLOAD_ERR_NO_TMP_DIR
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
Yii
::
warning
(
'Missing the temporary folder to store the uploaded file: '
.
$file
->
name
,
__METHOD__
);
break
;
return
[
$this
->
message
,
[]]
;
case
UPLOAD_ERR_CANT_WRITE
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
Yii
::
warning
(
'Failed to write the uploaded file to disk: '
.
$file
->
name
,
__METHOD__
);
break
;
return
[
$this
->
message
,
[]]
;
case
UPLOAD_ERR_EXTENSION
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
Yii
::
warning
(
'File upload was stopped by some PHP extension: '
.
$file
->
name
,
__METHOD__
);
break
;
return
[
$this
->
message
,
[]]
;
default
:
break
;
}
return
null
;
}
/**
...
...
framework/yii/validators/FilterValidator.php
View file @
cb4a8c76
...
...
@@ -46,8 +46,7 @@ class FilterValidator extends Validator
public
$skipOnEmpty
=
false
;
/**
* Initializes the validator.
* @throws InvalidConfigException if [[filter]] is not set.
* @inheritdoc
*/
public
function
init
()
{
...
...
@@ -58,11 +57,7 @@ class FilterValidator 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
* @throws InvalidConfigException if filter property is not a valid callback
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
framework/yii/validators/ImageValidator.php
View file @
cb4a8c76
...
...
@@ -110,7 +110,7 @@ class ImageValidator extends FileValidator
public
$wrongMimeType
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -140,58 +140,51 @@ class ImageValidator extends FileValidator
}
/**
* Internally validates a file object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @param UploadedFile $file uploaded file passed to check against a set of rules
* @inheritdoc
*/
p
ublic
function
validateFile
(
$object
,
$attribute
,
$file
)
p
rotected
function
validateValue
(
$file
)
{
parent
::
validateFile
(
$object
,
$attribute
,
$file
);
if
(
!
$object
->
hasErrors
(
$attribute
))
{
$this
->
validateImage
(
$object
,
$attribute
,
$file
);
}
$result
=
parent
::
validateValue
(
$file
);
return
empty
(
$result
)
?
$this
->
validateImage
(
$file
)
:
$result
;
}
/**
* Internally validates a file object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* Validates an image file.
* @param UploadedFile $image uploaded file passed to check against a set of rules
* @return array|null the error message and the parameters to be inserted into the error message.
* Null should be returned if the data is valid.
*/
p
ublic
function
validateImage
(
$object
,
$attribute
,
$image
)
p
rotected
function
validateImage
(
$image
)
{
if
(
!
empty
(
$this
->
mimeTypes
)
&&
!
in_array
(
FileHelper
::
getMimeType
(
$image
->
tempName
),
$this
->
mimeTypes
,
true
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
wrongMimeType
,
[
'file'
=>
$image
->
name
,
'mimeTypes'
=>
implode
(
', '
,
$this
->
mimeTypes
)])
;
return
[
$this
->
wrongMimeType
,
[
'file'
=>
$image
->
name
,
'mimeTypes'
=>
implode
(
', '
,
$this
->
mimeTypes
)]]
;
}
if
(
false
===
(
$imageInfo
=
getimagesize
(
$image
->
tempName
)))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notImage
,
[
'file'
=>
$image
->
name
]);
return
;
return
[
$this
->
notImage
,
[
'file'
=>
$image
->
name
]];
}
list
(
$width
,
$height
,
$type
)
=
$imageInfo
;
if
(
$width
==
0
||
$height
==
0
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notImage
,
[
'file'
=>
$image
->
name
]);
return
;
return
[
$this
->
notImage
,
[
'file'
=>
$image
->
name
]];
}
if
(
$this
->
minWidth
!==
null
&&
$width
<
$this
->
minWidth
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
underWidth
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
minWidth
])
;
return
[
$this
->
underWidth
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
minWidth
]]
;
}
if
(
$this
->
minHeight
!==
null
&&
$height
<
$this
->
minHeight
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
underHeight
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
minHeight
])
;
return
[
$this
->
underHeight
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
minHeight
]]
;
}
if
(
$this
->
maxWidth
!==
null
&&
$width
>
$this
->
maxWidth
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
overWidth
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
maxWidth
])
;
return
[
$this
->
overWidth
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
maxWidth
]]
;
}
if
(
$this
->
maxHeight
!==
null
&&
$height
>
$this
->
maxHeight
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
overHeight
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
maxHeight
])
;
return
[
$this
->
overHeight
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
maxHeight
]]
;
}
return
null
;
}
}
framework/yii/validators/InlineValidator.php
View file @
cb4a8c76
...
...
@@ -55,9 +55,7 @@ class InlineValidator extends Validator
public
$clientValidate
;
/**
* Validates the attribute of the object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -69,25 +67,7 @@ class InlineValidator extends Validator
}
/**
* Returns the JavaScript needed for performing client-side validation.
*
* You may override this method to return the JavaScript validation code if
* the validator can support client-side validation.
*
* The following JavaScript variables are predefined and can be used in the validation code:
*
* - `attribute`: the name of the attribute being validated.
* - `value`: the value being validated.
* - `messages`: an array used to hold the validation error messages for the attribute.
*
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script. Null if the validator does not support
* client-side validation.
* @see enableClientValidation
* @see \yii\web\ActiveForm::enableClientValidation
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/NumberValidator.php
View file @
cb4a8c76
...
...
@@ -56,7 +56,7 @@ class NumberValidator extends Validator
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -74,10 +74,7 @@ class NumberValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -99,24 +96,27 @@ class NumberValidator extends Validator
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
* @inheritdoc
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
return
preg_match
(
$this
->
integerOnly
?
$this
->
integerPattern
:
$this
->
numberPattern
,
"
$value
"
)
&&
(
$this
->
min
===
null
||
$value
>=
$this
->
min
)
&&
(
$this
->
max
===
null
||
$value
<=
$this
->
max
);
if
(
is_array
(
$value
))
{
return
[
Yii
::
t
(
'yii'
,
'{attribute} is invalid.'
),
[]];
}
$pattern
=
$this
->
integerOnly
?
$this
->
integerPattern
:
$this
->
numberPattern
;
if
(
!
preg_match
(
$pattern
,
"
$value
"
))
{
return
[
$this
->
message
,
[]];
}
elseif
(
$this
->
min
!==
null
&&
$value
<
$this
->
min
)
{
return
[
$this
->
tooSmall
,
[
'min'
=>
$this
->
min
]];
}
elseif
(
$this
->
max
!==
null
&&
$value
>
$this
->
max
)
{
return
[
$this
->
tooBig
,
[
'max'
=>
$this
->
max
]];
}
else
{
return
null
;
}
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/RangeValidator.php
View file @
cb4a8c76
...
...
@@ -38,8 +38,7 @@ class RangeValidator extends Validator
public
$not
=
false
;
/**
* Initializes the validator.
* @throws InvalidConfigException if [[range]] is not set.
* @inheritdoc
*/
public
function
init
()
{
...
...
@@ -53,37 +52,17 @@ class RangeValidator 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
* @inheritdoc
*/
p
ublic
function
validateAttribute
(
$object
,
$attribut
e
)
p
rotected
function
validateValue
(
$valu
e
)
{
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public
function
validateValue
(
$value
)
{
return
!
$this
->
not
&&
in_array
(
$value
,
$this
->
range
,
$this
->
strict
)
$valid
=
!
$this
->
not
&&
in_array
(
$value
,
$this
->
range
,
$this
->
strict
)
||
$this
->
not
&&
!
in_array
(
$value
,
$this
->
range
,
$this
->
strict
);
return
$valid
?
null
:
[
$this
->
message
,
[]];
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/RegularExpressionValidator.php
View file @
cb4a8c76
...
...
@@ -34,8 +34,7 @@ class RegularExpressionValidator extends Validator
public
$not
=
false
;
/**
* Initializes the validator.
* @throws InvalidConfigException if [[pattern]] is not set.
* @inheritdoc
*/
public
function
init
()
{
...
...
@@ -49,38 +48,18 @@ class RegularExpressionValidator 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
* @inheritdoc
*/
p
ublic
function
validateAttribute
(
$object
,
$attribut
e
)
p
rotected
function
validateValue
(
$valu
e
)
{
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public
function
validateValue
(
$value
)
{
return
!
is_array
(
$value
)
&&
$valid
=
!
is_array
(
$value
)
&&
(
!
$this
->
not
&&
preg_match
(
$this
->
pattern
,
$value
)
||
$this
->
not
&&
!
preg_match
(
$this
->
pattern
,
$value
));
return
$valid
?
null
:
[
$this
->
message
,
[]];
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/RequiredValidator.php
View file @
cb4a8c76
...
...
@@ -51,7 +51,7 @@ class RequiredValidator extends Validator
public
$message
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -63,48 +63,28 @@ class RequiredValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
if
(
!
$this
->
validateValue
(
$object
->
$attribute
))
{
if
(
$this
->
requiredValue
===
null
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
else
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
'requiredValue'
=>
$this
->
requiredValue
,
]);
}
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public
function
validateValue
(
$value
)
protected
function
validateValue
(
$value
)
{
if
(
$this
->
requiredValue
===
null
)
{
if
(
$this
->
strict
&&
$value
!==
null
||
!
$this
->
strict
&&
!
$this
->
isEmpty
(
$value
,
true
))
{
return
true
;
return
null
;
}
}
elseif
(
!
$this
->
strict
&&
$value
==
$this
->
requiredValue
||
$this
->
strict
&&
$value
===
$this
->
requiredValue
)
{
return
true
;
return
null
;
}
if
(
$this
->
requiredValue
===
null
)
{
return
[
$this
->
message
,
[]];
}
else
{
return
[
$this
->
message
,
[
'requiredValue'
=>
$this
->
requiredValue
,
]];
}
return
false
;
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/SafeValidator.php
View file @
cb4a8c76
...
...
@@ -15,10 +15,4 @@ namespace yii\validators;
*/
class
SafeValidator
extends
Validator
{
/**
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
}
}
framework/yii/validators/StringValidator.php
View file @
cb4a8c76
...
...
@@ -63,7 +63,7 @@ class StringValidator extends Validator
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -95,10 +95,7 @@ class StringValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
@@ -123,28 +120,31 @@ class StringValidator extends Validator
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
* @inheritdoc
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
if
(
!
is_string
(
$value
))
{
return
false
;
return
[
$this
->
message
,
[]]
;
}
$length
=
mb_strlen
(
$value
,
$this
->
encoding
);
return
(
$this
->
min
===
null
||
$length
>=
$this
->
min
)
&&
(
$this
->
max
===
null
||
$length
<=
$this
->
max
)
&&
(
$this
->
length
===
null
||
$length
===
$this
->
length
);
if
(
$this
->
min
!==
null
&&
$length
<
$this
->
min
)
{
return
[
$this
->
tooShort
,
[
'min'
=>
$this
->
min
]];
}
if
(
$this
->
max
!==
null
&&
$length
>
$this
->
max
)
{
return
[
$this
->
tooLong
,
[
'max'
=>
$this
->
max
]];
}
if
(
$this
->
length
!==
null
&&
$length
!==
$this
->
length
)
{
return
[
$this
->
notEqual
,
[
'length'
=>
$this
->
length
]];
}
return
null
;
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/UniqueValidator.php
View file @
cb4a8c76
...
...
@@ -35,7 +35,7 @@ class UniqueValidator extends Validator
public
$attributeName
;
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -46,11 +46,7 @@ class UniqueValidator extends Validator
}
/**
* Validates the attribute of 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 string $attribute the attribute being validated
* @throws InvalidConfigException if table doesn't have column specified
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
...
...
framework/yii/validators/UrlValidator.php
View file @
cb4a8c76
...
...
@@ -48,7 +48,7 @@ class UrlValidator extends Validator
/**
*
Initializes the validator.
*
@inheritdoc
*/
public
function
init
()
{
...
...
@@ -62,29 +62,23 @@ class UrlValidator 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
* @inheritdoc
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
$value
=
$object
->
$attribute
;
if
(
$this
->
validateValue
(
$value
))
{
if
(
$this
->
defaultScheme
!==
null
&&
strpos
(
$value
,
'://'
)
===
false
)
{
$object
->
$attribute
=
$this
->
defaultScheme
.
'://'
.
$value
;
}
}
else
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
$result
=
$this
->
validateValue
(
$value
);
if
(
!
empty
(
$result
))
{
$this
->
addError
(
$object
,
$attribute
,
$result
[
0
],
$result
[
1
]);
}
elseif
(
$this
->
defaultScheme
!==
null
&&
strpos
(
$value
,
'://'
)
===
false
)
{
$object
->
$attribute
=
$this
->
defaultScheme
.
'://'
.
$value
;
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
* @inheritdoc
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
// make sure the length is limited to avoid DOS attacks
if
(
is_string
(
$value
)
&&
strlen
(
$value
)
<
2000
)
{
...
...
@@ -105,20 +99,14 @@ class UrlValidator extends Validator
}
if
(
preg_match
(
$pattern
,
$value
))
{
return
true
;
return
null
;
}
}
return
false
;
return
[
$this
->
message
,
[]]
;
}
/**
* 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.
* @param \yii\web\View $view the view object that is going to be used to render views or view files
* containing a model form with this validator applied.
* @return string the client-side validation script.
* @see \yii\Web\ActiveForm::enableClientValidation
* @inheritdoc
*/
public
function
clientValidateAttribute
(
$object
,
$attribute
,
$view
)
{
...
...
framework/yii/validators/Validator.php
View file @
cb4a8c76
...
...
@@ -14,7 +14,7 @@ use yii\base\NotSupportedException;
/**
* Validator is the base class for all validators.
*
* Child classes should override the [[validate
Attribute()]] method
to provide the actual
* Child classes should override the [[validate
Value()]] and/or [[validateAttribute()]] methods
to provide the actual
* logic of performing data validation. Child classes may also override [[clientValidateAttribute()]]
* to provide client-side validation support.
*
...
...
@@ -38,13 +38,14 @@ use yii\base\NotSupportedException;
* - `required`: [[RequiredValidator]]
* - `safe`: [[SafeValidator]]
* - `string`: [[StringValidator]]
* - `trim`: [[FilterValidator]]
* - `unique`: [[UniqueValidator]]
* - `url`: [[UrlValidator]]
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
abstract
class
Validator
extends
Component
class
Validator
extends
Component
{
/**
* @var array list of built-in validators (name => class or configuration)
...
...
@@ -71,6 +72,10 @@ abstract class Validator extends Component
'required'
=>
'yii\validators\RequiredValidator'
,
'safe'
=>
'yii\validators\SafeValidator'
,
'string'
=>
'yii\validators\StringValidator'
,
'trim'
=>
[
'class'
=>
'yii\validators\FilterValidator'
,
'filter'
=>
'trim'
,
],
'unique'
=>
'yii\validators\UniqueValidator'
,
'url'
=>
'yii\validators\UrlValidator'
,
];
...
...
@@ -116,13 +121,6 @@ abstract class Validator extends Component
*/
public
$enableClientValidation
=
true
;
/**
* Validates a single attribute.
* Child classes must implement this method to provide the actual validation logic.
* @param \yii\base\Model $object the data object to be validated
* @param string $attribute the name of the attribute to be validated.
*/
abstract
public
function
validateAttribute
(
$object
,
$attribute
);
/**
* Creates a validator object.
...
...
@@ -177,7 +175,7 @@ abstract class Validator extends Component
* it will be ignored.
* If this parameter is null, every attribute listed in [[attributes]] will be validated.
*/
public
function
validate
(
$object
,
$attributes
=
null
)
public
function
validate
Attributes
(
$object
,
$attributes
=
null
)
{
if
(
is_array
(
$attributes
))
{
$attributes
=
array_intersect
(
$this
->
attributes
,
$attributes
);
...
...
@@ -194,12 +192,49 @@ abstract class Validator extends Component
}
/**
* Validates a single attribute.
* Child classes must implement this method to provide the actual validation logic.
* @param \yii\base\Model $object the data object to be validated
* @param string $attribute the name of the attribute to be validated.
*/
public
function
validateAttribute
(
$object
,
$attribute
)
{
$result
=
$this
->
validateValue
(
$object
->
$attribute
);
if
(
!
empty
(
$result
))
{
$this
->
addError
(
$object
,
$attribute
,
$result
[
0
],
$result
[
1
]);
}
}
/**
* Validates a given value.
* You may use this method to validate a value out of the context of a data model.
* @param mixed $value the data value to be validated.
* @param string $error the error message to be returned, if the validation fails.
* @return boolean whether the data is valid.
*/
public
function
validate
(
$value
,
&
$error
=
null
)
{
$result
=
$this
->
validateValue
(
$value
);
if
(
empty
(
$result
))
{
return
true
;
}
else
{
list
(
$message
,
$params
)
=
$result
;
$params
[
'attribute'
]
=
Yii
::
t
(
'yii'
,
'the input value'
);
$params
[
'value'
]
=
is_array
(
$value
)
?
'array()'
:
$value
;
$error
=
Yii
::
$app
->
getI18n
()
->
format
(
$message
,
$params
,
Yii
::
$app
->
language
);
return
false
;
}
}
/**
* Validates a value.
* A validator class can implement this method to support data validation out of the context of a data model.
* @param mixed $value the data value to be validated.
* @throws NotSupportedException if data validation without a model is not supported
* @return array|null the error message and the parameters to be inserted into the error message.
* Null should be returned if the data is valid.
* @throws NotSupportedException if the validator does not supporting data validation without a model
*/
p
ublic
function
validateValue
(
$value
)
p
rotected
function
validateValue
(
$value
)
{
throw
new
NotSupportedException
(
get_class
(
$this
)
.
' does not support validateValue().'
);
}
...
...
tests/unit/framework/validators/BooleanValidatorTest.php
View file @
cb4a8c76
...
...
@@ -19,23 +19,23 @@ class BooleanValidatorTest extends TestCase
public
function
testValidateValue
()
{
$val
=
new
BooleanValidator
;
$this
->
assertTrue
(
$val
->
validate
Value
(
true
));
$this
->
assertTrue
(
$val
->
validate
Value
(
false
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'0'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'1'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
null
));
$this
->
assertFalse
(
$val
->
validate
Value
([]));
$this
->
assertTrue
(
$val
->
validate
(
true
));
$this
->
assertTrue
(
$val
->
validate
(
false
));
$this
->
assertTrue
(
$val
->
validate
(
'0'
));
$this
->
assertTrue
(
$val
->
validate
(
'1'
));
$this
->
assertFalse
(
$val
->
validate
(
null
));
$this
->
assertFalse
(
$val
->
validate
([]));
$val
->
strict
=
true
;
$this
->
assertTrue
(
$val
->
validate
Value
(
'0'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'1'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
true
));
$this
->
assertFalse
(
$val
->
validate
Value
(
false
));
$this
->
assertTrue
(
$val
->
validate
(
'0'
));
$this
->
assertTrue
(
$val
->
validate
(
'1'
));
$this
->
assertFalse
(
$val
->
validate
(
true
));
$this
->
assertFalse
(
$val
->
validate
(
false
));
$val
->
trueValue
=
true
;
$val
->
falseValue
=
false
;
$this
->
assertFalse
(
$val
->
validate
Value
(
'0'
));
$this
->
assertFalse
(
$val
->
validate
Value
([]));
$this
->
assertTrue
(
$val
->
validate
Value
(
true
));
$this
->
assertTrue
(
$val
->
validate
Value
(
false
));
$this
->
assertFalse
(
$val
->
validate
(
'0'
));
$this
->
assertFalse
(
$val
->
validate
([]));
$this
->
assertTrue
(
$val
->
validate
(
true
));
$this
->
assertTrue
(
$val
->
validate
(
false
));
}
public
function
testValidateAttributeAndError
()
...
...
tests/unit/framework/validators/CompareValidatorTest.php
View file @
cb4a8c76
...
...
@@ -20,7 +20,7 @@ class CompareValidatorTest extends TestCase
{
$this
->
setExpectedException
(
'yii\base\InvalidConfigException'
);
$val
=
new
CompareValidator
;
$val
->
validate
Value
(
'val'
);
$val
->
validate
(
'val'
);
}
public
function
testValidateValue
()
...
...
@@ -28,14 +28,14 @@ class CompareValidatorTest extends TestCase
$value
=
18449
;
// default config
$val
=
new
CompareValidator
([
'compareValue'
=>
$value
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
$value
));
$this
->
assertTrue
(
$val
->
validate
Value
((
string
)
$value
));
$this
->
assertFalse
(
$val
->
validate
Value
(
$value
+
1
));
$this
->
assertTrue
(
$val
->
validate
(
$value
));
$this
->
assertTrue
(
$val
->
validate
((
string
)
$value
));
$this
->
assertFalse
(
$val
->
validate
(
$value
+
1
));
foreach
(
$this
->
getOperationTestData
(
$value
)
as
$op
=>
$tests
)
{
$val
=
new
CompareValidator
([
'compareValue'
=>
$value
]);
$val
->
operator
=
$op
;
foreach
(
$tests
as
$test
)
{
$this
->
assertEquals
(
$test
[
1
],
$val
->
validate
Value
(
$test
[
0
]));
$this
->
assertEquals
(
$test
[
1
],
$val
->
validate
(
$test
[
0
]));
}
}
}
...
...
@@ -172,4 +172,4 @@ class CompareValidatorTest extends TestCase
}
$this
->
fail
(
'InvalidConfigException expected none received'
);
}
}
\ No newline at end of file
}
tests/unit/framework/validators/DateValidatorTest.php
View file @
cb4a8c76
...
...
@@ -25,17 +25,17 @@ class DateValidatorTest extends TestCase
public
function
testValidateValue
()
{
$val
=
new
DateValidator
;
$this
->
assertFalse
(
$val
->
validate
Value
(
'3232-32-32'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'2013-09-13'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'31.7.2013'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'31-7-2013'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
time
()));
$this
->
assertFalse
(
$val
->
validate
(
'3232-32-32'
));
$this
->
assertTrue
(
$val
->
validate
(
'2013-09-13'
));
$this
->
assertFalse
(
$val
->
validate
(
'31.7.2013'
));
$this
->
assertFalse
(
$val
->
validate
(
'31-7-2013'
));
$this
->
assertFalse
(
$val
->
validate
(
time
()));
$val
->
format
=
'U'
;
$this
->
assertTrue
(
$val
->
validate
Value
(
time
()));
$this
->
assertTrue
(
$val
->
validate
(
time
()));
$val
->
format
=
'd.m.Y'
;
$this
->
assertTrue
(
$val
->
validate
Value
(
'31.7.2013'
));
$this
->
assertTrue
(
$val
->
validate
(
'31.7.2013'
));
$val
->
format
=
'Y-m-!d H:i:s'
;
$this
->
assertTrue
(
$val
->
validate
Value
(
'2009-02-15 15:16:17'
));
$this
->
assertTrue
(
$val
->
validate
(
'2009-02-15 15:16:17'
));
}
public
function
testValidateAttribute
()
...
...
@@ -68,4 +68,4 @@ class DateValidatorTest extends TestCase
$this
->
assertTrue
(
$model
->
hasErrors
(
'attr_date'
));
}
}
\ No newline at end of file
}
tests/unit/framework/validators/EmailValidatorTest.php
View file @
cb4a8c76
...
...
@@ -21,29 +21,29 @@ class EmailValidatorTest extends TestCase
{
$validator
=
new
EmailValidator
();
$this
->
assertTrue
(
$validator
->
validate
Value
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'rmcreative.ru'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'<mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'info@örtliches.de'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'sam@рмкреатиф.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
(
'rmcreative.ru'
));
$this
->
assertFalse
(
$validator
->
validate
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'<mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'info@örtliches.de'
));
$this
->
assertFalse
(
$validator
->
validate
(
'sam@рмкреатиф.ru'
));
$validator
->
allowName
=
true
;
$this
->
assertTrue
(
$validator
->
validate
Value
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'<mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'info@örtliches.de'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'sam@рмкреатиф.ru'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'Informtation info@oertliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'test@example.com'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'John Smith <john.smith@example.com>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'John Smith <example.com>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
(
'rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'<mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'info@örtliches.de'
));
$this
->
assertFalse
(
$validator
->
validate
(
'sam@рмкреатиф.ru'
));
$this
->
assertFalse
(
$validator
->
validate
(
'Informtation info@oertliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
(
'test@example.com'
));
$this
->
assertTrue
(
$validator
->
validate
(
'John Smith <john.smith@example.com>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'John Smith <example.com>'
));
}
public
function
testValidateValueIdn
()
...
...
@@ -55,33 +55,33 @@ class EmailValidatorTest extends TestCase
$validator
=
new
EmailValidator
();
$validator
->
enableIDN
=
true
;
$this
->
assertTrue
(
$validator
->
validate
Value
(
'5011@example.com'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'example@äüößìà.de'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'example@xn--zcack7ayc9a.de'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'info@örtliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'sam@рмкреатиф.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'rmcreative.ru'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'<mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'5011@example.com'
));
$this
->
assertTrue
(
$validator
->
validate
(
'example@äüößìà.de'
));
$this
->
assertTrue
(
$validator
->
validate
(
'example@xn--zcack7ayc9a.de'
));
$this
->
assertTrue
(
$validator
->
validate
(
'info@örtliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
(
'sam@рмкреатиф.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
(
'rmcreative.ru'
));
$this
->
assertFalse
(
$validator
->
validate
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'<mail@cebe.cc>'
));
$validator
->
allowName
=
true
;
$this
->
assertTrue
(
$validator
->
validate
Value
(
'info@örtliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'Informtation <info@örtliches.de>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'Informtation info@örtliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'sam@рмкреатиф.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'<mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'test@example.com'
));
$this
->
assertTrue
(
$validator
->
validate
Value
(
'John Smith <john.smith@example.com>'
));
$this
->
assertFalse
(
$validator
->
validate
Value
(
'John Smith <example.com>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'info@örtliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
(
'Informtation <info@örtliches.de>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'Informtation info@örtliches.de'
));
$this
->
assertTrue
(
$validator
->
validate
(
'sam@рмкреатиф.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'sam@rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'5011@gmail.com'
));
$this
->
assertFalse
(
$validator
->
validate
(
'rmcreative.ru'
));
$this
->
assertTrue
(
$validator
->
validate
(
'Carsten Brandt <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'"Carsten Brandt" <mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'<mail@cebe.cc>'
));
$this
->
assertTrue
(
$validator
->
validate
(
'test@example.com'
));
$this
->
assertTrue
(
$validator
->
validate
(
'John Smith <john.smith@example.com>'
));
$this
->
assertFalse
(
$validator
->
validate
(
'John Smith <example.com>'
));
}
public
function
testValidateValueMx
()
...
...
@@ -89,12 +89,12 @@ class EmailValidatorTest extends TestCase
$validator
=
new
EmailValidator
();
$validator
->
checkDNS
=
true
;
$this
->
assertTrue
(
$validator
->
validate
Value
(
'5011@gmail.com'
));
$this
->
assertTrue
(
$validator
->
validate
(
'5011@gmail.com'
));
$validator
->
checkDNS
=
false
;
$this
->
assertTrue
(
$validator
->
validate
Value
(
'test@nonexistingsubdomain.example.com'
));
$this
->
assertTrue
(
$validator
->
validate
(
'test@nonexistingsubdomain.example.com'
));
$validator
->
checkDNS
=
true
;
$this
->
assertFalse
(
$validator
->
validate
Value
(
'test@nonexistingsubdomain.example.com'
));
$this
->
assertFalse
(
$validator
->
validate
(
'test@nonexistingsubdomain.example.com'
));
}
public
function
testValidateAttribute
()
...
...
tests/unit/framework/validators/ExistValidatorTest.php
View file @
cb4a8c76
...
...
@@ -26,7 +26,7 @@ class ExistValidatorTest extends DatabaseTestCase
{
try
{
$val
=
new
ExistValidator
();
$result
=
$val
->
validate
Value
(
'ref'
);
$result
=
$val
->
validate
(
'ref'
);
$this
->
fail
(
'Exception should have been thrown at this time'
);
}
catch
(
Exception
$e
)
{
$this
->
assertInstanceOf
(
'yii\base\InvalidConfigException'
,
$e
);
...
...
@@ -35,7 +35,7 @@ class ExistValidatorTest extends DatabaseTestCase
// combine to save the time creating a new db-fixture set (likely ~5 sec)
try
{
$val
=
new
ExistValidator
([
'className'
=>
ValidatorTestMainModel
::
className
()]);
$val
->
validate
Value
(
'ref'
);
$val
->
validate
(
'ref'
);
$this
->
fail
(
'Exception should have been thrown at this time'
);
}
catch
(
Exception
$e
)
{
$this
->
assertInstanceOf
(
'yii\base\InvalidConfigException'
,
$e
);
...
...
@@ -46,10 +46,10 @@ class ExistValidatorTest extends DatabaseTestCase
public
function
testValidateValue
()
{
$val
=
new
ExistValidator
([
'className'
=>
ValidatorTestRefModel
::
className
(),
'attributeName'
=>
'id'
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
2
));
$this
->
assertTrue
(
$val
->
validate
Value
(
5
));
$this
->
assertFalse
(
$val
->
validate
Value
(
99
));
$this
->
assertFalse
(
$val
->
validate
Value
([
'1'
]));
$this
->
assertTrue
(
$val
->
validate
(
2
));
$this
->
assertTrue
(
$val
->
validate
(
5
));
$this
->
assertFalse
(
$val
->
validate
(
99
));
$this
->
assertFalse
(
$val
->
validate
([
'1'
]));
}
public
function
testValidateAttribute
()
...
...
@@ -92,4 +92,4 @@ class ExistValidatorTest extends DatabaseTestCase
$val
->
validateAttribute
(
$m
,
'test_val'
);
$this
->
assertTrue
(
$m
->
hasErrors
(
'test_val'
));
}
}
\ No newline at end of file
}
tests/unit/framework/validators/NumberValidatorTest.php
View file @
cb4a8c76
...
...
@@ -29,84 +29,84 @@ class NumberValidatorTest extends TestCase
public
function
testValidateValueSimple
()
{
$val
=
new
NumberValidator
();
$this
->
assertTrue
(
$val
->
validate
Value
(
20
));
$this
->
assertTrue
(
$val
->
validate
Value
(
0
));
$this
->
assertTrue
(
$val
->
validate
Value
(
-
20
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'20'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
25.45
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'25,45'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'12:45'
));
$this
->
assertTrue
(
$val
->
validate
(
20
));
$this
->
assertTrue
(
$val
->
validate
(
0
));
$this
->
assertTrue
(
$val
->
validate
(
-
20
));
$this
->
assertTrue
(
$val
->
validate
(
'20'
));
$this
->
assertTrue
(
$val
->
validate
(
25.45
));
$this
->
assertFalse
(
$val
->
validate
(
'25,45'
));
$this
->
assertFalse
(
$val
->
validate
(
'12:45'
));
$val
=
new
NumberValidator
([
'integerOnly'
=>
true
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
20
));
$this
->
assertTrue
(
$val
->
validate
Value
(
0
));
$this
->
assertFalse
(
$val
->
validate
Value
(
25.45
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'20'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'25,45'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'020'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
0x14
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'0x14'
));
// todo check this
$this
->
assertTrue
(
$val
->
validate
(
20
));
$this
->
assertTrue
(
$val
->
validate
(
0
));
$this
->
assertFalse
(
$val
->
validate
(
25.45
));
$this
->
assertTrue
(
$val
->
validate
(
'20'
));
$this
->
assertFalse
(
$val
->
validate
(
'25,45'
));
$this
->
assertTrue
(
$val
->
validate
(
'020'
));
$this
->
assertTrue
(
$val
->
validate
(
0x14
));
$this
->
assertFalse
(
$val
->
validate
(
'0x14'
));
// todo check this
}
public
function
testValidateValueAdvanced
()
{
$val
=
new
NumberValidator
();
$this
->
assertTrue
(
$val
->
validate
Value
(
'-1.23'
));
// signed float
$this
->
assertTrue
(
$val
->
validate
Value
(
'-4.423e-12'
));
// signed float + exponent
$this
->
assertTrue
(
$val
->
validate
Value
(
'12E3'
));
// integer + exponent
$this
->
assertFalse
(
$val
->
validate
Value
(
'e12'
));
// just exponent
$this
->
assertFalse
(
$val
->
validate
Value
(
'-e3'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'-4.534-e-12'
));
// 'signed' exponent
$this
->
assertFalse
(
$val
->
validate
Value
(
'12.23^4'
));
// expression instead of value
$this
->
assertTrue
(
$val
->
validate
(
'-1.23'
));
// signed float
$this
->
assertTrue
(
$val
->
validate
(
'-4.423e-12'
));
// signed float + exponent
$this
->
assertTrue
(
$val
->
validate
(
'12E3'
));
// integer + exponent
$this
->
assertFalse
(
$val
->
validate
(
'e12'
));
// just exponent
$this
->
assertFalse
(
$val
->
validate
(
'-e3'
));
$this
->
assertFalse
(
$val
->
validate
(
'-4.534-e-12'
));
// 'signed' exponent
$this
->
assertFalse
(
$val
->
validate
(
'12.23^4'
));
// expression instead of value
$val
=
new
NumberValidator
([
'integerOnly'
=>
true
]);
$this
->
assertFalse
(
$val
->
validate
Value
(
'-1.23'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'-4.423e-12'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'12E3'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'e12'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'-e3'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'-4.534-e-12'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'12.23^4'
));
$this
->
assertFalse
(
$val
->
validate
(
'-1.23'
));
$this
->
assertFalse
(
$val
->
validate
(
'-4.423e-12'
));
$this
->
assertFalse
(
$val
->
validate
(
'12E3'
));
$this
->
assertFalse
(
$val
->
validate
(
'e12'
));
$this
->
assertFalse
(
$val
->
validate
(
'-e3'
));
$this
->
assertFalse
(
$val
->
validate
(
'-4.534-e-12'
));
$this
->
assertFalse
(
$val
->
validate
(
'12.23^4'
));
}
public
function
testValidateValueMin
()
{
$val
=
new
NumberValidator
([
'min'
=>
1
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
-
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'22e-12'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
PHP_INT_MAX
+
1
));
$this
->
assertTrue
(
$val
->
validate
(
1
));
$this
->
assertFalse
(
$val
->
validate
(
-
1
));
$this
->
assertFalse
(
$val
->
validate
(
'22e-12'
));
$this
->
assertTrue
(
$val
->
validate
(
PHP_INT_MAX
+
1
));
$val
=
new
NumberValidator
([
'min'
=>
1
],
[
'integerOnly'
=>
true
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
-
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'22e-12'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
PHP_INT_MAX
+
1
));
$this
->
assertTrue
(
$val
->
validate
(
1
));
$this
->
assertFalse
(
$val
->
validate
(
-
1
));
$this
->
assertFalse
(
$val
->
validate
(
'22e-12'
));
$this
->
assertTrue
(
$val
->
validate
(
PHP_INT_MAX
+
1
));
}
public
function
testValidateValueMax
()
{
$val
=
new
NumberValidator
([
'max'
=>
1.25
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
1.5
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'22e-12'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'125e-2'
));
$this
->
assertTrue
(
$val
->
validate
(
1
));
$this
->
assertFalse
(
$val
->
validate
(
1.5
));
$this
->
assertTrue
(
$val
->
validate
(
'22e-12'
));
$this
->
assertTrue
(
$val
->
validate
(
'125e-2'
));
$val
=
new
NumberValidator
([
'max'
=>
1.25
,
'integerOnly'
=>
true
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
1.5
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'22e-12'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'125e-2'
));
$this
->
assertTrue
(
$val
->
validate
(
1
));
$this
->
assertFalse
(
$val
->
validate
(
1.5
));
$this
->
assertFalse
(
$val
->
validate
(
'22e-12'
));
$this
->
assertFalse
(
$val
->
validate
(
'125e-2'
));
}
public
function
testValidateValueRange
()
{
$val
=
new
NumberValidator
([
'min'
=>
-
10
,
'max'
=>
20
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
0
));
$this
->
assertTrue
(
$val
->
validate
Value
(
-
10
));
$this
->
assertFalse
(
$val
->
validate
Value
(
-
11
));
$this
->
assertFalse
(
$val
->
validate
Value
(
21
));
$this
->
assertTrue
(
$val
->
validate
(
0
));
$this
->
assertTrue
(
$val
->
validate
(
-
10
));
$this
->
assertFalse
(
$val
->
validate
(
-
11
));
$this
->
assertFalse
(
$val
->
validate
(
21
));
$val
=
new
NumberValidator
([
'min'
=>
-
10
,
'max'
=>
20
,
'integerOnly'
=>
true
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
0
));
$this
->
assertFalse
(
$val
->
validate
Value
(
-
11
));
$this
->
assertFalse
(
$val
->
validate
Value
(
22
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'20e-1'
));
$this
->
assertTrue
(
$val
->
validate
(
0
));
$this
->
assertFalse
(
$val
->
validate
(
-
11
));
$this
->
assertFalse
(
$val
->
validate
(
22
));
$this
->
assertFalse
(
$val
->
validate
(
'20e-1'
));
}
public
function
testValidateAttribute
()
...
...
@@ -163,4 +163,4 @@ class NumberValidatorTest extends TestCase
$msgs
=
$model
->
getErrors
(
'attr_number'
);
$this
->
assertSame
(
'attr_number is to small.'
,
$msgs
[
0
]);
}
}
\ No newline at end of file
}
tests/unit/framework/validators/RangeValidatorTest.php
View file @
cb4a8c76
...
...
@@ -30,36 +30,36 @@ class RangeValidatorTest extends TestCase
public
function
testValidateValue
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
)]);
$this
->
assertTrue
(
$val
->
validate
Value
(
1
));
$this
->
assertFalse
(
$val
->
validate
Value
(
0
));
$this
->
assertFalse
(
$val
->
validate
Value
(
11
));
$this
->
assertFalse
(
$val
->
validate
Value
(
5.5
));
$this
->
assertTrue
(
$val
->
validate
Value
(
10
));
$this
->
assertTrue
(
$val
->
validate
Value
(
"10"
));
$this
->
assertTrue
(
$val
->
validate
Value
(
"5"
));
$this
->
assertTrue
(
$val
->
validate
(
1
));
$this
->
assertFalse
(
$val
->
validate
(
0
));
$this
->
assertFalse
(
$val
->
validate
(
11
));
$this
->
assertFalse
(
$val
->
validate
(
5.5
));
$this
->
assertTrue
(
$val
->
validate
(
10
));
$this
->
assertTrue
(
$val
->
validate
(
"10"
));
$this
->
assertTrue
(
$val
->
validate
(
"5"
));
}
public
function
testValidateValueStrict
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
),
'strict'
=>
true
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
1
));
$this
->
assertTrue
(
$val
->
validate
Value
(
5
));
$this
->
assertTrue
(
$val
->
validate
Value
(
10
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"1"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"10"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"5.5"
));
$this
->
assertTrue
(
$val
->
validate
(
1
));
$this
->
assertTrue
(
$val
->
validate
(
5
));
$this
->
assertTrue
(
$val
->
validate
(
10
));
$this
->
assertFalse
(
$val
->
validate
(
"1"
));
$this
->
assertFalse
(
$val
->
validate
(
"10"
));
$this
->
assertFalse
(
$val
->
validate
(
"5.5"
));
}
public
function
testValidateValueNot
()
{
$val
=
new
RangeValidator
([
'range'
=>
range
(
1
,
10
,
1
),
'not'
=>
true
]);
$this
->
assertFalse
(
$val
->
validate
Value
(
1
));
$this
->
assertTrue
(
$val
->
validate
Value
(
0
));
$this
->
assertTrue
(
$val
->
validate
Value
(
11
));
$this
->
assertTrue
(
$val
->
validate
Value
(
5.5
));
$this
->
assertFalse
(
$val
->
validate
Value
(
10
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"10"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"5"
));
$this
->
assertFalse
(
$val
->
validate
(
1
));
$this
->
assertTrue
(
$val
->
validate
(
0
));
$this
->
assertTrue
(
$val
->
validate
(
11
));
$this
->
assertTrue
(
$val
->
validate
(
5.5
));
$this
->
assertFalse
(
$val
->
validate
(
10
));
$this
->
assertFalse
(
$val
->
validate
(
"10"
));
$this
->
assertFalse
(
$val
->
validate
(
"5"
));
}
public
function
testValidateAttribute
()
...
...
@@ -73,4 +73,4 @@ class RangeValidatorTest extends TestCase
$err
=
$m
->
getErrors
(
'attr_r2'
);
$this
->
assertTrue
(
stripos
(
$err
[
0
],
'attr_r2'
)
!==
false
);
}
}
\ No newline at end of file
}
tests/unit/framework/validators/RegularExpressionValidatorTest.php
View file @
cb4a8c76
...
...
@@ -18,13 +18,13 @@ class RegularExpressionValidatorTest extends TestCase
public
function
testValidateValue
()
{
$val
=
new
RegularExpressionValidator
([
'pattern'
=>
'/^[a-zA-Z0-9](\.)?([^\/]*)$/m'
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
'b.4'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'b./'
));
$this
->
assertFalse
(
$val
->
validate
Value
([
'a'
,
'b'
]));
$this
->
assertTrue
(
$val
->
validate
(
'b.4'
));
$this
->
assertFalse
(
$val
->
validate
(
'b./'
));
$this
->
assertFalse
(
$val
->
validate
([
'a'
,
'b'
]));
$val
->
not
=
true
;
$this
->
assertFalse
(
$val
->
validate
Value
(
'b.4'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'b./'
));
$this
->
assertFalse
(
$val
->
validate
Value
([
'a'
,
'b'
]));
$this
->
assertFalse
(
$val
->
validate
(
'b.4'
));
$this
->
assertTrue
(
$val
->
validate
(
'b./'
));
$this
->
assertFalse
(
$val
->
validate
([
'a'
,
'b'
]));
}
public
function
testValidateAttribute
()
...
...
@@ -48,7 +48,7 @@ class RegularExpressionValidatorTest extends TestCase
{
$this
->
setExpectedException
(
'yii\base\InvalidConfigException'
);
$val
=
new
RegularExpressionValidator
();
$val
->
validate
Value
(
'abc'
);
$val
->
validate
(
'abc'
);
}
}
\ No newline at end of file
}
tests/unit/framework/validators/RequiredValidatorTest.php
View file @
cb4a8c76
...
...
@@ -17,26 +17,26 @@ class RequiredValidatorTest extends TestCase
public
function
testValidateValueWithDefaults
()
{
$val
=
new
RequiredValidator
();
$this
->
assertFalse
(
$val
->
validate
Value
(
null
));
$this
->
assertFalse
(
$val
->
validate
Value
([]));
$this
->
assertTrue
(
$val
->
validate
Value
(
'not empty'
));
$this
->
assertTrue
(
$val
->
validate
Value
([
'with'
,
'elements'
]));
$this
->
assertFalse
(
$val
->
validate
(
null
));
$this
->
assertFalse
(
$val
->
validate
([]));
$this
->
assertTrue
(
$val
->
validate
(
'not empty'
));
$this
->
assertTrue
(
$val
->
validate
([
'with'
,
'elements'
]));
}
public
function
testValidateValueWithValue
()
{
$val
=
new
RequiredValidator
([
'requiredValue'
=>
55
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
55
));
$this
->
assertTrue
(
$val
->
validate
Value
(
"55"
));
$this
->
assertTrue
(
$val
->
validate
Value
(
"0x37"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"should fail"
));
$this
->
assertTrue
(
$val
->
validate
Value
(
true
));
$this
->
assertTrue
(
$val
->
validate
(
55
));
$this
->
assertTrue
(
$val
->
validate
(
"55"
));
$this
->
assertTrue
(
$val
->
validate
(
"0x37"
));
$this
->
assertFalse
(
$val
->
validate
(
"should fail"
));
$this
->
assertTrue
(
$val
->
validate
(
true
));
$val
->
strict
=
true
;
$this
->
assertTrue
(
$val
->
validate
Value
(
55
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"55"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"0x37"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
"should fail"
));
$this
->
assertFalse
(
$val
->
validate
Value
(
true
));
$this
->
assertTrue
(
$val
->
validate
(
55
));
$this
->
assertFalse
(
$val
->
validate
(
"55"
));
$this
->
assertFalse
(
$val
->
validate
(
"0x37"
));
$this
->
assertFalse
(
$val
->
validate
(
"should fail"
));
$this
->
assertFalse
(
$val
->
validate
(
true
));
}
public
function
testValidateAttribute
()
...
...
@@ -57,4 +57,4 @@ class RequiredValidatorTest extends TestCase
$val
->
validateAttribute
(
$m
,
'attr_val'
);
$this
->
assertFalse
(
$m
->
hasErrors
(
'attr_val'
));
}
}
\ No newline at end of file
}
tests/unit/framework/validators/StringValidatorTest.php
View file @
cb4a8c76
...
...
@@ -18,47 +18,47 @@ class StringValidatorTest extends TestCase
public
function
testValidateValue
()
{
$val
=
new
StringValidator
();
$this
->
assertFalse
(
$val
->
validate
Value
([
'not a string'
]));
$this
->
assertTrue
(
$val
->
validate
Value
(
'Just some string'
));
$this
->
assertFalse
(
$val
->
validate
([
'not a string'
]));
$this
->
assertTrue
(
$val
->
validate
(
'Just some string'
));
}
public
function
testValidateValueLength
()
{
$val
=
new
StringValidator
([
'length'
=>
25
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
25
)));
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'€'
,
25
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
125
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
''
));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
25
)));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'€'
,
25
)));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'x'
,
125
)));
$this
->
assertFalse
(
$val
->
validate
(
''
));
$val
=
new
StringValidator
([
'length'
=>
[
25
]]);
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
25
)));
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
1250
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'Ä'
,
24
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
''
));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
25
)));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
1250
)));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'Ä'
,
24
)));
$this
->
assertFalse
(
$val
->
validate
(
''
));
$val
=
new
StringValidator
([
'length'
=>
[
10
,
20
]]);
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
15
)));
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
10
)));
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
20
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
5
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
25
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
''
));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
15
)));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
10
)));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
20
)));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'x'
,
5
)));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'x'
,
25
)));
$this
->
assertFalse
(
$val
->
validate
(
''
));
// make sure min/max are overridden
$val
=
new
StringValidator
([
'length'
=>
[
10
,
20
],
'min'
=>
25
,
'max'
=>
35
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
15
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
30
)));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
15
)));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'x'
,
30
)));
}
public
function
testValidateValueMinMax
()
{
$val
=
new
StringValidator
([
'min'
=>
10
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'x'
,
10
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
'xxxx'
));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'x'
,
10
)));
$this
->
assertFalse
(
$val
->
validate
(
'xxxx'
));
$val
=
new
StringValidator
([
'max'
=>
10
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
'xxxx'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'y'
,
20
)));
$this
->
assertTrue
(
$val
->
validate
(
'xxxx'
));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'y'
,
20
)));
$val
=
new
StringValidator
([
'min'
=>
10
,
'max'
=>
20
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
str_repeat
(
'y'
,
15
)));
$this
->
assertFalse
(
$val
->
validate
Value
(
'abc'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
str_repeat
(
'b'
,
25
)));
$this
->
assertTrue
(
$val
->
validate
(
str_repeat
(
'y'
,
15
)));
$this
->
assertFalse
(
$val
->
validate
(
'abc'
));
$this
->
assertFalse
(
$val
->
validate
(
str_repeat
(
'b'
,
25
)));
}
public
function
testValidateAttribute
()
...
...
@@ -113,4 +113,4 @@ class StringValidatorTest extends TestCase
$errorMsg
=
$model
->
getErrors
(
'attr_string'
);
$this
->
assertEquals
(
'attr_string to short. Min is 5'
,
$errorMsg
[
0
]);
}
}
\ No newline at end of file
}
tests/unit/framework/validators/UrlValidatorTest.php
View file @
cb4a8c76
...
...
@@ -18,28 +18,28 @@ class UrlValidatorTest extends TestCase
public
function
testValidateValue
()
{
$val
=
new
UrlValidator
;
$this
->
assertFalse
(
$val
->
validate
Value
(
'google.de'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'http://google.de'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'https://google.de'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'htp://yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8'
$this
->
assertFalse
(
$val
->
validate
(
'google.de'
));
$this
->
assertTrue
(
$val
->
validate
(
'http://google.de'
));
$this
->
assertTrue
(
$val
->
validate
(
'https://google.de'
));
$this
->
assertFalse
(
$val
->
validate
(
'htp://yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
(
'https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8'
.
'&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'ftp://ftp.ruhr-uni-bochum.de/'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'http://invalid,domain'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'http://äüö?=!"§$%&/()=}][{³²€.edu'
));
$this
->
assertFalse
(
$val
->
validate
(
'ftp://ftp.ruhr-uni-bochum.de/'
));
$this
->
assertFalse
(
$val
->
validate
(
'http://invalid,domain'
));
$this
->
assertFalse
(
$val
->
validate
(
'http://äüö?=!"§$%&/()=}][{³²€.edu'
));
}
public
function
testValidateValueWithDefaultScheme
()
{
$val
=
new
UrlValidator
([
'defaultScheme'
=>
'https'
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
'yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'http://yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
(
'yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
(
'http://yiiframework.com'
));
}
public
function
testValidateValueWithoutScheme
()
{
$val
=
new
UrlValidator
([
'pattern'
=>
'/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i'
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
'yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
(
'yiiframework.com'
));
}
public
function
testValidateWithCustomScheme
()
...
...
@@ -48,13 +48,13 @@ class UrlValidatorTest extends TestCase
'validSchemes'
=>
[
'http'
,
'https'
,
'ftp'
,
'ftps'
],
'defaultScheme'
=>
'http'
,
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
'ftp://ftp.ruhr-uni-bochum.de/'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'google.de'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'http://google.de'
));
$this
->
assertTrue
(
$val
->
validate
Value
(
'https://google.de'
));
$this
->
assertFalse
(
$val
->
validate
Value
(
'htp://yiiframework.com'
));
$this
->
assertTrue
(
$val
->
validate
(
'ftp://ftp.ruhr-uni-bochum.de/'
));
$this
->
assertTrue
(
$val
->
validate
(
'google.de'
));
$this
->
assertTrue
(
$val
->
validate
(
'http://google.de'
));
$this
->
assertTrue
(
$val
->
validate
(
'https://google.de'
));
$this
->
assertFalse
(
$val
->
validate
(
'htp://yiiframework.com'
));
// relative urls not supported
$this
->
assertFalse
(
$val
->
validate
Value
(
'//yiiframework.com'
));
$this
->
assertFalse
(
$val
->
validate
(
'//yiiframework.com'
));
}
public
function
testValidateWithIdn
()
...
...
@@ -66,16 +66,16 @@ class UrlValidatorTest extends TestCase
$val
=
new
UrlValidator
([
'enableIDN'
=>
true
,
]);
$this
->
assertTrue
(
$val
->
validate
Value
(
'http://äüößìà.de'
));
$this
->
assertTrue
(
$val
->
validate
(
'http://äüößìà.de'
));
// converted via http://mct.verisign-grs.com/convertServlet
$this
->
assertTrue
(
$val
->
validate
Value
(
'http://xn--zcack7ayc9a.de'
));
$this
->
assertTrue
(
$val
->
validate
(
'http://xn--zcack7ayc9a.de'
));
}
public
function
testValidateLength
()
{
$url
=
'http://'
.
str_pad
(
'base'
,
2000
,
'url'
)
.
'.de'
;
$val
=
new
UrlValidator
;
$this
->
assertFalse
(
$val
->
validate
Value
(
$url
));
$this
->
assertFalse
(
$val
->
validate
(
$url
));
}
public
function
testValidateAttributeAndError
()
...
...
tests/unit/framework/validators/ValidatorTest.php
View file @
cb4a8c76
...
...
@@ -63,7 +63,7 @@ class ValidatorTest extends TestCase
{
$val
=
new
TestValidator
([
'attributes'
=>
[
'attr_runMe1'
,
'attr_runMe2'
]]);
$model
=
$this
->
getTestModel
();
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_skip'
));
...
...
@@ -73,7 +73,7 @@ class ValidatorTest extends TestCase
{
$val
=
new
TestValidator
([
'attributes'
=>
[
'attr_runMe1'
,
'attr_runMe2'
]]);
$model
=
$this
->
getTestModel
();
$val
->
validate
(
$model
,
[
'attr_runMe1'
]);
$val
->
validate
Attributes
(
$model
,
[
'attr_runMe1'
]);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_skip'
));
...
...
@@ -83,11 +83,11 @@ class ValidatorTest extends TestCase
{
$val
=
new
TestValidator
();
$model
=
$this
->
getTestModel
();
$val
->
validate
(
$model
,
[
'attr_runMe1'
]);
$val
->
validate
Attributes
(
$model
,
[
'attr_runMe1'
]);
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_skip'
));
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_skip'
));
...
...
@@ -97,27 +97,27 @@ class ValidatorTest extends TestCase
{
$val
=
new
TestValidator
([
'attributes'
=>
[
'attr_runMe1'
,
'attr_runMe2'
],
'skipOnError'
=>
false
]);
$model
=
$this
->
getTestModel
();
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_skip'
));
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe2'
));
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe1'
));
$val
->
validate
(
$model
,
[
'attr_runMe2'
]);
$val
->
validate
Attributes
(
$model
,
[
'attr_runMe2'
]);
$this
->
assertEquals
(
2
,
$val
->
countAttributeValidations
(
'attr_runMe2'
));
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe1'
));
$this
->
assertEquals
(
0
,
$val
->
countAttributeValidations
(
'attr_skip'
));
$val
=
new
TestValidator
([
'attributes'
=>
[
'attr_runMe1'
,
'attr_runMe2'
],
'skipOnError'
=>
true
]);
$model
=
$this
->
getTestModel
();
$val
->
enableErrorOnValidateAttribute
();
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_skip'
));
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe1'
));
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe1'
));
$this
->
assertEquals
(
0
,
$val
->
countAttributeValidations
(
'attr_skip'
));
$val
->
validate
(
$model
,
[
'attr_runMe2'
]);
$val
->
validate
Attributes
(
$model
,
[
'attr_runMe2'
]);
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe2'
));
$this
->
assertEquals
(
1
,
$val
->
countAttributeValidations
(
'attr_runMe1'
));
$this
->
assertEquals
(
0
,
$val
->
countAttributeValidations
(
'attr_skip'
));
...
...
@@ -135,13 +135,13 @@ class ValidatorTest extends TestCase
'skipOnEmpty'
=>
true
,
]);
$model
=
$this
->
getTestModel
([
'attr_empty1'
=>
''
,
'attr_emtpy2'
=>
' '
]);
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_empty1'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_empty2'
));
$model
->
attr_empty1
=
'not empty anymore'
;
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_empty1'
));
$this
->
assertFalse
(
$val
->
isAttributeValidated
(
'attr_empty2'
));
$val
=
new
TestValidator
([
...
...
@@ -154,7 +154,7 @@ class ValidatorTest extends TestCase
'skipOnEmpty'
=>
false
,
]);
$model
=
$this
->
getTestModel
([
'attr_empty1'
=>
''
,
'attr_emtpy2'
=>
' '
]);
$val
->
validate
(
$model
);
$val
->
validate
Attributes
(
$model
);
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe1'
));
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_runMe2'
));
$this
->
assertTrue
(
$val
->
isAttributeValidated
(
'attr_empty1'
));
...
...
@@ -188,7 +188,7 @@ class ValidatorTest extends TestCase
TestValidator
::
className
()
.
' does not support validateValue().'
);
$val
=
new
TestValidator
();
$val
->
validate
Value
(
'abc'
);
$val
->
validate
(
'abc'
);
}
public
function
testClientValidateAttribute
()
...
...
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