| Total Complexity | 141 |
| Total Lines | 883 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FormValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FormValidation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class FormValidation{ |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The form validation status |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | protected $_success = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The list of errors messages |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $_errorsMessages = array(); |
||
| 41 | |||
| 42 | // Array of rule sets, fieldName => PIPE seperated ruleString |
||
| 43 | protected $_rules = array(); |
||
| 44 | |||
| 45 | // Array of errors, niceName => Error Message |
||
| 46 | protected $_errors = array(); |
||
| 47 | |||
| 48 | // Array of post Key => Nice name labels |
||
| 49 | protected $_labels = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The errors delimiters |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The each error delimiter |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Indicated if need force the validation to be failed |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | protected $_forceFail = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The list of the error messages overrides by the original |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $_errorPhraseOverrides = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The logger instance |
||
| 77 | * @var Log |
||
| 78 | */ |
||
| 79 | private $logger; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The data to be validated, the default is to use $_POST |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $data = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
||
| 89 | * CSRF global configuration |
||
| 90 | * |
||
| 91 | * @var boolean |
||
| 92 | */ |
||
| 93 | public $enableCsrfCheck = false; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set all errors and rule sets empty, and sets success to false. |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function __construct() { |
||
| 101 | $this->logger =& class_loader('Log', 'classes'); |
||
| 102 | $this->logger->setLogger('Library::FormValidation'); |
||
| 103 | |||
| 104 | //Load form validation language message |
||
| 105 | Loader::lang('form_validation'); |
||
| 106 | $obj = & get_instance(); |
||
| 107 | $this->_errorsMessages = array( |
||
| 108 | 'required' => $obj->lang->get('fv_required'), |
||
| 109 | 'min_length' => $obj->lang->get('fv_min_length'), |
||
| 110 | 'max_length' => $obj->lang->get('fv_max_length'), |
||
| 111 | 'exact_length' => $obj->lang->get('fv_exact_length'), |
||
| 112 | 'less_than' => $obj->lang->get('fv_less_than'), |
||
| 113 | 'greater_than' => $obj->lang->get('fv_greater_than'), |
||
| 114 | 'matches' => $obj->lang->get('fv_matches'), |
||
| 115 | 'valid_email' => $obj->lang->get('fv_valid_email'), |
||
| 116 | 'not_equal' => array( |
||
| 117 | 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
||
| 118 | 'string' => $obj->lang->get('fv_not_equal_string') |
||
| 119 | ), |
||
| 120 | 'depends' => $obj->lang->get('fv_depends'), |
||
| 121 | 'is_unique' => $obj->lang->get('fv_is_unique'), |
||
| 122 | 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
||
| 123 | 'exists' => $obj->lang->get('fv_exists'), |
||
| 124 | 'regex' => $obj->lang->get('fv_regex'), |
||
| 125 | 'in_list' => $obj->lang->get('fv_in_list'), |
||
| 126 | 'numeric' => $obj->lang->get('fv_numeric'), |
||
| 127 | 'callback' => $obj->lang->get('fv_callback'), |
||
| 128 | ); |
||
| 129 | $this->_resetValidation(); |
||
| 130 | $this->setData($obj->request->post(null)); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Reset the form validation instance |
||
| 135 | */ |
||
| 136 | protected function _resetValidation() { |
||
| 137 | $this->_rules = array(); |
||
| 138 | $this->_labels = array(); |
||
| 139 | $this->_errorPhraseOverrides = array(); |
||
| 140 | $this->_errors = array(); |
||
| 141 | $this->_success = false; |
||
| 142 | $this->_forceFail = false; |
||
| 143 | $this->data = array(); |
||
| 144 | $this->enableCsrfCheck = false; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set the form validation data |
||
| 149 | * @param array $data the values to be validated |
||
| 150 | * |
||
| 151 | * @return FormValidation Current instance of object. |
||
| 152 | */ |
||
| 153 | public function setData(array $data){ |
||
| 154 | $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
||
| 155 | $this->data = $data; |
||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the form validation data |
||
| 161 | * @return array the form validation data to be validated |
||
| 162 | */ |
||
| 163 | public function getData(){ |
||
| 164 | return $this->data; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the validation function name to validate a rule |
||
| 169 | * |
||
| 170 | * @return string the function name |
||
| 171 | */ |
||
| 172 | protected function _toCallCase($funcName, $prefix='_validate') { |
||
| 173 | $funcName = strtolower($funcName); |
||
| 174 | $finalFuncName = $prefix; |
||
| 175 | foreach (explode('_', $funcName) as $funcNamePart) { |
||
| 176 | $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
||
| 177 | } |
||
| 178 | return $finalFuncName; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the boolean of the data status success. It goes by the simple |
||
| 183 | * |
||
| 184 | * @return boolean Whether or not the data validation has succeeded |
||
| 185 | */ |
||
| 186 | public function isSuccess() { |
||
| 187 | return $this->_success; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Checks if the request method is POST or the Data to be validated is set |
||
| 192 | * |
||
| 193 | * @return boolean Whether or not the form has been submitted or the data is available for validation. |
||
| 194 | */ |
||
| 195 | public function canDoValidation() { |
||
| 196 | return get_instance()->request->method() === 'POST' || ! empty($this->data); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Runs _run once POST data has been submitted or data is set manually. |
||
| 201 | * |
||
| 202 | * @return boolean |
||
| 203 | */ |
||
| 204 | public function run() { |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Takes and trims each data, if it has any rules, we parse the rule string and run |
||
| 214 | * each rule against the data value. Sets _success to true if there are no errors |
||
| 215 | * afterwards. |
||
| 216 | */ |
||
| 217 | protected function _run() { |
||
| 218 | if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
||
| 219 | $this->logger->debug('Check if CSRF is enabled in configuration'); |
||
| 220 | //first check for CSRF |
||
| 221 | if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
||
| 222 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
||
| 223 | } |
||
| 224 | else{ |
||
| 225 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | ///////////////////////////////////////////// |
||
| 229 | $this->_forceFail = false; |
||
| 230 | |||
| 231 | foreach ($this->getData() as $inputName => $inputVal) { |
||
| 232 | if(is_array($this->data[$inputName])){ |
||
| 233 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
||
| 234 | } |
||
| 235 | else{ |
||
| 236 | $this->data[$inputName] = trim($this->data[$inputName]); |
||
| 237 | } |
||
| 238 | |||
| 239 | if (array_key_exists($inputName, $this->_rules)) { |
||
| 240 | foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
||
| 241 | $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | if (empty($this->_errors) && $this->_forceFail === false) { |
||
| 247 | $this->_success = true; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Adds a rule to a form data validation field. |
||
| 253 | * |
||
| 254 | * @param string $inputField Name of the field or the data key to add a rule to |
||
| 255 | * @param string $ruleSets PIPE seperated string of rules |
||
| 256 | * |
||
| 257 | * @return FormValidation Current instance of object. |
||
| 258 | */ |
||
| 259 | public function setRule($inputField, $inputLabel, $ruleSets) { |
||
| 260 | $this->_rules[$inputField] = $ruleSets; |
||
| 261 | $this->_labels[$inputField] = $inputLabel; |
||
| 262 | $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Takes an array of rules and uses setRule() to set them, accepts an array |
||
| 268 | * of rule names rather than a pipe-delimited string as well. |
||
| 269 | * @param array $ruleSets |
||
| 270 | * |
||
| 271 | * @return FormValidation Current instance of object. |
||
| 272 | */ |
||
| 273 | public function setRules(array $ruleSets) { |
||
| 274 | foreach ($ruleSets as $ruleSet) { |
||
| 275 | $pipeDelimitedRules = null; |
||
| 276 | if (is_array($ruleSet['rules'])) { |
||
| 277 | $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
||
| 278 | } else { |
||
| 279 | $pipeDelimitedRules = $ruleSet['rules']; |
||
| 280 | } |
||
| 281 | $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
||
| 282 | } |
||
| 283 | return $this; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
||
| 288 | * end of the errors block respectively. |
||
| 289 | * |
||
| 290 | * @param string $start Before block of errors gets displayed, HTML allowed. |
||
| 291 | * @param string $end After the block of errors gets displayed, HTML allowed. |
||
| 292 | * |
||
| 293 | * @return FormValidation Current instance of object. |
||
| 294 | */ |
||
| 295 | public function setErrorsDelimiter($start, $end) { |
||
| 296 | $this->_allErrorsDelimiter[0] = $start; |
||
| 297 | $this->_allErrorsDelimiter[1] = $end; |
||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * This is the individual error delimiter, each argument occurs once before and after |
||
| 303 | * each individual error listed. |
||
| 304 | * |
||
| 305 | * @param string $start Displayed before each error. |
||
| 306 | * @param string $end Displayed after each error. |
||
| 307 | * |
||
| 308 | * @return FormValidation Current instance of object. |
||
| 309 | */ |
||
| 310 | public function setErrorDelimiter($start, $end) { |
||
| 311 | $this->_eachErrorDelimiter[0] = $start; |
||
| 312 | $this->_eachErrorDelimiter[1] = $end; |
||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the each errors delimiters |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function getErrorDelimiter() { |
||
| 322 | return $this->_eachErrorDelimiter; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the all errors delimiters |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function getErrorsDelimiter() { |
||
| 331 | return $this->_allErrorsDelimiter; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * This sets a custom error message that can override the default error phrase provided |
||
| 336 | * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
||
| 337 | * which will globally change the error phrase of that rule, or in the format of: |
||
| 338 | * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
||
| 339 | * that rule, applied on that field. |
||
| 340 | * |
||
| 341 | * @return boolean True on success, false on failure. |
||
| 342 | */ |
||
| 343 | public function setMessage() { |
||
| 344 | $numArgs = func_num_args(); |
||
| 345 | switch ($numArgs) { |
||
| 346 | default: |
||
| 347 | return false; |
||
| 348 | // A global rule error message |
||
| 349 | case 2: |
||
| 350 | foreach ($this->post(null) as $key => $val) { |
||
| 351 | $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
||
| 352 | } |
||
| 353 | break; |
||
| 354 | // Field specific rule error message |
||
| 355 | case 3: |
||
| 356 | $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
||
| 357 | break; |
||
| 358 | } |
||
| 359 | return true; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Adds a custom error message in the errorSet array, that will |
||
| 364 | * forcibly display it. |
||
| 365 | * |
||
| 366 | * @param string $inputName The form input name or data key |
||
| 367 | * @param string $errorMessage Error to display |
||
| 368 | * |
||
| 369 | * @return formValidation Current instance of the object |
||
| 370 | */ |
||
| 371 | public function setCustomError($inputName, $errorMessage) { |
||
| 372 | $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
||
| 373 | $this->_errors[$inputName] = $errorMessage; |
||
| 374 | return $this; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
||
| 379 | * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
||
| 380 | * all values. |
||
| 381 | * |
||
| 382 | * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
||
| 383 | * @param boolean $trim Defaults to true, trims all $this->data values. |
||
| 384 | * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
||
| 385 | */ |
||
| 386 | public function post($key = null, $trim = true) { |
||
| 387 | $returnValue = null; |
||
| 388 | if (is_null($key)) { |
||
| 389 | $returnValue = array(); |
||
| 390 | foreach ($this->getData() as $key => $val) { |
||
| 391 | $returnValue[$key] = $this->post($key, $trim); |
||
| 392 | } |
||
| 393 | } else { |
||
| 394 | $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
||
| 395 | } |
||
| 396 | return $returnValue; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Gets all errors from errorSet and displays them, can be echo out from the |
||
| 401 | * function or just returned. |
||
| 402 | * |
||
| 403 | * @param boolean $limit number of error to display or return |
||
| 404 | * @param boolean $echo Whether or not the values are to be returned or displayed |
||
| 405 | * |
||
| 406 | * @return string Errors formatted for output |
||
| 407 | */ |
||
| 408 | public function displayErrors($limit = null, $echo = true) { |
||
| 409 | list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
||
| 410 | list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
||
| 411 | $errorOutput = $errorsStart; |
||
| 412 | $i = 0; |
||
| 413 | if (!empty($this->_errors)) { |
||
| 414 | foreach ($this->_errors as $fieldName => $error) { |
||
| 415 | if ($i === $limit) { |
||
| 416 | break; |
||
| 417 | } |
||
| 418 | $errorOutput .= $errorStart; |
||
| 419 | $errorOutput .= $error; |
||
| 420 | $errorOutput .= $errorEnd; |
||
| 421 | $i++; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | $errorOutput .= $errorsEnd; |
||
| 425 | echo ($echo) ? $errorOutput : ''; |
||
| 426 | return (! $echo) ? $errorOutput : null; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Returns raw array of errors in no format instead of displaying them |
||
| 431 | * formatted. |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | public function returnErrors() { |
||
| 436 | return $this->_errors; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Breaks up a PIPE seperated string of rules, and puts them into an array. |
||
| 441 | * |
||
| 442 | * @param string $ruleString String to be parsed. |
||
| 443 | * |
||
| 444 | * @return array Array of each value in original string. |
||
| 445 | */ |
||
| 446 | protected function _parseRuleString($ruleString) { |
||
| 447 | $ruleSets = array(); |
||
| 448 | /* |
||
| 449 | //////////////// hack for regex rule that can contain "|" |
||
| 450 | */ |
||
| 451 | if(strpos($ruleString, 'regex') !== false){ |
||
| 452 | $regexRule = array(); |
||
| 453 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
||
| 454 | preg_match($rule, $ruleString, $regexRule); |
||
| 455 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
||
| 456 | if(!empty($regexRule[0])){ |
||
| 457 | $ruleSets[] = $regexRule[0]; |
||
| 458 | } |
||
| 459 | $ruleStringRegex = explode('|', $ruleStringTemp); |
||
| 460 | foreach ($ruleStringRegex as $rule) { |
||
| 461 | $rule = trim($rule); |
||
| 462 | if($rule){ |
||
| 463 | $ruleSets[] = $rule; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | } |
||
| 468 | /***********************************/ |
||
| 469 | else{ |
||
| 470 | if (strpos($ruleString, '|') !== FALSE) { |
||
| 471 | $ruleSets = explode('|', $ruleString); |
||
| 472 | } else { |
||
| 473 | $ruleSets[] = $ruleString; |
||
| 474 | } |
||
| 475 | } |
||
| 476 | return $ruleSets; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns whether or not a field obtains the rule "required". |
||
| 481 | * |
||
| 482 | * @param string $fieldName Field to check if required. |
||
| 483 | * |
||
| 484 | * @return boolean Whether or not the field is required. |
||
| 485 | */ |
||
| 486 | protected function _fieldIsRequired($fieldName) { |
||
| 487 | $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
||
| 488 | return (in_array('required', $rules)); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
||
| 493 | * and adds an error to the errorSet if it fails validation of the rule. |
||
| 494 | * |
||
| 495 | * @param string $inputName Name or key of the validation data |
||
| 496 | * @param string $inputVal Value of the validation data |
||
| 497 | * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | protected function _validateRule($inputName, $inputVal, $ruleName) { |
||
| 501 | $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
||
| 502 | // Array to store args |
||
| 503 | $ruleArgs = array(); |
||
| 504 | |||
| 505 | preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
||
| 506 | |||
| 507 | // Get the rule arguments, realRule is just the base rule name |
||
| 508 | // Like min_length instead of min_length[3] |
||
| 509 | $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
||
| 510 | |||
| 511 | if (method_exists($this, $this->_toCallCase($ruleName))) { |
||
| 512 | $methodToCall = $this->_toCallCase($ruleName); |
||
| 513 | call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
||
| 514 | } |
||
| 515 | return; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Set error for the given field or key |
||
| 520 | * |
||
| 521 | * @param string $inputName the input or key name |
||
| 522 | * @param string $ruleName the rule name |
||
| 523 | * @param array|string $replacements |
||
| 524 | */ |
||
| 525 | protected function _setError($inputName, $ruleName, $replacements = array()) { |
||
| 526 | $rulePhraseKeyParts = explode(',', $ruleName); |
||
| 527 | $rulePhrase = null; |
||
| 528 | foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
||
| 529 | if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
||
| 530 | $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
||
| 531 | } else { |
||
| 532 | $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
||
| 533 | } |
||
| 534 | } |
||
| 535 | // Any overrides? |
||
| 536 | if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
||
| 537 | $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
||
| 538 | } |
||
| 539 | // Type cast to array in case it's a string |
||
| 540 | $replacements = (array) $replacements; |
||
| 541 | $replacementCount = count($replacements); |
||
| 542 | for ($i = 1; $i <= $replacementCount; $i++) { |
||
| 543 | $key = $i - 1; |
||
| 544 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
||
| 545 | } |
||
| 546 | if (! array_key_exists($inputName, $this->_errors)) { |
||
| 547 | $this->_errors[$inputName] = $rulePhrase; |
||
| 548 | } |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Used to run a callback for the callback rule, as well as pass in a default |
||
| 553 | * argument of the post value. For example the username field having a rule: |
||
| 554 | * callback[userExists] will eval userExists(data[username]) - Note the use |
||
| 555 | * of eval over call_user_func is in case the function is not user defined. |
||
| 556 | * |
||
| 557 | * @param type $inputArg |
||
| 558 | * @param string $callbackFunc |
||
| 559 | * |
||
| 560 | * @return mixed |
||
| 561 | */ |
||
| 562 | protected function _runCallback($inputArg, $callbackFunc) { |
||
| 563 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
||
|
1 ignored issue
–
show
|
|||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Used for applying a rule only if the empty callback evaluates to true, |
||
| 568 | * for example required[funcName] - This runs funcName without passing any |
||
| 569 | * arguments. |
||
| 570 | * |
||
| 571 | * @param string $callbackFunc |
||
| 572 | * |
||
| 573 | * @return anything |
||
| 574 | */ |
||
| 575 | protected function _runEmptyCallback($callbackFunc) { |
||
| 576 | return eval('return ' . $callbackFunc . '();'); |
||
|
1 ignored issue
–
show
|
|||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Gets a specific label of a specific field input name. |
||
| 581 | * |
||
| 582 | * @param string $inputName |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | protected function _getLabel($inputName) { |
||
| 587 | return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Peform validation for the rule "required" |
||
| 592 | * @param string $inputName the form field or data key name used |
||
| 593 | * @param string $ruleName the rule name for this validation ("required") |
||
| 594 | * @param array $ruleArgs the rules argument |
||
| 595 | */ |
||
| 596 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
||
| 597 | $inputVal = $this->post($inputName); |
||
| 598 | if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
||
| 599 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
||
| 600 | if ($inputVal == '' && $callbackReturn == true) { |
||
| 601 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | else if($inputVal == '') { |
||
| 605 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
||
| 606 | } |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Perform validation for the honey pot so means for the validation to be failed |
||
| 611 | * @param string $inputName the form field or data key name used |
||
| 612 | * @param string $ruleName the rule name for this validation |
||
| 613 | * @param array $ruleArgs the rules argument |
||
| 614 | */ |
||
| 615 | protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
||
| 616 | if ($this->data[$inputName] != '') { |
||
| 617 | $this->_forceFail = true; |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Peform validation for the rule "callback" |
||
| 623 | * @param string $inputName the form field or data key name used |
||
| 624 | * @param string $ruleName the rule name for this validation ("callback") |
||
| 625 | * @param array $ruleArgs the rules argument |
||
| 626 | */ |
||
| 627 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
||
| 628 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
||
| 629 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
||
| 630 | if(! $result){ |
||
| 631 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 632 | } |
||
| 633 | } |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Peform validation for the rule "depends" |
||
| 638 | * @param string $inputName the form field or data key name used |
||
| 639 | * @param string $ruleName the rule name for this validation ("depends") |
||
| 640 | * @param array $ruleArgs the rules argument |
||
| 641 | */ |
||
| 642 | protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
||
| 643 | if (array_key_exists($ruleArgs[1], $this->_errors)) { |
||
| 644 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 645 | } |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Peform validation for the rule "not_equal" |
||
| 650 | * @param string $inputName the form field or data key name used |
||
| 651 | * @param string $ruleName the rule name for this validation ("not_equal") |
||
| 652 | * @param array $ruleArgs the rules argument |
||
| 653 | */ |
||
| 654 | protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
||
| 655 | $canNotEqual = explode(',', $ruleArgs[1]); |
||
| 656 | foreach ($canNotEqual as $doNotEqual) { |
||
| 657 | $inputVal = $this->post($inputName); |
||
| 658 | if (preg_match('/post:(.*)/', $doNotEqual)) { |
||
| 659 | if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
||
| 660 | $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
||
| 661 | continue; |
||
| 662 | } |
||
| 663 | } |
||
| 664 | else{ |
||
| 665 | if ($inputVal == $doNotEqual) { |
||
| 666 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
||
| 667 | continue; |
||
| 668 | } |
||
| 669 | } |
||
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Peform validation for the rule "matches" |
||
| 675 | * @param string $inputName the form field or data key name used |
||
| 676 | * @param string $ruleName the rule name for this validation ("matches") |
||
| 677 | * @param array $ruleArgs the rules argument |
||
| 678 | */ |
||
| 679 | protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
||
| 680 | $inputVal = $this->post($inputName); |
||
| 681 | if ($inputVal != $this->data[$ruleArgs[1]]) { |
||
| 682 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 683 | } |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Peform validation for the rule "valid_email" |
||
| 688 | * @param string $inputName the form field or data key name used |
||
| 689 | * @param string $ruleName the rule name for this validation ("valid_email") |
||
| 690 | * @param array $ruleArgs the rules argument |
||
| 691 | */ |
||
| 692 | protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
||
| 693 | $inputVal = $this->post($inputName); |
||
| 694 | if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
||
| 695 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 696 | return; |
||
| 697 | } |
||
| 698 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
||
| 699 | } |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Peform validation for the rule "exact_length" |
||
| 704 | * @param string $inputName the form field or data key name used |
||
| 705 | * @param string $ruleName the rule name for this validation ("exact_length") |
||
| 706 | * @param array $ruleArgs the rules argument |
||
| 707 | */ |
||
| 708 | protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
||
| 709 | $inputVal = $this->post($inputName); |
||
| 710 | if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
||
| 711 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 712 | return; |
||
| 713 | } |
||
| 714 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 715 | } |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Peform validation for the rule "max_length" |
||
| 720 | * @param string $inputName the form field or data key name used |
||
| 721 | * @param string $ruleName the rule name for this validation ("max_length") |
||
| 722 | * @param array $ruleArgs the rules argument |
||
| 723 | */ |
||
| 724 | protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
||
| 725 | $inputVal = $this->post($inputName); |
||
| 726 | if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
||
| 727 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 728 | return; |
||
| 729 | } |
||
| 730 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Peform validation for the rule "min_length" |
||
| 736 | * @param string $inputName the form field or data key name used |
||
| 737 | * @param string $ruleName the rule name for this validation ("min_length") |
||
| 738 | * @param array $ruleArgs the rules argument |
||
| 739 | */ |
||
| 740 | protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
||
| 741 | $inputVal = $this->post($inputName); |
||
| 742 | if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
||
| 743 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 744 | return; |
||
| 745 | } |
||
| 746 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Peform validation for the rule "less_than" |
||
| 752 | * @param string $inputName the form field or data key name used |
||
| 753 | * @param string $ruleName the rule name for this validation ("less_than") |
||
| 754 | * @param array $ruleArgs the rules argument |
||
| 755 | */ |
||
| 756 | protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
||
| 757 | $inputVal = $this->post($inputName); |
||
| 758 | if ($inputVal >= $ruleArgs[1]) { |
||
| 759 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 760 | return; |
||
| 761 | } |
||
| 762 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 763 | } |
||
| 764 | } |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Peform validation for the rule "greater_than" |
||
| 768 | * @param string $inputName the form field or data key name used |
||
| 769 | * @param string $ruleName the rule name for this validation ("greater_than") |
||
| 770 | * @param array $ruleArgs the rules argument |
||
| 771 | */ |
||
| 772 | protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
||
| 779 | } |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Peform validation for the rule "numeric" |
||
| 784 | * @param string $inputName the form field or data key name used |
||
| 785 | * @param string $ruleName the rule name for this validation ("numeric") |
||
| 786 | * @param array $ruleArgs the rules argument |
||
| 787 | */ |
||
| 788 | protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
||
| 789 | $inputVal = $this->post($inputName); |
||
| 790 | if (! is_numeric($inputVal)) { |
||
| 791 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 792 | return; |
||
| 793 | } |
||
| 794 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 795 | } |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Peform validation for the rule "exists" |
||
| 800 | * @param string $inputName the form field or data key name used |
||
| 801 | * @param string $ruleName the rule name for this validation ("exists") |
||
| 802 | * @param array $ruleArgs the rules argument |
||
| 803 | */ |
||
| 804 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
||
| 805 | $inputVal = $this->post($inputName); |
||
| 806 | $obj = & get_instance(); |
||
| 807 | if(! isset($obj->database)){ |
||
| 808 | return; |
||
| 809 | } |
||
| 810 | list($table, $column) = explode('.', $ruleArgs[1]); |
||
| 811 | $obj->database->from($table) |
||
| 812 | ->where($column, $inputVal) |
||
| 813 | ->get(); |
||
| 814 | $nb = $obj->database->numRows(); |
||
| 815 | if ($nb == 0) { |
||
| 816 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 817 | return; |
||
| 818 | } |
||
| 819 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 820 | } |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Peform validation for the rule "is_unique" |
||
| 825 | * @param string $inputName the form field or data key name used |
||
| 826 | * @param string $ruleName the rule name for this validation ("is_unique") |
||
| 827 | * @param array $ruleArgs the rules argument |
||
| 828 | */ |
||
| 829 | protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
||
| 830 | $inputVal = $this->post($inputName); |
||
| 831 | $obj = & get_instance(); |
||
| 832 | if(! isset($obj->database)){ |
||
| 833 | return; |
||
| 834 | } |
||
| 835 | list($table, $column) = explode('.', $ruleArgs[1]); |
||
| 836 | $obj->database->from($table) |
||
| 837 | ->where($column, $inputVal) |
||
| 838 | ->get(); |
||
| 839 | $nb = $obj->database->numRows(); |
||
| 840 | if ($nb != 0) { |
||
| 841 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 842 | return; |
||
| 843 | } |
||
| 844 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 845 | } |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Peform validation for the rule "is_unique_update" |
||
| 850 | * @param string $inputName the form field or data key name used |
||
| 851 | * @param string $ruleName the rule name for this validation ("is_unique_update") |
||
| 852 | * @param array $ruleArgs the rules argument |
||
| 853 | */ |
||
| 854 | protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
||
| 876 | } |
||
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Peform validation for the rule "in_list" |
||
| 881 | * @param string $inputName the form field or data key name used |
||
| 882 | * @param string $ruleName the rule name for this validation ("in_list") |
||
| 883 | * @param array $ruleArgs the rules argument |
||
| 884 | */ |
||
| 885 | protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
||
| 894 | } |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Peform validation for the rule "regex" |
||
| 899 | * @param string $inputName the form field or data key name used |
||
| 900 | * @param string $ruleName the rule name for this validation ("regex") |
||
| 901 | * @param array $ruleArgs the rules argument |
||
| 902 | */ |
||
| 903 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
||
| 911 | } |
||
| 912 | } |
||
| 913 | |||
| 914 | } |
||
| 915 |