Permalink
Browse files

* (Fixes issue 2069) Added CDateValidator

  • Loading branch information...
1 parent 03d09cd commit b03d541f7ac2c16a310f5ebcc1f36ab9f229bfac qiang.xue committed Mar 12, 2011
View
@@ -61,6 +61,7 @@ Version 1.1.7 to be released
- Chg #2148: Upgraded jQuery to version 1.5.1 (Sam Dark)
- Chg #2163: CConsoleCommand::usageError() will now exit with error code 1 (Qiang)
- Chg: jQuery UI now uses minified CSS (Sam Dark)
+- New #2069: Added CDateValidator (Qiang)
- New: Added support for query caching (Qiang)
Version 1.1.6 January 16, 2011
@@ -139,6 +139,9 @@ equal to another attribute or constant.
- `email`: alias of [CEmailValidator], ensuring the attribute is a
valid email address.
+ - `date`: alias of [CDateValidator], ensuring the attribute represents
+a valid date, time, or datetime value.
+
- `default`: alias of [CDefaultValueValidator], assigning a default value
to the specified attributes.
View
@@ -701,6 +701,7 @@ public static function registerAutoloader($callback)
'CBooleanValidator' => '/validators/CBooleanValidator.php',
'CCaptchaValidator' => '/validators/CCaptchaValidator.php',
'CCompareValidator' => '/validators/CCompareValidator.php',
+ 'CDateValidator' => '/validators/CDateValidator.php',
'CDefaultValueValidator' => '/validators/CDefaultValueValidator.php',
'CEmailValidator' => '/validators/CEmailValidator.php',
'CExistValidator' => '/validators/CExistValidator.php',
@@ -0,0 +1,76 @@
+<?php
+/**
+ * CDateValidator class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CDateValidator verifies if the attribute represents a date, time or datetime.
+ *
+ * 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>
+ * @version $Id: CDateValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $
+ * @package system.validators
+ * @since 1.1.7
+ */
+class CDateValidator extends CValidator
+{
+ /**
+ * @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.
+ * @param CModel $object the object being validated
+ * @param string $attribute the attribute being validated
+ */
+ protected function validateAttribute($object,$attribute)
+ {
+ $value=$object->$attribute;
+ if($this->allowEmpty && $this->isEmpty($value))
+ return;
+
+ $formats=is_string($this->format) ? array($this->format) : $this->format;
+ $valid=false;
+ 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.');
+ $this->addError($object,$attribute,$message);
+ }
+ }
+}
+
@@ -26,6 +26,9 @@
* will be used to determine how to parse the date string. If the given date
* value doesn't follow the format, the attribute is considered as invalid.
*
+ * Starting from version 1.1.7, we have a dedicated date validator {@link CDateValidator}.
+ * Please consider using this validator to validate a date-typed value.
+ *
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.validators
@@ -44,6 +44,7 @@
* <li>default: {@link CDefaultValueValidator}</li>
* <li>exist: {@link CExistValidator}</li>
* <li>boolean: {@link CBooleanValidator}</li>
+ * <li>date: {@link CDateValidator}</li>
* <li>safe: {@link CSafeValidator}</li>
* <li>unsafe: {@link CUnsafeValidator}</li>
* </ul>
@@ -77,6 +78,7 @@
'boolean'=>'CBooleanValidator',
'safe'=>'CSafeValidator',
'unsafe'=>'CUnsafeValidator',
+ 'date'=>'CDateValidator',
);
/**
Oops, something went wrong.

0 comments on commit b03d541

Please sign in to comment.