From e0c9de073cce5b5c9975694c03e2dbe63788bd66 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 29 Jul 2006 14:43:53 +0000 Subject: Merge from 3.0 branch till 1305. --- framework/Web/UI/WebControls/TRangeValidator.php | 59 ++++++++++++++++++++++-- 1 file changed, 56 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 9d23eb5e..b54a1684 100644 --- a/framework/Web/UI/WebControls/TRangeValidator.php +++ b/framework/Web/UI/WebControls/TRangeValidator.php @@ -34,6 +34,13 @@ Prado::using('System.Web.UI.WebControls.TBaseValidator'); * by {@link TSimpleDateFormatter}. If the property is not set, * the GNU date syntax is assumed. * - String A string data type. + * - StringLength check for string length. + * + * The TRangeValidator allows a special DataType "StringLength" that + * can be used to verify minimum and maximum string length. The + * {@link setCharset Charset} property can be used to force a particular + * charset for comparison. Otherwise, the application charset is used and is + * defaulted as UTF-8. * * @author Qiang Xue * @version $Revision: $ $Date: $ @@ -96,13 +103,14 @@ 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, Date, String, StringLength) 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','String', 'StringLength'),'String'); } /** @@ -122,6 +130,22 @@ class TRangeValidator extends TBaseValidator return $this->getViewState('DateFormat', ''); } + /** + * @param string charset for string length comparison. + */ + public function setCharset($value) + { + $this->setViewState('Charset', $value, ''); + } + + /** + * @return string charset for string length comparison. + */ + public function getCharset() + { + return $this->getViewState('Charset', ''); + } + /** * This method overrides the parent's implementation. * The validation succeeds if the input data is within the range. @@ -142,6 +166,8 @@ class TRangeValidator extends TBaseValidator return $this->isValidFloat($value); case 'Date': return $this->isValidDate($value); + case 'StringLength': + return $this->isValidStringLength($value); default: return $this->isValidString($value); } @@ -238,6 +264,33 @@ class TRangeValidator extends TBaseValidator $valid=$valid && (strcmp($value,$maxValue)<=0); return $valid; } + + /** + * @param string string for comparision + * @return boolean true if min and max string length are satisfied. + */ + protected function isValidStringLength($value) + { + $minValue=$this->getMinValue(); + $maxValue=$this->getMaxValue(); + + $valid=true; + $charset = $this->getCharset(); + if($charset==='') + { + $app= $this->getApplication()->getGlobalization(); + $charset = $app ? $app->getCharset() : null; + if(!$charset) + $charset = 'UTF-8'; + } + + $length = iconv_strlen($value, $charset); + if($minValue!=='') + $valid = $valid && $length >= intval($minValue); + if($maxValue!=='') + $valid = $valid && $length <= intval($maxValue); + return $valid; + } /** * Returns an array of javascript validator options. -- cgit v1.2.3