| Total Complexity | 144 |
| Total Lines | 913 |
| 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() { |
||
| 220 | if ($this->canDoValidation()) { |
||
| 221 | $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
||
| 222 | $this->_run(); |
||
| 223 | } |
||
| 224 | return $this->isSuccess(); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Validate the CSRF |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | protected function validateCSRF(){ |
||
| 232 | if(get_instance()->request->method() == 'POST'){ |
||
| 233 | $this->logger->debug('Check if CSRF is enabled in configuration'); |
||
| 234 | //first check for CSRF |
||
| 235 | if (get_config('csrf_enable', false) && ! Security::validateCSRF()){ |
||
| 236 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
||
| 237 | } |
||
| 238 | else{ |
||
| 239 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
||
| 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 | |||
| 270 | if (empty($this->_errors) && $this->_forceFail === false) { |
||
| 271 | $this->_success = true; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Adds a rule to a form data validation field. |
||
| 277 | * |
||
| 278 | * @param string $inputField Name of the field or the data key to add a rule to |
||
| 279 | * @param string $ruleSets PIPE seperated string of rules |
||
| 280 | * |
||
| 281 | * @return FormValidation Current instance of object. |
||
| 282 | */ |
||
| 283 | public function setRule($inputField, $inputLabel, $ruleSets) { |
||
| 284 | $this->_rules[$inputField] = $ruleSets; |
||
| 285 | $this->_labels[$inputField] = $inputLabel; |
||
| 286 | $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
||
| 287 | return $this; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Takes an array of rules and uses setRule() to set them, accepts an array |
||
| 292 | * of rule names rather than a pipe-delimited string as well. |
||
| 293 | * @param array $ruleSets |
||
| 294 | * |
||
| 295 | * @return FormValidation Current instance of object. |
||
| 296 | */ |
||
| 297 | public function setRules(array $ruleSets) { |
||
| 298 | foreach ($ruleSets as $ruleSet) { |
||
| 299 | $pipeDelimitedRules = null; |
||
| 300 | if (is_array($ruleSet['rules'])) { |
||
| 301 | $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
||
| 302 | } else { |
||
| 303 | $pipeDelimitedRules = $ruleSet['rules']; |
||
| 304 | } |
||
| 305 | $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
||
| 306 | } |
||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
||
| 312 | * end of the errors block respectively. |
||
| 313 | * |
||
| 314 | * @param string $start Before block of errors gets displayed, HTML allowed. |
||
| 315 | * @param string $end After the block of errors gets displayed, HTML allowed. |
||
| 316 | * |
||
| 317 | * @return FormValidation Current instance of object. |
||
| 318 | */ |
||
| 319 | public function setErrorsDelimiter($start, $end) { |
||
| 320 | $this->_allErrorsDelimiter[0] = $start; |
||
| 321 | $this->_allErrorsDelimiter[1] = $end; |
||
| 322 | return $this; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * This is the individual error delimiter, each argument occurs once before and after |
||
| 327 | * each individual error listed. |
||
| 328 | * |
||
| 329 | * @param string $start Displayed before each error. |
||
| 330 | * @param string $end Displayed after each error. |
||
| 331 | * |
||
| 332 | * @return FormValidation Current instance of object. |
||
| 333 | */ |
||
| 334 | public function setErrorDelimiter($start, $end) { |
||
| 335 | $this->_eachErrorDelimiter[0] = $start; |
||
| 336 | $this->_eachErrorDelimiter[1] = $end; |
||
| 337 | return $this; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the each errors delimiters |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function getErrorDelimiter() { |
||
| 346 | return $this->_eachErrorDelimiter; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get the all errors delimiters |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function getErrorsDelimiter() { |
||
| 355 | return $this->_allErrorsDelimiter; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * This sets a custom error message that can override the default error phrase provided |
||
| 360 | * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
||
| 361 | * which will globally change the error phrase of that rule, or in the format of: |
||
| 362 | * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
||
| 363 | * that rule, applied on that field. |
||
| 364 | * |
||
| 365 | * @return boolean True on success, false on failure. |
||
| 366 | */ |
||
| 367 | public function setMessage() { |
||
| 368 | $numArgs = func_num_args(); |
||
| 369 | switch ($numArgs) { |
||
| 370 | default: |
||
| 371 | return false; |
||
| 372 | // A global rule error message |
||
| 373 | case 2: |
||
| 374 | foreach ($this->post(null) as $key => $val) { |
||
| 375 | $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
||
| 376 | } |
||
| 377 | break; |
||
| 378 | // Field specific rule error message |
||
| 379 | case 3: |
||
| 380 | $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
||
| 381 | break; |
||
| 382 | } |
||
| 383 | return true; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Adds a custom error message in the errorSet array, that will |
||
| 388 | * forcibly display it. |
||
| 389 | * |
||
| 390 | * @param string $inputName The form input name or data key |
||
| 391 | * @param string $errorMessage Error to display |
||
| 392 | * |
||
| 393 | * @return formValidation Current instance of the object |
||
| 394 | */ |
||
| 395 | public function setCustomError($inputName, $errorMessage) { |
||
| 396 | $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
||
| 397 | $this->_errors[$inputName] = $errorMessage; |
||
| 398 | return $this; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
||
| 403 | * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
||
| 404 | * all values. |
||
| 405 | * |
||
| 406 | * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
||
| 407 | * @param boolean $trim Defaults to true, trims all $this->data values. |
||
| 408 | * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
||
| 409 | */ |
||
| 410 | public function post($key = null, $trim = true) { |
||
| 411 | $returnValue = null; |
||
| 412 | if (is_null($key)) { |
||
| 413 | $returnValue = array(); |
||
| 414 | foreach ($this->getData() as $key => $val) { |
||
| 415 | $returnValue[$key] = $this->post($key, $trim); |
||
| 416 | } |
||
| 417 | } else { |
||
| 418 | $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
||
| 419 | } |
||
| 420 | return $returnValue; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Gets all errors from errorSet and displays them, can be echo out from the |
||
| 425 | * function or just returned. |
||
| 426 | * |
||
| 427 | * @param boolean $limit number of error to display or return |
||
| 428 | * @param boolean $echo Whether or not the values are to be returned or displayed |
||
| 429 | * |
||
| 430 | * @return string Errors formatted for output |
||
| 431 | */ |
||
| 432 | public function displayErrors($limit = null, $echo = true) { |
||
| 433 | list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
||
| 434 | list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
||
| 435 | $errorOutput = $errorsStart; |
||
| 436 | $i = 0; |
||
| 437 | if (!empty($this->_errors)) { |
||
| 438 | foreach ($this->_errors as $fieldName => $error) { |
||
| 439 | if ($i === $limit) { |
||
| 440 | break; |
||
| 441 | } |
||
| 442 | $errorOutput .= $errorStart; |
||
| 443 | $errorOutput .= $error; |
||
| 444 | $errorOutput .= $errorEnd; |
||
| 445 | $i++; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | $errorOutput .= $errorsEnd; |
||
| 449 | echo ($echo) ? $errorOutput : ''; |
||
| 450 | return (! $echo) ? $errorOutput : null; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns raw array of errors in no format instead of displaying them |
||
| 455 | * formatted. |
||
| 456 | * |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | public function returnErrors() { |
||
| 460 | return $this->_errors; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Breaks up a PIPE seperated string of rules, and puts them into an array. |
||
| 465 | * |
||
| 466 | * @param string $ruleString String to be parsed. |
||
| 467 | * |
||
| 468 | * @return array Array of each value in original string. |
||
| 469 | */ |
||
| 470 | protected function _parseRuleString($ruleString) { |
||
| 471 | $ruleSets = array(); |
||
| 472 | /* |
||
| 473 | //////////////// hack for regex rule that can contain "|" |
||
| 474 | */ |
||
| 475 | if(strpos($ruleString, 'regex') !== false){ |
||
| 476 | $regexRule = array(); |
||
| 477 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
||
| 478 | preg_match($rule, $ruleString, $regexRule); |
||
| 479 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
||
| 480 | if(!empty($regexRule[0])){ |
||
| 481 | $ruleSets[] = $regexRule[0]; |
||
| 482 | } |
||
| 483 | $ruleStringRegex = explode('|', $ruleStringTemp); |
||
| 484 | foreach ($ruleStringRegex as $rule) { |
||
| 485 | $rule = trim($rule); |
||
| 486 | if($rule){ |
||
| 487 | $ruleSets[] = $rule; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | } |
||
| 492 | /***********************************/ |
||
| 493 | else{ |
||
| 494 | if (strpos($ruleString, '|') !== FALSE) { |
||
| 495 | $ruleSets = explode('|', $ruleString); |
||
| 496 | } else { |
||
| 497 | $ruleSets[] = $ruleString; |
||
| 498 | } |
||
| 499 | } |
||
| 500 | return $ruleSets; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns whether or not a field obtains the rule "required". |
||
| 505 | * |
||
| 506 | * @param string $fieldName Field to check if required. |
||
| 507 | * |
||
| 508 | * @return boolean Whether or not the field is required. |
||
| 509 | */ |
||
| 510 | protected function _fieldIsRequired($fieldName) { |
||
| 511 | $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
||
| 512 | return (in_array('required', $rules)); |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
||
| 517 | * and adds an error to the errorSet if it fails validation of the rule. |
||
| 518 | * |
||
| 519 | * @param string $inputName Name or key of the validation data |
||
| 520 | * @param string $inputVal Value of the validation data |
||
| 521 | * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | protected function _validateRule($inputName, $inputVal, $ruleName) { |
||
| 525 | $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
||
| 526 | // Array to store args |
||
| 527 | $ruleArgs = array(); |
||
| 528 | |||
| 529 | preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
||
| 530 | |||
| 531 | // Get the rule arguments, realRule is just the base rule name |
||
| 532 | // Like min_length instead of min_length[3] |
||
| 533 | $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
||
| 534 | |||
| 535 | if (method_exists($this, $this->_toCallCase($ruleName))) { |
||
| 536 | $methodToCall = $this->_toCallCase($ruleName); |
||
| 537 | call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
||
| 538 | } |
||
| 539 | return; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Set error for the given field or key |
||
| 544 | * |
||
| 545 | * @param string $inputName the input or key name |
||
| 546 | * @param string $ruleName the rule name |
||
| 547 | * @param array|string $replacements |
||
| 548 | */ |
||
| 549 | protected function _setError($inputName, $ruleName, $replacements = array()) { |
||
| 550 | $rulePhraseKeyParts = explode(',', $ruleName); |
||
| 551 | $rulePhrase = null; |
||
| 552 | foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
||
| 553 | if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
||
| 554 | $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
||
| 555 | } else { |
||
| 556 | $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
||
| 557 | } |
||
| 558 | } |
||
| 559 | // Any overrides? |
||
| 560 | if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
||
| 561 | $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
||
| 562 | } |
||
| 563 | // Type cast to array in case it's a string |
||
| 564 | $replacements = (array) $replacements; |
||
| 565 | $replacementCount = count($replacements); |
||
| 566 | for ($i = 1; $i <= $replacementCount; $i++) { |
||
| 567 | $key = $i - 1; |
||
| 568 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
||
| 569 | } |
||
| 570 | if (! array_key_exists($inputName, $this->_errors)) { |
||
| 571 | $this->_errors[$inputName] = $rulePhrase; |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Used to run a callback for the callback rule, as well as pass in a default |
||
| 577 | * argument of the post value. For example the username field having a rule: |
||
| 578 | * callback[userExists] will eval userExists(data[username]) - Note the use |
||
| 579 | * of eval over call_user_func is in case the function is not user defined. |
||
| 580 | * |
||
| 581 | * @param type $inputArg |
||
| 582 | * @param string $callbackFunc |
||
| 583 | * |
||
| 584 | * @return mixed |
||
| 585 | */ |
||
| 586 | protected function _runCallback($inputArg, $callbackFunc) { |
||
| 587 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Used for applying a rule only if the empty callback evaluates to true, |
||
| 592 | * for example required[funcName] - This runs funcName without passing any |
||
| 593 | * arguments. |
||
| 594 | * |
||
| 595 | * @param string $callbackFunc |
||
| 596 | * |
||
| 597 | * @return mixed |
||
| 598 | */ |
||
| 599 | protected function _runEmptyCallback($callbackFunc) { |
||
| 600 | return eval('return ' . $callbackFunc . '();'); |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Gets a specific label of a specific field input name. |
||
| 605 | * |
||
| 606 | * @param string $inputName |
||
| 607 | * |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | protected function _getLabel($inputName) { |
||
| 611 | return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Peform validation for the rule "required" |
||
| 616 | * @param string $inputName the form field or data key name used |
||
| 617 | * @param string $ruleName the rule name for this validation ("required") |
||
| 618 | * @param array $ruleArgs the rules argument |
||
| 619 | */ |
||
| 620 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
||
| 621 | $inputVal = $this->post($inputName); |
||
| 622 | if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
||
| 623 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
||
| 624 | if ($inputVal == '' && $callbackReturn == true) { |
||
| 625 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | else if($inputVal == '') { |
||
| 629 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
||
| 630 | } |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Perform validation for the honey pot so means for the validation to be failed |
||
| 635 | * @param string $inputName the form field or data key name used |
||
| 636 | * @param string $ruleName the rule name for this validation |
||
| 637 | * @param array $ruleArgs the rules argument |
||
| 638 | */ |
||
| 639 | protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
||
| 640 | if ($this->data[$inputName] != '') { |
||
| 641 | $this->_forceFail = true; |
||
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Peform validation for the rule "callback" |
||
| 647 | * @param string $inputName the form field or data key name used |
||
| 648 | * @param string $ruleName the rule name for this validation ("callback") |
||
| 649 | * @param array $ruleArgs the rules argument |
||
| 650 | */ |
||
| 651 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
||
| 652 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
||
| 653 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
||
| 654 | if(! $result){ |
||
| 655 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Peform validation for the rule "depends" |
||
| 662 | * @param string $inputName the form field or data key name used |
||
| 663 | * @param string $ruleName the rule name for this validation ("depends") |
||
| 664 | * @param array $ruleArgs the rules argument |
||
| 665 | */ |
||
| 666 | protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
||
| 667 | if (array_key_exists($ruleArgs[1], $this->_errors)) { |
||
| 668 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 669 | } |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Peform validation for the rule "not_equal" |
||
| 674 | * @param string $inputName the form field or data key name used |
||
| 675 | * @param string $ruleName the rule name for this validation ("not_equal") |
||
| 676 | * @param array $ruleArgs the rules argument |
||
| 677 | */ |
||
| 678 | protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
||
| 679 | $canNotEqual = explode(',', $ruleArgs[1]); |
||
| 680 | foreach ($canNotEqual as $doNotEqual) { |
||
| 681 | $inputVal = $this->post($inputName); |
||
| 682 | if (preg_match('/post:(.*)/', $doNotEqual)) { |
||
| 683 | if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
||
| 684 | $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
||
| 685 | continue; |
||
| 686 | } |
||
| 687 | } |
||
| 688 | else{ |
||
| 689 | if ($inputVal == $doNotEqual) { |
||
| 690 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
||
| 691 | continue; |
||
| 692 | } |
||
| 693 | } |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Peform validation for the rule "matches" |
||
| 699 | * @param string $inputName the form field or data key name used |
||
| 700 | * @param string $ruleName the rule name for this validation ("matches") |
||
| 701 | * @param array $ruleArgs the rules argument |
||
| 702 | */ |
||
| 703 | protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
||
| 704 | $inputVal = $this->post($inputName); |
||
| 705 | if ($inputVal != $this->data[$ruleArgs[1]]) { |
||
| 706 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 707 | } |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Peform validation for the rule "valid_email" |
||
| 712 | * @param string $inputName the form field or data key name used |
||
| 713 | * @param string $ruleName the rule name for this validation ("valid_email") |
||
| 714 | * @param array $ruleArgs the rules argument |
||
| 715 | */ |
||
| 716 | protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
||
| 717 | $inputVal = $this->post($inputName); |
||
| 718 | 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)) { |
||
| 719 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 720 | return; |
||
| 721 | } |
||
| 722 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
||
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Peform validation for the rule "exact_length" |
||
| 728 | * @param string $inputName the form field or data key name used |
||
| 729 | * @param string $ruleName the rule name for this validation ("exact_length") |
||
| 730 | * @param array $ruleArgs the rules argument |
||
| 731 | */ |
||
| 732 | protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
||
| 733 | $inputVal = $this->post($inputName); |
||
| 734 | if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
||
| 735 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 736 | return; |
||
| 737 | } |
||
| 738 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 739 | } |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Peform validation for the rule "max_length" |
||
| 744 | * @param string $inputName the form field or data key name used |
||
| 745 | * @param string $ruleName the rule name for this validation ("max_length") |
||
| 746 | * @param array $ruleArgs the rules argument |
||
| 747 | */ |
||
| 748 | protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
||
| 749 | $inputVal = $this->post($inputName); |
||
| 750 | if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
||
| 751 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 752 | return; |
||
| 753 | } |
||
| 754 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 755 | } |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Peform validation for the rule "min_length" |
||
| 760 | * @param string $inputName the form field or data key name used |
||
| 761 | * @param string $ruleName the rule name for this validation ("min_length") |
||
| 762 | * @param array $ruleArgs the rules argument |
||
| 763 | */ |
||
| 764 | protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
||
| 765 | $inputVal = $this->post($inputName); |
||
| 766 | if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
||
| 767 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 768 | return; |
||
| 769 | } |
||
| 770 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Peform validation for the rule "less_than" |
||
| 776 | * @param string $inputName the form field or data key name used |
||
| 777 | * @param string $ruleName the rule name for this validation ("less_than") |
||
| 778 | * @param array $ruleArgs the rules argument |
||
| 779 | */ |
||
| 780 | protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
||
| 781 | $inputVal = $this->post($inputName); |
||
| 782 | if ($inputVal >= $ruleArgs[1]) { |
||
| 783 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 784 | return; |
||
| 785 | } |
||
| 786 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
||
| 787 | } |
||
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Peform validation for the rule "greater_than" |
||
| 792 | * @param string $inputName the form field or data key name used |
||
| 793 | * @param string $ruleName the rule name for this validation ("greater_than") |
||
| 794 | * @param array $ruleArgs the rules argument |
||
| 795 | */ |
||
| 796 | protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
||
| 803 | } |
||
| 804 | } |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Peform validation for the rule "numeric" |
||
| 808 | * @param string $inputName the form field or data key name used |
||
| 809 | * @param string $ruleName the rule name for this validation ("numeric") |
||
| 810 | * @param array $ruleArgs the rules argument |
||
| 811 | */ |
||
| 812 | protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
||
| 813 | $inputVal = $this->post($inputName); |
||
| 814 | if (! is_numeric($inputVal)) { |
||
| 815 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 816 | return; |
||
| 817 | } |
||
| 818 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 819 | } |
||
| 820 | } |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Peform validation for the rule "exists" |
||
| 824 | * @param string $inputName the form field or data key name used |
||
| 825 | * @param string $ruleName the rule name for this validation ("exists") |
||
| 826 | * @param array $ruleArgs the rules argument |
||
| 827 | */ |
||
| 828 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
||
| 829 | $inputVal = $this->post($inputName); |
||
| 830 | $obj = & get_instance(); |
||
| 831 | if(! isset($obj->database)){ |
||
| 832 | return; |
||
| 833 | } |
||
| 834 | list($table, $column) = explode('.', $ruleArgs[1]); |
||
| 835 | $obj->database->from($table) |
||
| 836 | ->where($column, $inputVal) |
||
| 837 | ->get(); |
||
| 838 | $nb = $obj->database->numRows(); |
||
| 839 | if ($nb == 0) { |
||
| 840 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 841 | return; |
||
| 842 | } |
||
| 843 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 844 | } |
||
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Peform validation for the rule "is_unique" |
||
| 849 | * @param string $inputName the form field or data key name used |
||
| 850 | * @param string $ruleName the rule name for this validation ("is_unique") |
||
| 851 | * @param array $ruleArgs the rules argument |
||
| 852 | */ |
||
| 853 | protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
||
| 854 | $inputVal = $this->post($inputName); |
||
| 855 | if (! is_object($this->databaseInstance)){ |
||
| 856 | $obj = & get_instance(); |
||
| 857 | if(isset($obj->database)){ |
||
| 858 | $this->databaseInstance = $obj->database; |
||
| 859 | } else { |
||
| 860 | return; |
||
| 861 | } |
||
| 862 | } |
||
| 863 | list($table, $column) = explode('.', $ruleArgs[1]); |
||
| 864 | $this->databaseInstance->getQueryBuilder()->from($table) |
||
| 865 | ->where($column, $inputVal); |
||
| 866 | $this->databaseInstance->get(); |
||
| 867 | if ($this->databaseInstance->numRows() != 0) { |
||
| 868 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
||
| 869 | return; |
||
| 870 | } |
||
| 871 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
||
| 872 | } |
||
| 873 | } |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Peform validation for the rule "is_unique_update" |
||
| 877 | * @param string $inputName the form field or data key name used |
||
| 878 | * @param string $ruleName the rule name for this validation ("is_unique_update") |
||
| 879 | * @param array $ruleArgs the rules argument |
||
| 880 | */ |
||
| 881 | protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
||
| 906 | } |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Peform validation for the rule "in_list" |
||
| 911 | * @param string $inputName the form field or data key name used |
||
| 912 | * @param string $ruleName the rule name for this validation ("in_list") |
||
| 913 | * @param array $ruleArgs the rules argument |
||
| 914 | */ |
||
| 915 | protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
||
| 924 | } |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Peform validation for the rule "regex" |
||
| 929 | * @param string $inputName the form field or data key name used |
||
| 930 | * @param string $ruleName the rule name for this validation ("regex") |
||
| 931 | * @param array $ruleArgs the rules argument |
||
| 932 | */ |
||
| 933 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
||
| 934 | $inputVal = $this->post($inputName); |
||
| 935 | $regex = $ruleArgs[1]; |
||
| 941 | } |
||
| 942 | } |
||
| 943 | |||
| 944 | } |
||
| 945 |