DateValidator.php 1.82 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

Qiang Xue committed
10 11 12
use Yii;
use DateTime;

w  
Qiang Xue committed
13
/**
Qiang Xue committed
14
 * DateValidator verifies if the attribute represents a date, time or datetime in a proper format.
w  
Qiang Xue committed
15 16
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Alexander Makarov committed
17
 * @since 2.0
w  
Qiang Xue committed
18
 */
Alexander Makarov committed
19
class DateValidator extends Validator
w  
Qiang Xue committed
20 21
{
	/**
Qiang Xue committed
22
	 * @var string the date format that the value being validated should follow.
Carsten Brandt committed
23
	 * Please refer to <http://www.php.net/manual/en/datetime.createfromformat.php> on
Qiang Xue committed
24
	 * supported formats.
w  
Qiang Xue committed
25
	 */
Qiang Xue committed
26
	public $format = 'Y-m-d';
w  
Qiang Xue committed
27 28 29 30 31 32 33
	/**
	 * @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
	 * receive the parsing result.
	 */
	public $timestampAttribute;

Qiang Xue committed
34
	/**
Qiang Xue committed
35
	 * @inheritdoc
Qiang Xue committed
36 37 38 39 40
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
41
			$this->message = Yii::t('yii', 'The format of {attribute} is invalid.');
Qiang Xue committed
42 43 44
		}
	}

w  
Qiang Xue committed
45
	/**
Qiang Xue committed
46
	 * @inheritdoc
w  
Qiang Xue committed
47
	 */
w  
Qiang Xue committed
48
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
49 50
	{
		$value = $object->$attribute;
Qiang Xue committed
51 52 53
		$result = $this->validateValue($value);
		if (!empty($result)) {
			$this->addError($object, $attribute, $result[0], $result[1]);
Qiang Xue committed
54
		} elseif ($this->timestampAttribute !== null) {
Qiang Xue committed
55
			$date = DateTime::createFromFormat($this->format, $value);
Qiang Xue committed
56
			$object->{$this->timestampAttribute} = $date->getTimestamp();
w  
Qiang Xue committed
57 58
		}
	}
Qiang Xue committed
59 60

	/**
Qiang Xue committed
61
	 * @inheritdoc
Qiang Xue committed
62
	 */
Qiang Xue committed
63
	protected function validateValue($value)
Qiang Xue committed
64
	{
Qiang Xue committed
65 66 67 68
		if (is_array($value)) {
			return [$this->message, []];
		}
		$date = DateTime::createFromFormat($this->format, $value);
69
		$errors = DateTime::getLastErrors();
Qiang Xue committed
70 71
		$invalid = $date === false || $errors['error_count'] || $errors['warning_count'];
		return $invalid ? [$this->message, []] : null;
Qiang Xue committed
72
	}
w  
Qiang Xue committed
73
}