DateValidator.php 2.28 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;

w  
Qiang Xue committed
10
/**
Alexander Makarov committed
11
 * DateValidator verifies if the attribute represents a date, time or datetime.
w  
Qiang Xue committed
12 13 14 15 16
 *
 * 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>
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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
{
	/**
	 * @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.
	 */
	public $allowEmpty = true;
	/**
	 * @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;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
43
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
44 45
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
46
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
47 48
	{
		$value = $object->$attribute;
Alexander Makarov committed
49
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
50
			return;
Alexander Makarov committed
51
		}
w  
Qiang Xue committed
52 53 54

		$formats = is_string($this->format) ? array($this->format) : $this->format;
		$valid = false;
Alexander Makarov committed
55
		foreach ($formats as $format) {
w  
Qiang Xue committed
56
			$timestamp = CDateTimeParser::parse($value, $format, array('month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0, 'second' => 0));
Alexander Makarov committed
57
			if ($timestamp !== false) {
w  
Qiang Xue committed
58
				$valid = true;
Alexander Makarov committed
59
				if ($this->timestampAttribute !== null) {
w  
Qiang Xue committed
60
					$object-> {$this->timestampAttribute} = $timestamp;
Alexander Makarov committed
61
				}
w  
Qiang Xue committed
62 63 64 65
				break;
			}
		}

Alexander Makarov committed
66
		if (!$valid) {
67
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii|The format of {attribute} is invalid.');
w  
Qiang Xue committed
68 69 70 71 72
			$this->addError($object, $attribute, $message);
		}
	}
}