Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa]Weryfikacja pol inputu, Bardziej do poprawy i oceny
goartur
post 29.07.2015, 14:18:37
Post #1





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

Ostrzeżenie: (0%)
-----


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. 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 = 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 && 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. !empty(Input::get($field)) && 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. !empty(Input::get($field)) && 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. !empty(Input::get($field)) && 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. !empty(Input::get($field)) && strlen(Input::get($field)) < $validationInfo && 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. !empty(Input::get($field)) && 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. !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 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 static function get($item)
  4. {
  5. if (isset($_POST[$item])) {
  6. return escape($_POST[$item]);
  7. }
  8. if (isset($_GET[$item])) {
  9. return escape($_GET[$item]);
  10. }
  11. return '';
  12. }
  13. }



ErrorHandeler.php
  1. class ErrorHandler
  2. {
  3. static $instance = null;
  4. public static $errorBooleon = false;
  5. public static $errors = array();
  6.  
  7. public function addError($newError)
  8. {
  9. self::$errorBooleon = true;
  10. self::$errors[] = $newError;
  11. }
  12.  
  13. public static function checkIfErrors()
  14. {
  15. return self::$errorBooleon;
  16. }
  17.  
  18. public static function returnErrors()
  19. {
  20. return self::$errors;
  21.  
  22. }
  23.  
  24. public 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 = array(
  5. "username" => array( //username to name inputu
  6. 'require' => true,
  7. 'minLength' => 3,
  8. 'maxLength' => 15
  9. )
  10.  
  11. )
  12. )
  13. if(ErrorHandler::checkIfErrors() == false ){
  14. echo 'success';
  15. }
  16.  
  17. if (ErrorHandler::checkIfErrors()) {
  18. foreach (ErrorHandler::returnErrors() as $error) {
  19. echo $error . '</Br>';
  20. ErrorHandler::$errorBooleon = false;
  21. }
  22. }

  1. <form method="post">
  2. <input type="text" name="username"/>
  3. <input type="submit" name="register"/>
  4. </form>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
nospor
post 29.07.2015, 14:40:28
Post #2





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




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


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
goartur
post 29.07.2015, 14:41:54
Post #3





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
nospor
post 29.07.2015, 14:49:43
Post #4





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




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.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 16:39