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
86f947e9
Commit
86f947e9
authored
Apr 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished DateValidator.
parent
bb5b6a41
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
32 deletions
+22
-32
DateValidator.php
framework/validators/DateValidator.php
+22
-32
No files found.
framework/validators/DateValidator.php
View file @
86f947e9
...
...
@@ -7,11 +7,11 @@
namespace
yii\validators
;
use
Yii
;
use
DateTime
;
/**
* DateValidator verifies if the attribute represents a date, time or datetime.
*
* By setting the {@link format} property, one can specify what format the date value
* must be in. If the given date value doesn't follow the format, the attribute is considered as invalid.
* DateValidator verifies if the attribute represents a date, time or datetime in a proper format.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
...
...
@@ -19,17 +19,11 @@ namespace yii\validators;
class
DateValidator
extends
Validator
{
/**
* @var mixed the format pattern that the date value should follow.
* This can be either a string or an array representing multiple formats.
* Defaults to 'MM/dd/yyyy'. Please see {@link CDateTimeParser} for details
* about how to specify a date format.
*/
public
$format
=
'MM/dd/yyyy'
;
/**
* @var boolean whether the attribute value can be null or empty. Defaults to true,
* meaning that if the attribute is empty, it is considered valid.
* @var string the date format that the value being validated should follow.
* Please refer to [[http://www.php.net/manual/en/datetime.createfromformat.php]] on
* supported formats.
*/
public
$
allowEmpty
=
true
;
public
$
format
=
'Y-m-d'
;
/**
* @var string the name of the attribute to receive the parsing result.
* When this property is not null and the validation is successful, the named attribute will
...
...
@@ -46,27 +40,23 @@ class DateValidator extends Validator
public
function
validateAttribute
(
$object
,
$attribute
)
{
$value
=
$object
->
$attribute
;
if
(
$this
->
allowEmpty
&&
$this
->
isEmpty
(
$value
))
{
return
;
}
$formats
=
is_string
(
$this
->
format
)
?
array
(
$this
->
format
)
:
$this
->
format
;
$valid
=
false
;
foreach
(
$formats
as
$format
)
{
$timestamp
=
CDateTimeParser
::
parse
(
$value
,
$format
,
array
(
'month'
=>
1
,
'day'
=>
1
,
'hour'
=>
0
,
'minute'
=>
0
,
'second'
=>
0
));
if
(
$timestamp
!==
false
)
{
$valid
=
true
;
if
(
$this
->
timestampAttribute
!==
null
)
{
$object
->
{
$this
->
timestampAttribute
}
=
$timestamp
;
}
break
;
$date
=
DateTime
::
createFromFormat
(
$this
->
format
,
$value
);
if
(
$date
===
false
)
{
$message
=
$this
->
message
!==
null
?
$this
->
message
:
Yii
::
t
(
'yii|The format of {attribute} is invalid.'
);
$this
->
addError
(
$object
,
$attribute
,
$message
);
}
elseif
(
$this
->
timestampAttribute
!==
false
)
{
$object
->
{
$this
->
timestampAttribute
}
=
$date
->
getTimestamp
();
}
}
if
(
!
$valid
)
{
$message
=
(
$this
->
message
!==
null
)
?
$this
->
message
:
\Yii
::
t
(
'yii|The format of {attribute} is invalid.'
);
$this
->
addError
(
$object
,
$attribute
,
$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
DateTime
::
createFromFormat
(
$this
->
format
,
$value
)
!==
false
;
}
}
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