Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
d8d0ddfa
Commit
d8d0ddfa
authored
Aug 08, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring the validator class.
parent
c145e6cb
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
29 deletions
+13
-29
system/validator.php
+13
-29
No files found.
system/validator.php
View file @
d8d0ddfa
...
...
@@ -60,8 +60,8 @@ class Validator {
}
$this
->
attributes
=
$attributes
;
$this
->
rules
=
$rules
;
$this
->
messages
=
$messages
;
$this
->
rules
=
$rules
;
}
/**
...
...
@@ -125,10 +125,7 @@ class Validator {
// No validation will be run for attributes that do not exist unless the rule being validated
// is "required" or "accepted". No other rules have implicit "required" checks.
if
(
!
static
::
validate_required
(
$attribute
)
and
!
in_array
(
$rule
,
array
(
'required'
,
'accepted'
)))
{
return
;
}
if
(
!
static
::
validate_required
(
$attribute
)
and
!
in_array
(
$rule
,
array
(
'required'
,
'accepted'
)))
return
;
if
(
!
$this
->
$validator
(
$attribute
,
$parameters
))
{
...
...
@@ -144,15 +141,9 @@ class Validator {
*/
protected
function
validate_required
(
$attribute
)
{
if
(
!
array_key_exists
(
$attribute
,
$this
->
attributes
))
{
return
false
;
}
if
(
!
array_key_exists
(
$attribute
,
$this
->
attributes
))
return
false
;
if
(
is_string
(
$this
->
attributes
[
$attribute
])
and
trim
(
$this
->
attributes
[
$attribute
])
===
''
)
{
return
false
;
}
if
(
is_string
(
$this
->
attributes
[
$attribute
])
and
trim
(
$this
->
attributes
[
$attribute
])
===
''
)
return
false
;
return
true
;
}
...
...
@@ -259,12 +250,9 @@ class Validator {
*/
protected
function
get_size
(
$attribute
)
{
if
(
is_numeric
(
$this
->
attributes
[
$attribute
]))
{
return
$this
->
attributes
[
$attribute
];
}
if
(
is_numeric
(
$this
->
attributes
[
$attribute
]))
return
$this
->
attributes
[
$attribute
];
return
(
array_key_exists
(
$attribute
,
$_FILES
))
?
$this
->
attributes
[
$attribute
][
'size'
]
/
10
00
:
Str
::
length
(
trim
(
$this
->
attributes
[
$attribute
]));
return
(
array_key_exists
(
$attribute
,
$_FILES
))
?
$this
->
attributes
[
$attribute
][
'size'
]
/
10
24
:
Str
::
length
(
trim
(
$this
->
attributes
[
$attribute
]));
}
/**
...
...
@@ -294,16 +282,15 @@ class Validator {
/**
* Validate the uniqueness of an attribute value on a given database table.
*
* If a database column is not specified, the attribute name will be used.
*
* @param string $attribute
* @param array $parameters
* @return bool
*/
protected
function
validate_unique
(
$attribute
,
$parameters
)
{
if
(
!
isset
(
$parameters
[
1
]))
{
$parameters
[
1
]
=
$attribute
;
}
if
(
!
isset
(
$parameters
[
1
]))
$parameters
[
1
]
=
$attribute
;
return
DB\Manager
::
connection
()
->
table
(
$parameters
[
0
])
->
where
(
$parameters
[
1
],
'='
,
$this
->
attributes
[
$attribute
])
->
count
()
==
0
;
}
...
...
@@ -398,10 +385,7 @@ class Validator {
{
foreach
(
$parameters
as
$extension
)
{
if
(
File
::
is
(
$extension
,
$this
->
attributes
[
$attribute
][
'tmp_name'
]))
{
return
true
;
}
if
(
File
::
is
(
$extension
,
$this
->
attributes
[
$attribute
][
'tmp_name'
]))
return
true
;
}
return
false
;
...
...
@@ -435,7 +419,7 @@ class Validator {
$message
=
Lang
::
line
(
'validation.'
.
$rule
)
->
get
(
$this
->
language
);
// For "size" rules that are validating strings or files, we need to adjust
// the default error message
appropriately
.
// the default error message
for the appropriate type
.
if
(
in_array
(
$rule
,
$this
->
size_rules
)
and
!
is_numeric
(
$this
->
attributes
[
$attribute
]))
{
return
(
array_key_exists
(
$attribute
,
$_FILES
))
...
...
@@ -458,7 +442,7 @@ class Validator {
*/
protected
function
format_message
(
$message
,
$attribute
,
$rule
,
$parameters
)
{
$display
=
Lang
::
line
(
'attributes.'
.
$attribute
)
->
get
(
$this
->
language
,
function
()
use
(
$attribute
)
{
return
str_replace
(
'_'
,
' '
,
$attribute
);
}
);
$display
=
Lang
::
line
(
'attributes.'
.
$attribute
)
->
get
(
$this
->
language
,
str_replace
(
'_'
,
' '
,
$attribute
)
);
$message
=
str_replace
(
':attribute'
,
$display
,
$message
);
...
...
@@ -466,7 +450,7 @@ class Validator {
{
$max
=
(
$rule
==
'between'
)
?
$parameters
[
1
]
:
$parameters
[
0
];
$message
=
str_replace
(
':size'
,
$parameters
[
0
],
str_replace
(
':min'
,
$parameters
[
0
],
str_replace
(
':max'
,
$max
,
$message
))
);
$message
=
str_replace
(
array
(
':size'
,
':min'
,
':max'
),
array
(
$parameters
[
0
],
$parameters
[
0
],
$max
),
$message
);
}
elseif
(
in_array
(
$rule
,
array
(
'in'
,
'not_in'
,
'mimes'
)))
{
...
...
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