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
a23f2334
Commit
a23f2334
authored
Jul 31, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enhanced the usage of StringValidator.
parent
4e1264ef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
14 deletions
+30
-14
model.md
docs/guide/model.md
+2
-2
StringValidator.php
framework/yii/validators/StringValidator.php
+28
-12
No files found.
docs/guide/model.md
View file @
a23f2334
...
...
@@ -237,8 +237,8 @@ function rules()
{
return
array
(
// rule applied when corresponding field is "safe"
array
(
'username'
,
'string'
,
'
min'
=>
2
),
array
(
'first_name'
,
'string'
,
'm
in'
=>
2
),
array
(
'username'
,
'string'
,
'
length'
=>
array
(
4
,
32
)
),
array
(
'first_name'
,
'string'
,
'm
ax'
=>
128
),
array
(
'password'
,
'required'
),
// rule applied when scenario is "signup" no matter if field is "safe" or not
...
...
framework/yii/validators/StringValidator.php
View file @
a23f2334
...
...
@@ -21,17 +21,24 @@ use yii\helpers\Html;
class
StringValidator
extends
Validator
{
/**
* @var integer maximum length. Defaults to null, meaning no maximum limit.
* @var integer|array specifies the length limit of the value to be validated.
* This can be specified in one of the following forms:
*
* - an integer: the exact length that the value should be of;
* - an array of one element: the minimum length that the value should be of. For example, `array(8)`.
* This will overwrite [[min]].
* - an array of two elements: the minimum and maximum lengths that the value should be of.
* For example, `array(8, 128)`. This will overwrite both [[min]] and [[max]].
*/
public
$
max
;
public
$
length
;
/**
* @var integer m
inimum length. Defaults to null, meaning no minimum
limit.
* @var integer m
aximum length. If not set, it means no maximum length
limit.
*/
public
$m
in
;
public
$m
ax
;
/**
* @var integer
exact length. Defaults to null, meaning no exact
length limit.
* @var integer
minimum length. If not set, it means no minimum
length limit.
*/
public
$
is
;
public
$
min
;
/**
* @var string user-defined error message used when the value is not a string
*/
...
...
@@ -61,6 +68,15 @@ class StringValidator extends Validator
public
function
init
()
{
parent
::
init
();
if
(
is_array
(
$this
->
length
))
{
if
(
isset
(
$this
->
length
[
0
]))
{
$this
->
min
=
$this
->
length
[
0
];
}
if
(
isset
(
$this
->
length
[
1
]))
{
$this
->
max
=
$this
->
length
[
1
];
}
$this
->
length
=
null
;
}
if
(
$this
->
encoding
===
null
)
{
$this
->
encoding
=
Yii
::
$app
->
charset
;
}
...
...
@@ -73,7 +89,7 @@ class StringValidator extends Validator
if
(
$this
->
max
!==
null
&&
$this
->
tooLong
===
null
)
{
$this
->
tooLong
=
Yii
::
t
(
'yii'
,
'{attribute} should contain at most {max} characters.'
);
}
if
(
$this
->
is
!==
null
&&
$this
->
notEqual
===
null
)
{
if
(
$this
->
length
!==
null
&&
$this
->
notEqual
===
null
)
{
$this
->
notEqual
=
Yii
::
t
(
'yii'
,
'{attribute} should contain {length} characters.'
);
}
}
...
...
@@ -101,8 +117,8 @@ class StringValidator extends Validator
if
(
$this
->
max
!==
null
&&
$length
>
$this
->
max
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooLong
,
array
(
'{max}'
=>
$this
->
max
));
}
if
(
$this
->
is
!==
null
&&
$length
!==
$this
->
is
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notEqual
,
array
(
'{length}'
=>
$this
->
is
));
if
(
$this
->
length
!==
null
&&
$length
!==
$this
->
length
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notEqual
,
array
(
'{length}'
=>
$this
->
length
));
}
}
...
...
@@ -119,7 +135,7 @@ class StringValidator extends Validator
$length
=
mb_strlen
(
$value
,
$this
->
encoding
);
return
(
$this
->
min
===
null
||
$length
>=
$this
->
min
)
&&
(
$this
->
max
===
null
||
$length
<=
$this
->
max
)
&&
(
$this
->
is
===
null
||
$length
===
$this
->
is
);
&&
(
$this
->
length
===
null
||
$length
===
$this
->
length
);
}
/**
...
...
@@ -158,8 +174,8 @@ class StringValidator extends Validator
'{max}'
=>
$this
->
max
,
)));
}
if
(
$this
->
is
!==
null
)
{
$options
[
'is'
]
=
$this
->
is
;
if
(
$this
->
length
!==
null
)
{
$options
[
'is'
]
=
$this
->
length
;
$options
[
'notEqual'
]
=
Html
::
encode
(
strtr
(
$this
->
notEqual
,
array
(
'{attribute}'
=>
$label
,
'{value}'
=>
$value
,
...
...
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