CDateValidator.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * CDateValidator class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CDateValidator verifies if the attribute represents a date, time or datetime.
  12. *
  13. * By setting the {@link format} property, one can specify what format the date value
  14. * must be in. If the given date value doesn't follow the format, the attribute is considered as invalid.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @version $Id: CDateValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $
  18. * @package system.validators
  19. * @since 1.1.7
  20. */
  21. class CDateValidator extends CValidator
  22. {
  23. /**
  24. * @var mixed the format pattern that the date value should follow.
  25. * This can be either a string or an array representing multiple formats.
  26. * Defaults to 'MM/dd/yyyy'. Please see {@link CDateTimeParser} for details
  27. * about how to specify a date format.
  28. */
  29. public $format='MM/dd/yyyy';
  30. /**
  31. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  32. * meaning that if the attribute is empty, it is considered valid.
  33. */
  34. public $allowEmpty=true;
  35. /**
  36. * @var string the name of the attribute to receive the parsing result.
  37. * When this property is not null and the validation is successful, the named attribute will
  38. * receive the parsing result.
  39. */
  40. public $timestampAttribute;
  41. /**
  42. * Validates the attribute of the object.
  43. * If there is any error, the error message is added to the object.
  44. * @param CModel $object the object being validated
  45. * @param string $attribute the attribute being validated
  46. */
  47. protected function validateAttribute($object,$attribute)
  48. {
  49. $value=$object->$attribute;
  50. if($this->allowEmpty && $this->isEmpty($value))
  51. return;
  52. $valid=false;
  53. // reason of array checking is explained here: https://github.com/yiisoft/yii/issues/1955
  54. if(!is_array($value))
  55. {
  56. $formats=is_string($this->format) ? array($this->format) : $this->format;
  57. foreach($formats as $format)
  58. {
  59. $timestamp=CDateTimeParser::parse($value,$format,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0));
  60. if($timestamp!==false)
  61. {
  62. $valid=true;
  63. if($this->timestampAttribute!==null)
  64. $object->{$this->timestampAttribute}=$timestamp;
  65. break;
  66. }
  67. }
  68. }
  69. if(!$valid)
  70. {
  71. $message=$this->message!==null?$this->message : Yii::t('yii','The format of {attribute} is invalid.');
  72. $this->addError($object,$attribute,$message);
  73. }
  74. }
  75. }