public
Last active

ZF: Date GreaterThan & LessThan Validators

  • Download Gist
My/Form.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
// ...
 
$this->addElementPrefixPath('My_Validate', 'My/Validate/', 'validate');
$this->addElement('text', 'start_time', array(
'label' => 'Start Time (GMT)',
'class' => 'datetime',
'required' => true,
'validators' => array(
array('Date_LessThan', false, array(
'date' => 'end_time',
'format' => 'YYYY-MM-dd HH:mm:ss'
))
)
));
 
$this->addElement('text', 'end_time', array(
'label' => 'End Time (GMT)',
'class' => 'datetime',
'required' => true,
'validators' => array(
array('Date_GreaterThan', false, array(
'date' => 'start_time',
'format' => 'YYYY-MM-dd HH:mm:ss'
))
)
));
 
// ...
My/Validate/Date/Abstract.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
<?php
class My_Validate_Date_Abstract extends Zend_Validate_Abstract
{
// Zend_Validate_Date Message Constants
const INVALID = 'dateInvalid';
const NOT_YYYY_MM_DD = 'dateNotYYYY-MM-DD';
const INVALID_DATE = 'dateInvalidDate';
const FALSEFORMAT = 'dateFalseFormat';
 
// Zend_Validate_Date Custom Message Constants
const FIELD_INVALID = 'dateInvalidField';
const FIELD_NOT_YYYY_MM_DD = 'dateNotYYYY-MM-DDField';
const FIELD_INVALID_DATE = 'dateInvalidDateField';
const FIELD_FALSEFORMAT = 'dateFalseFormatField';
 
/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = array(
// Zend_Validate_Date Messages
self::INVALID => "Invalid type given, value should be string, integer, array or Zend_Date",
self::NOT_YYYY_MM_DD => "'%value%' is not of the format YYYY-MM-DD",
self::INVALID_DATE => "'%value%' does not appear to be a valid date",
self::FALSEFORMAT => "'%value%' does not fit given date format",
 
// Zend_Validate_Date Custom Messages
self::FIELD_INVALID => "Invalid type given, date should be string, integer, array or Zend_Date",
self::FIELD_NOT_YYYY_MM_DD => "'%date%' is not of the format YYYY-MM-DD",
self::FIELD_INVALID_DATE => "'%date%' does not appear to be a valid date",
self::FIELD_FALSEFORMAT => "'%date%' does not fit given date format"
);
 
/**
* Validation failure message variable mappings
*
* @var array
*/
protected $_messageVariables = array(
'date' => '_date',
'format' => '_format',
'locale' => '_locale'
);
 
/**
* Date value used to compare. When specified as a string, it can either be
* a date (to be converted to Zend_Date) or a field name to lookup in the
* context of the isValid method.
*
* @var string|Zend_Date
*/
protected $_date;
 
/**
* Optional format
*
* @var string|null
*/
protected $_format;
 
/**
* Optional locale
*
* @var string|Zend_Locale|null
*/
protected $_locale;
 
/**
* Optional or equal which allows us to say less than or equal and vice
* versa.
*
* @var boolean false
*/
protected $_orEqual;
 
/**
* Sets validator options
*
* @param string|Zend_Date $date
* @param string $format OPTIONAL
* @param string|Zend_Locale $locale OPTIONAL
* @param boolean $orEqual OPTIONAL
* @return void
*/
public function __construct($date, $format = null, $locale = null,
$orEqual = false)
{
$this->setDate($date);
 
$this->setFormat($format);
 
if ($locale === null) {
require_once 'Zend/Registry.php';
if (Zend_Registry::isRegistered('Zend_Locale')) {
$locale = Zend_Registry::get('Zend_Locale');
}
}
 
if ($locale !== null) {
$this->setLocale($locale);
}
 
$this->setOrEqual($orEqual);
}
 
/**
* Returns the date option
*
* @return string|Zend_Date
*/
public function getDate()
{
return $this->_date;
}
 
/**
* Sets the date option
*
* @param string|Zend_Locale $date
* @return My_Validate_Date_Abstract
*/
public function setDate($date)
{
require_once 'Zend/Date.php';
if (!$date instanceof Zend_Date) {
if (Zend_Date::isDate($date, $this->_format, $this->_locale)) {
$date = new Zend_Date($date, $this->_format, $this->_locale);
}
}
 
$this->_date = $date;
 
return $this;
}
 
/**
* Returns the locale option
*
* @return string|Zend_Locale|null
*/
public function getLocale()
{
return $this->_locale;
}
 
/**
* Sets the locale option
*
* @param string|Zend_Locale $locale
* @return My_Validate_Date_Abstract
*/
public function setLocale($locale = null)
{
require_once 'Zend/Locale.php';
$this->_locale = Zend_Locale::findLocale($locale);
 
if ($this->_date instanceof Zend_Date)
$this->_date->setLocale($this->_locale);
 
return $this;
}
 
/**
* Returns the format option
*
* @return string|null
*/
public function getFormat()
{
return $this->_format;
}
 
/**
* Sets the format option
*
* @param string $format
* @return My_Validate_Date_Abstract
*/
public function setFormat($format = null)
{
$this->_format = $format;
return $this;
}
 
/**
* Sets the orEqual option
* @return My_Validate_Date_Abstract
* @param boolean $orEqual[optional]
*/
public function setOrEqual($orEqual = false)
{
$this->_orEqual = $orEqual;
return $this;
}
 
/**
* Returns the orEqual option.
* @return boolean
*/
public function getOrEqual()
{
return $this->_orEqual;
}
 
/**
* Sets the value to be validated and clears the messages and errors arrays.
* This method will also validate the value as a date.
*
* @return boolean
* @param mixed $value
*/
protected function _setValue($value)
{
if (!$value instanceof Zend_Date) {
// Before we convert our value to Zend_Date, let's make sure it's valid.
require_once 'Zend/Validate/Date.php';
$validator = new Zend_Validate_Date($this->_format, $this->_locale);
if ($validator->isValid($value) === false) {
$errorMessageCodes = $validator->getErrors();
$this->_error($errorMessageCodes[0], $value);
return false;
}
 
$value = new Zend_Date($value, $this->_format, $this->_locale);
}
 
parent::_setValue($value);
return true;
}
 
/**
* Parses our specified date value. This method also determines if a field
* name was passed in for the date and we will grab the value from the field
* in the context array.
*
* @return boolean
* @param array $context[optional]
*/
protected function _parseDate($context = null)
{
// If our $date is a string and exists in our context array, then this means
// the user passed in a field name as the date to parse. Let's get the value
// from the field and convert it to a Zend_Date
if (!$this->_date instanceof Zend_Date) {
if (is_array($context) && array_key_exists($this->_date, $context)) {
// Get the field value from the context array
$date = $context[$this->_date];
 
// Before we set our date, let's make sure the fields value is a valid
// date format
require_once 'Zend/Validate/Date.php';
$validator = new Zend_Validate_Date($this->_format, $this->_locale);
if ($validator->isValid($date) === false) {
$errorMessageCodes = $validator->getErrors();
$errorCode = $errorMessageCodes[0] .'Field';
$this->_error($errorCode, $date);
return false;
}
 
$this->setDate($date);
} else {
// The $date isn't found in the context and isn't a Zend_Date instance.
$this->_error(self::FIELD_INVALID_DATE, $this->_date);
return false;
}
}
return true;
}
 
public function isValid($value, $context = null)
{}
}
My/Validate/Date/GreaterThan.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php
class My_Validate_Date_GreaterThan extends My_Validate_Date_Abstract
{
const NOT_GREATER = 'notGreaterThan';
 
/**
* Sets validator options
*
* @param string|Zend_Date $date
* @param string $format OPTIONAL
* @param string|Zend_Locale $locale OPTIONAL
* @param boolean $orEqual OPTIONAL
* @return void
*/
public function __construct($date, $format = null, $locale = null,
$orEqual = false)
{
$this->_messageTemplates[self::NOT_GREATER] = "'%value%' is not greater than '%date%'";
parent::__construct($date, $format, $locale);
}
 
/**
* Defined by Zend_Validate_Interface
*
* Returns true if $value is a valid date and is greater than the specified
* $date. If optional $format or $locale is set the date format is checked
* according to Zend_Date, see Zend_Date::isDate()
*
* @param string|Zend_Date $value
* @return boolean
*/
public function isValid($value, $context = null)
{
if (!$this->_setValue($value)) return false;
if (!$this->_parseDate($context)) return false;
 
$compare = $this->_value->compare($this->_date);
$compare = ($this->_orEqual) ? ($compare >= 0) : ($compare > 0);
 
if (!$compare) {
$this->_error(self::NOT_GREATER);
return false;
}
 
return true;
}
}
My/Validate/Date/LessThan.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php
class My_Validate_Date_LessThan extends My_Validate_Date_Abstract
{
const NOT_LESS = 'notLessThan';
 
/**
* Sets validator options
*
* @param string|Zend_Date $date
* @param string $format OPTIONAL
* @param string|Zend_Locale $locale OPTIONAL
* @param boolean $orEqual OPTIONAL
* @return void
*/
public function __construct($date, $format = null, $locale = null,
$orEqual = false)
{
$this->_messageTemplates[self::NOT_LESS] = "'%value%' is not less than '%date%'";
parent::__construct($date, $format, $locale, $orEqual);
}
 
/**
* Defined by Zend_Validate_Interface
*
* Returns true if $value is a valid date and is greater than the specified
* $date. If optional $format or $locale is set the date format is checked
* according to Zend_Date, see Zend_Date::isDate()
*
* @param string|Zend_Date $value
* @return boolean
*/
public function isValid($value, $context = null)
{
if (!$this->_setValue($value)) return false;
if (!$this->_parseDate($context)) return false;
 
$compare = $this->_value->compare($this->_date);
$compare = ($this->_orEqual) ? ($compare <= 0) : ($compare < 0);
 
if (!$compare) {
$this->_error(self::NOT_LESS);
return false;
}
 
return true;
}
}

Hii,
There is an error in the code of the My_Validate_Date_GreaterThan class:

 /**
 * Sets validator options
 *
 * @param  string|Zend_Date   $date
 * @param  string             $format OPTIONAL
 * @param  string|Zend_Locale $locale OPTIONAL
 * @param  boolean            $orEqual OPTIONAL
 * @return void
 */
public function __construct($date, $format = null, $locale = null,
    $orEqual = false)
{
  $this->_messageTemplates[self::NOT_GREATER] = "'%value%' is not greater than '%date%'";
  parent::__construct($date, $format, $locale, $orEqual); // <-- here $orEqual was missing ...
}

Regards

Mimiz

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.