From 1afc913c386bba8e6072c278b0eb4fd9818ab310 Mon Sep 17 00:00:00 2001 From: xue <> Date: Tue, 25 Apr 2006 01:27:04 +0000 Subject: Undo previous checkin (which goes int 3.0 branch and will be merged back). --- framework/Web/UI/WebControls/TRangeValidator.php | 41 ++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'framework/Web/UI/WebControls/TRangeValidator.php') diff --git a/framework/Web/UI/WebControls/TRangeValidator.php b/framework/Web/UI/WebControls/TRangeValidator.php index b7387522..56cc16bc 100644 --- a/framework/Web/UI/WebControls/TRangeValidator.php +++ b/framework/Web/UI/WebControls/TRangeValidator.php @@ -29,6 +29,7 @@ Prado::using('System.Web.UI.WebControls.TBaseValidator'); * operation is performed. The following value types are supported: * - Integer A 32-bit signed integer data type. * - Float A double-precision floating point number data type. + * - Currency A decimal data type that can contain currency symbols. * - Date A date data type. The date format can be specified by * setting {@link setDateFormat DateFormat} property, which must be recognizable * by {@link TSimpleDateFormatter}. If the property is not set, @@ -86,13 +87,13 @@ class TRangeValidator extends TBaseValidator } /** - * Sets the data type (Integer, Float, Date, String) that the values being - * compared are converted to before the comparison is made. + * Sets the data type (Integer, Float, Currency, Date, String) that the values + * being compared are converted to before the comparison is made. * @param string the data type */ public function setDataType($value) { - $this->setViewState('DataType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','String'),'String'); + $this->setViewState('DataType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','Currency','String'),'String'); } /** @@ -130,6 +131,8 @@ class TRangeValidator extends TBaseValidator return $this->isValidInteger($value); case 'Float': return $this->isValidFloat($value); + case 'Currency': + return $this->isValidCurrency($value); case 'Date': return $this->isValidDate($value); default: @@ -175,6 +178,38 @@ class TRangeValidator extends TBaseValidator return $valid; } + /** + * Determine if the value is a valid currency range, + * @param string currency value + * @return boolean true if within range. + */ + protected function isValidCurrency($value) + { + $minValue=$this->getMinValue(); + $maxValue=$this->getMaxValue(); + + $valid=true; + $value = $this->getCurrencyValue($value); + if($minValue!=='') + $valid=$valid && ($value>= $this->getCurrencyValue($minValue)); + if($maxValue!=='') + $valid=$valid && ($value<= $this->getCurrencyValue($minValue)); + return $valid; + } + + /** + * Parse the string into a currency value, return the float value of the currency. + * @param string currency as string + * @return float currency value. + */ + protected function getCurrencyValue($value) + { + if(preg_match('/[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?/',$value,$matches)) + return floatval($matches[0]); + else + return 0.0; + } + /** * Determine if the date is within the specified range. * Uses pradoParseDate and strtotime to get the date from string. -- cgit v1.2.3