| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TDataTypeValidator class.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TDataTypeValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Using TBaseValidator class
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Web.UI.WebControls.TBaseValidator');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TDataTypeValidator class
|
|
|
20 |
*
|
|
|
21 |
* TDataTypeValidator verifies if the input data is of the type specified
|
|
|
22 |
* by {@link setDataType DataType}.
|
|
|
23 |
* The following data types are supported:
|
|
|
24 |
* - <b>Integer</b> A 32-bit signed integer data type.
|
|
|
25 |
* - <b>Float</b> A double-precision floating point number data type.
|
|
|
26 |
* - <b>Date</b> A date data type.
|
|
|
27 |
* - <b>String</b> A string data type.
|
|
|
28 |
* For <b>Date</b> type, the property {@link setDateFormat DateFormat}
|
|
|
29 |
* will be used to determine how to parse the date string. If it is not
|
|
|
30 |
* provided, the string will be assumed to be in GNU datetime format.
|
|
|
31 |
*
|
|
|
32 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
33 |
* @version $Id: TDataTypeValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
34 |
* @package System.Web.UI.WebControls
|
|
|
35 |
* @since 3.0
|
|
|
36 |
*/
|
|
|
37 |
class TDataTypeValidator extends TBaseValidator
|
|
|
38 |
{
|
|
|
39 |
/**
|
|
|
40 |
* Gets the name of the javascript class responsible for performing validation for this control.
|
|
|
41 |
* This method overrides the parent implementation.
|
|
|
42 |
* @return string the javascript class name
|
|
|
43 |
*/
|
|
|
44 |
protected function getClientClassName()
|
|
|
45 |
{
|
|
|
46 |
return 'Prado.WebUI.TDataTypeValidator';
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* @return TValidationDataType the data type that the values being compared are converted to before the comparison is made. Defaults to TValidationDataType::String.
|
|
|
51 |
*/
|
|
|
52 |
public function getDataType()
|
|
|
53 |
{
|
|
|
54 |
return $this->getViewState('DataType','String');
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Sets the data type that the values being compared are converted to before the comparison is made.
|
|
|
59 |
* @param TValidationDataType the data type
|
|
|
60 |
*/
|
|
|
61 |
public function setDataType($value)
|
|
|
62 |
{
|
|
|
63 |
$this->setViewState('DataType',TPropertyValue::ensureEnum($value,'TValidationDataType'),TValidationDataType::String);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Sets the date format for a date validation
|
|
|
68 |
* @param string the date format value
|
|
|
69 |
*/
|
|
|
70 |
public function setDateFormat($value)
|
|
|
71 |
{
|
|
|
72 |
$this->setViewState('DateFormat', $value, '');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* @return string the date validation date format if any
|
|
|
77 |
*/
|
|
|
78 |
public function getDateFormat()
|
|
|
79 |
{
|
|
|
80 |
return $this->getViewState('DateFormat', '');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Determine if the given value is of a particular type using RegExp.
|
|
|
86 |
* @param string value to check
|
|
|
87 |
* @return boolean true if value fits the type expression.
|
|
|
88 |
*/
|
|
|
89 |
protected function evaluateDataTypeCheck($value)
|
|
|
90 |
{
|
|
|
91 |
if($value=='')
|
|
|
92 |
return true;
|
|
|
93 |
|
|
|
94 |
switch($this->getDataType())
|
|
|
95 |
{
|
|
|
96 |
case TValidationDataType::Integer:
|
|
|
97 |
return preg_match('/^[-+]?[0-9]+$/',trim($value));
|
|
|
98 |
case TValidationDataType::Float:
|
|
|
99 |
return preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
|
|
|
100 |
case TValidationDataType::Date:
|
|
|
101 |
$dateFormat = $this->getDateFormat();
|
|
|
102 |
if(strlen($dateFormat))
|
|
|
103 |
{
|
|
|
104 |
$formatter = Prado::createComponent('System.Util.TSimpleDateFormatter',$dateFormat);
|
|
|
105 |
return $formatter->isValidDate($value);
|
|
|
106 |
}
|
|
|
107 |
else
|
|
|
108 |
return strtotime($value) > 0;
|
|
|
109 |
}
|
|
|
110 |
return true;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Returns an array of javascript validator options.
|
|
|
115 |
* @return array javascript validator options.
|
|
|
116 |
*/
|
|
|
117 |
protected function getClientScriptOptions()
|
|
|
118 |
{
|
|
|
119 |
$options = parent::getClientScriptOptions();
|
|
|
120 |
$options['DataType']=$this->getDataType();
|
|
|
121 |
if(($dateFormat=$this->getDateFormat())!=='')
|
|
|
122 |
$options['DateFormat']=$dateFormat;
|
|
|
123 |
return $options;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* This method overrides the parent's implementation.
|
|
|
128 |
* The validation succeeds if the input data is of valid type.
|
|
|
129 |
* The validation always succeeds if ControlToValidate is not specified
|
|
|
130 |
* or the input data is empty.
|
|
|
131 |
* @return boolean whether the validation succeeds
|
|
|
132 |
*/
|
|
|
133 |
public function evaluateIsValid()
|
|
|
134 |
{
|
|
|
135 |
if(($value=$this->getValidationValue($this->getValidationTarget()))==='')
|
|
|
136 |
return true;
|
|
|
137 |
|
|
|
138 |
return $this->evaluateDataTypeCheck($value);
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|