DateValidator.php 2.31 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
Alexander Makarov committed
3
 * DateValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

w  
Qiang Xue committed
12
/**
Alexander Makarov committed
13
 * DateValidator verifies if the attribute represents a date, time or datetime.
w  
Qiang Xue committed
14 15 16 17 18
 *
 * 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
19
 * @since 2.0
w  
Qiang Xue committed
20
 */
Alexander Makarov committed
21
class DateValidator extends Validator
w  
Qiang Xue committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
	/**
	 * @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
45
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
46 47
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
48
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
49 50
	{
		$value = $object->$attribute;
Alexander Makarov committed
51
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
52
			return;
Alexander Makarov committed
53
		}
w  
Qiang Xue committed
54 55 56

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

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