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