DateValidator.php 2.08 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 23 24
	 * @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.
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 35 36 37 38 39 40
	/**
	 * Initializes the validator.
	 */
	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 46 47
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
48
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
49 50
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
51
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
52 53
	{
		$value = $object->$attribute;
Qiang Xue committed
54 55 56 57
		if (is_array($value)) {
			$this->addError($object, $attribute, $this->message);
			return;
		}
Qiang Xue committed
58 59
		$date = DateTime::createFromFormat($this->format, $value);
		if ($date === false) {
Qiang Xue committed
60
			$this->addError($object, $attribute, $this->message);
Qiang Xue committed
61
		} elseif ($this->timestampAttribute !== null) {
Qiang Xue committed
62
			$object->{$this->timestampAttribute} = $date->getTimestamp();
w  
Qiang Xue committed
63 64
		}
	}
Qiang Xue committed
65 66 67 68 69 70 71 72

	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
73 74 75
		DateTime::createFromFormat($this->format, $value);
		$errors = DateTime::getLastErrors();
		return $errors['error_count'] === 0 && $errors['warning_count'] === 0;
Qiang Xue committed
76
	}
w  
Qiang Xue committed
77
}