* DateValidator verifies if the attribute represents a date, time or datetime in a proper format.
* DateValidator verifies if the attribute represents a date, time or datetime in a proper format.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
* @since 2.0
*/
*/
classDateValidatorextendsValidator
classDateValidatorextendsValidator
{
{
/**
/**
* @var string the date format that the value being validated should follow.
* @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
* This can be a date time pattern as described in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax).
* supported formats.
*
* Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the PHP Datetime class.
* Please refer to <http://php.net/manual/en/datetime.createfromformat.php> on supported formats.
*
* If this property is not set, the default value will be obtained from `Yii::$app->formatter->dateFormat`, see [[\yii\i18n\Formatter::dateFormat]] for details.
*
* Here are some example values:
*
* ```php
* 'MM/dd/yyyy' // date in ICU format
* 'php:m/d/Y' // the same date in PHP format
* ```
*/
public$format;
/**
* @var string the locale ID that is used to localize the date parsing.
* This is only effective when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed.
* If not set, the locale of the [[\yii\base\Application::formatter|formatter]] will be used.
* See also [[\yii\i18n\Formatter::locale]].
*/
*/
public$format='Y-m-d';
public$locale;
/**
* @var string the timezone to use for parsing date and time values.
* This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php)
* e.g. `UTC`, `Europe/Berlin` or `America/Chicago`.
* Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
* If this property is not set, [[\yii\base\Application::timeZone]] will be used.
*/
public$timeZone;
/**
/**
* @var string the name of the attribute to receive the parsing result.
* @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
* When this property is not null and the validation is successful, the named attribute will
...
@@ -31,6 +60,16 @@ class DateValidator extends Validator
...
@@ -31,6 +60,16 @@ class DateValidator extends Validator
*/
*/
public$timestampAttribute;
public$timestampAttribute;
/**
* @var array map of short format names to IntlDateFormatter constant values.
*/
private$_dateFormats=[
'short'=>3,// IntlDateFormatter::SHORT,
'medium'=>2,// IntlDateFormatter::MEDIUM,
'long'=>1,// IntlDateFormatter::LONG,
'full'=>0,// IntlDateFormatter::FULL,
];
/**
/**
* @inheritdoc
* @inheritdoc
...
@@ -41,6 +80,15 @@ class DateValidator extends Validator
...
@@ -41,6 +80,15 @@ class DateValidator extends Validator
if($this->message===null){
if($this->message===null){
$this->message=Yii::t('yii','The format of {attribute} is invalid.');
$this->message=Yii::t('yii','The format of {attribute} is invalid.');
}
}
if($this->format===null){
$this->format=Yii::$app->formatter->dateFormat;
}
if($this->locale===null){
$this->locale=Yii::$app->language;
}
if($this->timeZone===null){
$this->timeZone=Yii::$app->timeZone;
}
}
}
/**
/**
...
@@ -49,12 +97,11 @@ class DateValidator extends Validator
...
@@ -49,12 +97,11 @@ class DateValidator extends Validator