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