summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TRangeValidator.php
diff options
context:
space:
mode:
authorxue <>2006-07-29 14:43:53 +0000
committerxue <>2006-07-29 14:43:53 +0000
commite0c9de073cce5b5c9975694c03e2dbe63788bd66 (patch)
tree0c04506594635064d9f3f62eb45c8aad5c3be685 /framework/Web/UI/WebControls/TRangeValidator.php
parent6385105e7793509de726b2941d038840c04195c1 (diff)
Merge from 3.0 branch till 1305.
Diffstat (limited to 'framework/Web/UI/WebControls/TRangeValidator.php')
-rw-r--r--framework/Web/UI/WebControls/TRangeValidator.php59
1 files changed, 56 insertions, 3 deletions
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.
* - <b>String</b> A string data type.
+ * - <b>StringLength</b> 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 <qiang.xue@gmail.com>
* @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');
}
/**
@@ -123,6 +131,22 @@ class TRangeValidator extends TBaseValidator
}
/**
+ * @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.
* The validation always succeeds if the input data is empty.
@@ -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.