Commit 86f947e9 by Qiang Xue

Finished DateValidator.

parent bb5b6a41
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
namespace yii\validators; namespace yii\validators;
use Yii;
use DateTime;
/** /**
* DateValidator verifies if the attribute represents a date, time or datetime. * DateValidator verifies if the attribute represents a date, time or datetime in a proper format.
*
* 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.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
...@@ -19,17 +19,11 @@ namespace yii\validators; ...@@ -19,17 +19,11 @@ namespace yii\validators;
class DateValidator extends Validator class DateValidator extends Validator
{ {
/** /**
* @var mixed the format pattern that the date value should follow. * @var string the date format that the value being validated should follow.
* This can be either a string or an array representing multiple formats. * Please refer to [[http://www.php.net/manual/en/datetime.createfromformat.php]] on
* Defaults to 'MM/dd/yyyy'. Please see {@link CDateTimeParser} for details * supported formats.
* 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.
*/ */
public $allowEmpty = true; public $format = 'Y-m-d';
/** /**
* @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
...@@ -46,27 +40,23 @@ class DateValidator extends Validator ...@@ -46,27 +40,23 @@ class DateValidator extends Validator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) { $date = DateTime::createFromFormat($this->format, $value);
return; if ($date === false) {
} $message = $this->message !== null ? $this->message : Yii::t('yii|The format of {attribute} is invalid.');
$this->addError($object, $attribute, $message);
$formats = is_string($this->format) ? array($this->format) : $this->format; } elseif ($this->timestampAttribute !== false) {
$valid = false; $object->{$this->timestampAttribute} = $date->getTimestamp();
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;
} }
} }
if (!$valid) { /**
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|The format of {attribute} is invalid.'); * Validates the given value.
$this->addError($object, $attribute, $message); * @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;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment