Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ Algorytmy, klasy, funkcje _ [klasa]Weryfikacja pol inputu

Napisany przez: goartur 29.07.2015, 14:18:37

Witam, oto moja pierwsza klasa ktora zrobilem dla mojego malego projektu postanowilem go udostepnic aby dowiedziec sie co mam tutaj poprawic, oraz czekam na jakies rady, w OOP siedze od kolo 4/5 tygodni. Dzieki.

ValidationInput.php

  1. <?php
  2. /**
  3.  * By Artur Kupczak, This class can be used in every project, please don't remove this line.
  4.  */
  5.  
  6. class ValidationInput{
  7. http://www.php.net/static $instance = null;
  8. //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  9. // Looping inside of parameters given in the form, and then passing data to class validateRequest
  10. //
  11. // @param array $validation Value passed from requested validation
  12.  
  13. public function validateInput($validation = http://www.php.net/array()){
  14. foreach($validation as $field => $requests){
  15. foreach($requests as $validationName => $validationInfo) {
  16. $this->validateRequest($validationName,$validationInfo,$field);
  17. }
  18. }
  19. }
  20. //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  21. // Looping inside of parameters given in the form
  22. //
  23. // @param string $validationName
  24. // @param string $validationInfo
  25. // @param string $field
  26. //
  27. // Case possible options:
  28. // require booleon - If booleon true has been given then field is required otherwise the error will be displayed
  29. // preg_match String (Choice between)-
  30. // Alphabetic - Allow Numbers and Letters
  31. // Numeric - Allow Numbers Only
  32. // Letters - Allow letters Only
  33. //
  34. // minLength Numbers - The minimum length of the field
  35. // maxLength Numbers - The maximum length of the field
  36. // matchWith String - String given is a name of field that the field need to compare, For example password/passwordRepeat
  37. // filter_validate String (Choice between)-
  38. // Email - Compare field with email and return error if it's not a email
  39. //
  40. //
  41. private function validateRequest($validationName, $validationInfo, $field)
  42. {
  43. switch ($validationName) {
  44. case 'require':
  45. $validationInfo == true && http://www.php.net/empty(Input::get($field))
  46. ? ErrorHandler::getInstance()->addError("Field " . $field . " can't be empty")
  47. : false;
  48. break;
  49. case 'preg_match':
  50. switch($validationInfo){
  51. case 'alphabetic':
  52. !http://www.php.net/empty(Input::get($field)) && http://www.php.net/preg_match("/[^A-z0-9]/", Input::get($field)) == 1
  53. ? ErrorHandler::getInstance()->addError("Field " . $field . " contains prohibited characters")
  54. : false;
  55. break;
  56. case 'numeric':
  57. !http://www.php.net/empty(Input::get($field)) && http://www.php.net/preg_match("/[^0-9]/", Input::get($field)) == 1
  58. ? ErrorHandler::getInstance()->addError("Field " . $field . " can contains numbers only")
  59. : false;
  60. break;
  61. case 'letters':
  62. !http://www.php.net/empty(Input::get($field)) && http://www.php.net/preg_match("/[^A-z]/", Input::get($field)) == 1
  63. ? ErrorHandler::getInstance()->addError("Field " . $field . " can contains letters only")
  64. : false;
  65. break;
  66.  
  67. }
  68. break;
  69. case 'minLength':
  70. !http://www.php.net/empty(Input::get($field)) && http://www.php.net/strlen(Input::get($field)) < $validationInfo && http://www.php.net/strlen(Input::get($field)) >= 1
  71. ? ErrorHandler::getInstance()->addError("" . $field . " field need to be minimum " . $validationInfo . " characters")
  72. : false;
  73. break;
  74. case 'maxLength':
  75. !http://www.php.net/empty(Input::get($field)) && http://www.php.net/strlen(Input::get($field)) > $validationInfo
  76. ? ErrorHandler::getInstance()->addError("This field is longer then " . $validationInfo . " characters")
  77. : false;
  78. break;
  79. case "matchWith":
  80. Input::get($field) != Input::get($validationInfo)
  81. ? ErrorHandler::getInstance()->addError($field . " field doesn't match " . $validationInfo . " field")
  82. : false;
  83. break;
  84. case "filter_validate";
  85. switch ($validationInfo) {
  86. case "email":
  87. !http://www.php.net/empty(Input::get($field)) && !filter_var(Input::get($field), FILTER_VALIDATE_EMAIL)
  88. ? ErrorHandler::getInstance()->addError("This is not a valid E-mail")
  89. : false;
  90. break;
  91. }
  92. break;
  93. }
  94. }
  95.  
  96.  
  97.  
  98.  
  99. public http://www.php.net/static function getInstance()
  100. {
  101. if(self::$instance === null)
  102. {
  103. self::$instance = new self();
  104. }
  105.  
  106. return self::$instance;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. }


Input.php (Nie jest wymagany):
  1. class Input
  2. {
  3. public http://www.php.net/static function get($item)
  4. {
  5. if (http://www.php.net/isset($_POST[$item])) {
  6. return escape($_POST[$item]);
  7. }
  8. if (http://www.php.net/isset($_GET[$item])) {
  9. return escape($_GET[$item]);
  10. }
  11. return '';
  12. }
  13. }



ErrorHandeler.php
  1. class ErrorHandler
  2. {
  3. http://www.php.net/static $instance = null;
  4. public http://www.php.net/static $errorBooleon = false;
  5. public http://www.php.net/static $errors = http://www.php.net/array();
  6.  
  7. public function addError($newError)
  8. {
  9. self::$errorBooleon = true;
  10. self::$errors[] = $newError;
  11. }
  12.  
  13. public http://www.php.net/static function checkIfErrors()
  14. {
  15. return self::$errorBooleon;
  16. }
  17.  
  18. public http://www.php.net/static function returnErrors()
  19. {
  20. return self::$errors;
  21.  
  22. }
  23.  
  24. public http://www.php.net/static function getInstance()
  25. {
  26. if (self::$instance === null) {
  27. self::$instance = new self();
  28. }
  29.  
  30. return self::$instance;
  31. }
  32.  
  33.  
  34. }


Przykladowe uzycie :
  1. if(Input::get("register") ){
  2.  
  3. ValidationInput::getInstance()->validateInput(
  4. $require = http://www.php.net/array(
  5. "username" => http://www.php.net/array( //username to name inputu
  6. 'require' => true,
  7. 'minLength' => 3,
  8. 'maxLength' => 15
  9. )
  10.  
  11. )
  12. )
  13. if(ErrorHandler::checkIfErrors() == false ){
  14. http://www.php.net/echo 'success';
  15. }
  16.  
  17. if (ErrorHandler::checkIfErrors()) {
  18. foreach (ErrorHandler::returnErrors() as $error) {
  19. http://www.php.net/echo $error . '</Br>';
  20. ErrorHandler::$errorBooleon = false;
  21. }
  22. }

  1. <http://december.com/html/4/element/form.html method="post">
  2. <http://december.com/html/4/element/input.html type="text" name="username"/>
  3. <http://december.com/html/4/element/input.html type="submit" name="register"/>
  4. </http://december.com/html/4/element/form.html>

Napisany przez: nospor 29.07.2015, 14:40:28

Wyobraź sobie, ze masz pole "Ilosc dzieci". Dajesz to pole jako required - trzeba je uzupelnic. I ktos nie ma dzieci.... i zonk, nie uzupelni Twojego forma, albo bedzie musiał skłamać i wpisać 1 biggrin.gif

Napisany przez: goartur 29.07.2015, 14:41:54

Cytat(nospor @ 29.07.2015, 15:40:28 ) *
Wyobraź sobie, ze masz pole "Ilosc dzieci". Dajesz to pole jako required - trzeba je uzupelnic. I ktos nie ma dzieci.... i zonk, nie uzupelni Twojego forma, albo bedzie musiał skłamać i wpisać 1 biggrin.gif



Nie do konca rozumie ? Jeszcze raz oneeyedsmiley02.png

Napisany przez: nospor 29.07.2015, 14:49:43

Pole required sprawdzasz tak:
empty(Input::get($field))

No iteraz dajesz pole, ktore jest wymagane, znaczy ze trzeba cos wpisac, ale w to pole mozna wpisac 0 - to jest poprawna wartosc akurat dla tego pola. A Twoj walidator odrzuci to i nie przyjmie tego pola, gdyz empty(0) daje true, tak samo jak empty od pustego pola.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)