Created
June 30, 2010 17:03
-
-
Save MisterPhoton/458941 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Zend/Validate/Abstract.php'; | |
/** | |
* Mo' money, mo' money, mo' money. | |
* | |
* @package System_Form_Validate | |
*/ | |
class System_Form_Validate_Currency extends Zend_Validate_Abstract { | |
/** | |
* Validation failure message key for effed up money format. | |
*/ | |
const FORMAT = 'format'; | |
/** | |
* Validation failure message key for an amount not meeting the minimum required. | |
*/ | |
const NOT_ENOUGH = 'notEnough'; | |
/** | |
* Validation failure message key for an amount being too high. | |
*/ | |
const TOO_MUCH = 'tooMuch'; // No such thing! HA! | |
/** | |
* @var float | |
*/ | |
protected $min = null; | |
/** | |
* @var float | |
*/ | |
protected $max = null; | |
/** | |
* @var string | |
*/ | |
protected $strMin = ''; | |
/** | |
* @var string | |
*/ | |
protected $strMax = ''; | |
/** | |
* Validation failure messages. | |
* | |
* @var array | |
*/ | |
protected $_messageTemplates = array( | |
self::FORMAT => '%field% must be in the format: <strong><em>123.45</em></strong>', | |
self::NOT_ENOUGH => '%field% must be at least <strong>%min%</strong>', | |
self::TOO_MUCH => '%field% may not be more than <strong>%max%</strong>' | |
); | |
/** | |
* Message variable mappings. | |
* | |
* @var array | |
*/ | |
protected $_messageVariables = array( | |
'min' => 'strMin', 'max' => 'strMax' | |
); | |
/** | |
* Constructor. | |
* | |
* @param float $min | |
* @param float $max | |
*/ | |
public function __construct($min = null, $max = null) { | |
$this->min = $min; | |
$this->max = $max; | |
} | |
/** | |
* Tests the value given as valid expiration date as returned by a MonthYear form element. | |
* Assume that if $this->max is the same as $value, validation should pass. | |
* | |
* @param mixed $value | |
*/ | |
public function isValid($value) { | |
$value = (integer)(floatval($value) * 100); | |
$this->min = (null !== $this->min) ? intval(ceil($this->min * 100)) : null; | |
$this->max = (null !== $this->max) ? intval(ceil($this->max * 100)) : null; | |
$value = floatval($value); | |
if (! preg_match('/^\d+(?:\.\d{0,2})?$/', $value)) { | |
$this->_error(self::FORMAT); | |
return false; | |
} | |
if (null !== $this->min && $value < $this->min) { | |
$this->strMin = sprintf('$%.2f', $this->min); | |
$this->_error(self::NOT_ENOUGH); | |
return false; | |
} | |
elseif (null !== $this->max && $value > $this->max) { | |
$this->strMax = sprintf('$%.2f', $this->max); | |
$this->_error(self::TOO_MUCH); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment