summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TDataTypeValidator.php
diff options
context:
space:
mode:
authorxue <>2006-02-22 05:09:29 +0000
committerxue <>2006-02-22 05:09:29 +0000
commitd7c8a56d49200cb46d94403934674d670035ff20 (patch)
tree94f2b9370333afd15db61916e0b4c6e0049507c9 /framework/Web/UI/WebControls/TDataTypeValidator.php
parenta5c9d537393a949f5b2d9469505890fa508b92ab (diff)
cleanup of validators.
Diffstat (limited to 'framework/Web/UI/WebControls/TDataTypeValidator.php')
-rw-r--r--framework/Web/UI/WebControls/TDataTypeValidator.php107
1 files changed, 107 insertions, 0 deletions
diff --git a/framework/Web/UI/WebControls/TDataTypeValidator.php b/framework/Web/UI/WebControls/TDataTypeValidator.php
new file mode 100644
index 00000000..daaa9b6e
--- /dev/null
+++ b/framework/Web/UI/WebControls/TDataTypeValidator.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * TDataTypeValidator class.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI.WebControls
+ */
+
+/**
+ * Using TBaseValidator class
+ */
+Prado::using('System.Web.UI.WebControls.TBaseValidator');
+
+/**
+ * TDataTypeValidator class
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI.WebControls
+ * @since 3.0
+ */
+class TDataTypeValidator extends TBaseValidator
+{
+ /**
+ * @return string the data type that the values being compared are converted to before the comparison is made. Defaults to String.
+ */
+ public function getValueType()
+ {
+ return $this->getViewState('ValueType','String');
+ }
+
+ /**
+ * Sets the data type (Integer, Double, Currency, Date, String) that the values being compared are converted to before the comparison is made.
+ * @param string the data type
+ */
+ public function setValueType($value)
+ {
+ $this->setViewState('ValueType',TPropertyValue::ensureEnum($value,'Integer','Double','Date','Currency','String'),'String');
+ }
+
+ /**
+ * Sets the date format for a date validation
+ * @param string the date format value
+ */
+ public function setDateFormat($value)
+ {
+ $this->setViewState('DateFormat', $value, '');
+ }
+
+ /**
+ * @return string the date validation date format if any
+ */
+ public function getDateFormat()
+ {
+ return $this->getViewState('DateFormat', '');
+ }
+
+
+ /**
+ * Determine if the given value is of a particular type using RegExp.
+ * @param string value to check
+ * @return boolean true if value fits the type expression.
+ */
+ protected function evaluateDataTypeCheck($value)
+ {
+ switch($this->getValueType())
+ {
+ case 'Integer':
+ return preg_match('/^[-+]?[0-9]+$/',trim($value));
+ case 'Float':
+ return preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
+ case 'Currency':
+ return preg_match('/[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
+ case 'Date':
+ $dateFormat = $this->getDateFormat();
+ if(strlen($dateFormat))
+ {
+ $formatter = Prado::createComponent('System.Data.TSimpleDateFormatter',$dateFormat);
+ return $formatter->isValidDate($value);
+ }
+ else
+ return strtotime($value) > 0;
+ }
+ return true;
+ }
+
+ /**
+ * This method overrides the parent's implementation.
+ * The validation succeeds if the input data is of valid type.
+ * The validation always succeeds if ControlToValidate is not specified
+ * or the input data is empty.
+ * @return boolean whether the validation succeeds
+ */
+ public function evaluateIsValid()
+ {
+ if(($value=$this->getValidationValue($this->getValidationTarget()))==='')
+ return true;
+
+ return $this->evaluateDataTypeCheck($value);
+ }
+}
+
+?> \ No newline at end of file