[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TRangeValidator class file 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TRangeValidator.php 1397 2006-09-07 07:55:53Z wei $ 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 * TRangeValidator class 20 * 21 * TRangeValidator tests whether an input value is within a specified range. 22 * 23 * TRangeValidator uses three key properties to perform its validation. 24 * The {@link setMinValue MinValue} and {@link setMaxValue MaxValue} 25 * properties specify the minimum and maximum values of the valid range. 26 * The {@link setDataType DataType} property is used to specify the 27 * data type of the value and the minimum and maximum range values. 28 * These values are converted to this data type before the validation 29 * operation is performed. The following value types are supported: 30 * - <b>Integer</b> A 32-bit signed integer data type. 31 * - <b>Float</b> A double-precision floating point number data type. 32 * - <b>Date</b> A date data type. The date format can be specified by 33 * setting {@link setDateFormat DateFormat} property, which must be recognizable 34 * by {@link TSimpleDateFormatter}. If the property is not set, 35 * the GNU date syntax is assumed. 36 * - <b>String</b> A string data type. 37 * - <b>StringLength</b> check for string length. 38 * 39 * The TRangeValidator allows a special DataType "StringLength" that 40 * can be used to verify minimum and maximum string length. The 41 * {@link setCharset Charset} property can be used to force a particular 42 * charset for comparison. Otherwise, the application charset is used and is 43 * defaulted as UTF-8. 44 * 45 * @author Qiang Xue <qiang.xue@gmail.com> 46 * @version $Id: TRangeValidator.php 1397 2006-09-07 07:55:53Z wei $ 47 * @package System.Web.UI.WebControls 48 * @since 3.0 49 */ 50 class TRangeValidator extends TBaseValidator 51 { 52 /** 53 * Gets the name of the javascript class responsible for performing validation for this control. 54 * This method overrides the parent implementation. 55 * @return string the javascript class name 56 */ 57 protected function getClientClassName() 58 { 59 return 'Prado.WebUI.TRangeValidator'; 60 } 61 62 /** 63 * @return string the minimum value of the validation range. 64 */ 65 public function getMinValue() 66 { 67 return $this->getViewState('MinValue',''); 68 } 69 70 /** 71 * Sets the minimum value of the validation range. 72 * @param string the minimum value 73 */ 74 public function setMinValue($value) 75 { 76 $this->setViewState('MinValue',$value,''); 77 } 78 79 /** 80 * @return string the maximum value of the validation range. 81 */ 82 public function getMaxValue() 83 { 84 return $this->getViewState('MaxValue',''); 85 } 86 87 /** 88 * Sets the maximum value of the validation range. 89 * @param string the maximum value 90 */ 91 public function setMaxValue($value) 92 { 93 $this->setViewState('MaxValue',$value,''); 94 } 95 96 /** 97 * @return TRangeValidationDataType the data type that the values being compared are 98 * converted to before the comparison is made. Defaults to TRangeValidationDataType::String. 99 */ 100 public function getDataType() 101 { 102 return $this->getViewState('DataType',TRangeValidationDataType::String); 103 } 104 105 /** 106 * Sets the data type that the values being compared are converted to before the comparison is made. 107 * @param TRangeValidationDataType the data type 108 */ 109 public function setDataType($value) 110 { 111 $this->setViewState('DataType',TPropertyValue::ensureEnum($value,'TRangeValidationDataType'),TRangeValidationDataType::String); 112 } 113 114 /** 115 * Sets the date format for a date validation 116 * @param string the date format value 117 */ 118 public function setDateFormat($value) 119 { 120 $this->setViewState('DateFormat', $value, ''); 121 } 122 123 /** 124 * @return string the date validation date format if any 125 */ 126 public function getDateFormat() 127 { 128 return $this->getViewState('DateFormat', ''); 129 } 130 131 /** 132 * @param string charset for string length comparison. 133 */ 134 public function setCharset($value) 135 { 136 $this->setViewState('Charset', $value, ''); 137 } 138 139 /** 140 * @return string charset for string length comparison. 141 */ 142 public function getCharset() 143 { 144 return $this->getViewState('Charset', ''); 145 } 146 147 /** 148 * This method overrides the parent's implementation. 149 * The validation succeeds if the input data is within the range. 150 * The validation always succeeds if the input data is empty. 151 * @return boolean whether the validation succeeds 152 */ 153 protected function evaluateIsValid() 154 { 155 $value=$this->getValidationValue($this->getValidationTarget()); 156 if($value==='') 157 return true; 158 159 switch($this->getDataType()) 160 { 161 case TRangeValidationDataType::Integer: 162 return $this->isValidInteger($value); 163 case TRangeValidationDataType::Float: 164 return $this->isValidFloat($value); 165 case TRangeValidationDataType::Date: 166 return $this->isValidDate($value); 167 case TRangeValidationDataType::StringLength: 168 return $this->isValidStringLength($value); 169 default: 170 return $this->isValidString($value); 171 } 172 } 173 174 /** 175 * Determine if the value is within the integer range. 176 * @param string value to validate true 177 * @return boolean true if within integer range. 178 */ 179 protected function isValidInteger($value) 180 { 181 $minValue=$this->getMinValue(); 182 $maxValue=$this->getMaxValue(); 183 184 $value=intval($value); 185 $valid=true; 186 if($minValue!=='') 187 $valid=$valid && ($value>=intval($minValue)); 188 if($maxValue!=='') 189 $valid=$valid && ($value<=intval($maxValue)); 190 return $valid; 191 } 192 193 /** 194 * Determine if the value is within the specified float range. 195 * @param string value to validate 196 * @return boolean true if within range. 197 */ 198 protected function isValidFloat($value) 199 { 200 $minValue=$this->getMinValue(); 201 $maxValue=$this->getMaxValue(); 202 203 $value=floatval($value); 204 $valid=true; 205 if($minValue!=='') 206 $valid=$valid && ($value>=floatval($minValue)); 207 if($maxValue!=='') 208 $valid=$valid && ($value<=floatval($maxValue)); 209 return $valid; 210 } 211 212 /** 213 * Determine if the date is within the specified range. 214 * Uses pradoParseDate and strtotime to get the date from string. 215 * @param string date as string to validate 216 * @return boolean true if within range. 217 */ 218 protected function isValidDate($value) 219 { 220 $minValue=$this->getMinValue(); 221 $maxValue=$this->getMaxValue(); 222 223 $valid=true; 224 225 $dateFormat = $this->getDateFormat(); 226 if($dateFormat!=='') 227 { 228 $formatter=Prado::createComponent('System.Util.TSimpleDateFormatter', $dateFormat); 229 $value = $formatter->parse($value, $dateFormat); 230 if($minValue!=='') 231 $valid=$valid && ($value>=$formatter->parse($minValue)); 232 if($maxValue!=='') 233 $valid=$valid && ($value<=$formatter->parse($maxValue)); 234 return $valid; 235 } 236 else 237 { 238 $value=strtotime($value); 239 if($minValue!=='') 240 $valid=$valid && ($value>=strtotime($minValue)); 241 if($maxValue!=='') 242 $valid=$valid && ($value<=strtotime($maxValue)); 243 return $valid; 244 } 245 } 246 247 /** 248 * Compare the string with a minimum and a maxiumum value. 249 * Uses strcmp for comparision. 250 * @param string value to compare with. 251 * @return boolean true if the string is within range. 252 */ 253 protected function isValidString($value) 254 { 255 $minValue=$this->getMinValue(); 256 $maxValue=$this->getMaxValue(); 257 258 $valid=true; 259 if($minValue!=='') 260 $valid=$valid && (strcmp($value,$minValue)>=0); 261 if($maxValue!=='') 262 $valid=$valid && (strcmp($value,$maxValue)<=0); 263 return $valid; 264 } 265 266 /** 267 * @param string string for comparision 268 * @return boolean true if min and max string length are satisfied. 269 */ 270 protected function isValidStringLength($value) 271 { 272 $minValue=$this->getMinValue(); 273 $maxValue=$this->getMaxValue(); 274 275 $valid=true; 276 $charset = $this->getCharset(); 277 if($charset==='') 278 { 279 $app= $this->getApplication()->getGlobalization(); 280 $charset = $app ? $app->getCharset() : null; 281 if(!$charset) 282 $charset = 'UTF-8'; 283 } 284 285 $length = iconv_strlen($value, $charset); 286 if($minValue!=='') 287 $valid = $valid && $length >= intval($minValue); 288 if($maxValue!=='') 289 $valid = $valid && $length <= intval($maxValue); 290 return $valid; 291 } 292 293 /** 294 * Returns an array of javascript validator options. 295 * @return array javascript validator options. 296 */ 297 protected function getClientScriptOptions() 298 { 299 $options=parent::getClientScriptOptions(); 300 $options['MinValue']=$this->getMinValue(); 301 $options['MaxValue']=$this->getMaxValue(); 302 $options['DataType']=$this->getDataType(); 303 if(($dateFormat=$this->getDateFormat())!=='') 304 $options['DateFormat']=$dateFormat; 305 return $options; 306 } 307 } 308 309 310 /** 311 * TRangeValidationDataType class. 312 * TRangeValidationDataType defines the enumerable type for the possible data types that 313 * a range validator can validate upon. 314 * 315 * The following enumerable values are defined: 316 * - Integer 317 * - Float 318 * - Date 319 * - String 320 * - StringLength 321 * 322 * @author Qiang Xue <qiang.xue@gmail.com> 323 * @version $Id: TRangeValidator.php 1397 2006-09-07 07:55:53Z wei $ 324 * @package System.Web.UI.WebControls 325 * @since 3.0.4 326 */ 327 class TRangeValidationDataType extends TValidationDataType 328 { 329 const StringLength='StringLength'; 330 } 331 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |