Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa do walidacji formularza - opinia
Forum PHP.pl > Forum > Gotowe rozwiązania
moto0095
Witam. Napisałem ostatnio klasę do walidacji formularza.
I prosiłbym o opinię oraz porady np: co trzeba/można zmienić.
  1. <?
  2. class FormValidator {
  3. private $error = array();
  4. private $elem = array();
  5.  
  6. public function validate($name, $error = '', $filter = array()) {
  7. $this->error = $error;
  8. foreach($filter as $key => $value) {
  9. $this->filter($name, $key, $value);
  10. }
  11. }
  12.  
  13. private function filter($name, $filter, $par = FALSE) {
  14. switch($filter) {
  15. case 'req' :
  16. if(!isset($name) || empty($name) || $par == TRUE) {
  17. $this->set_error('Pole jest wymagane i nie może pozostać puste.');
  18. }
  19. break;
  20.  
  21. case 'email' :
  22. if(!filter_var($name, FILTER_VALIDATE_EMAIL) || $par == TRUE) {
  23. $this->set_error('Podany adres e-mail jest niepoprawny');
  24. }
  25. break;
  26.  
  27. case 'url' :
  28. if(!filter_var($name, FILTER_VALIDATE_URL) || $par == TRUE) {
  29. $this->set_error('Podany adres URL jest niepoprawny');
  30. }
  31. break;
  32.  
  33. case 'num' :
  34. if(!preg_match('/^[0-9 ]+$/', $name) || $par == TRUE) {
  35. $this->set_error('Ple musi zawierać wyłącznie znaki numeryczne');
  36. }
  37. break;
  38.  
  39. case 'alfa' :
  40. if(!preg_match('/^[a-zA-Z ]+$/', $name) || $par == TRUE) {
  41. $this->set_error('Ple musi zawierać wyłącznie litery');
  42. }
  43. break;
  44.  
  45. case 'alnum' :
  46. if(!preg_match('/^[a-zA-Z0-9 ]+$/', $name) || $par == TRUE) {
  47. $this->set_error('Ple musi zawierać wyłącznie znaki alfanumeryczne');
  48. }
  49. break;
  50.  
  51. case 'max' :
  52. if(strlen(trim($name)) > $par) {
  53. $this->set_error('Pole musi mieć maxymalną długość '.$par.'.');
  54. }
  55. break;
  56.  
  57. case 'min' :
  58. if(strlen(trim($name)) < $par) {
  59. $this->set_error('Pole musi mieć minimalną długość '.$par.'.');
  60. }
  61. break;
  62.  
  63. case 'length' :
  64. if(strlen(trim($name)) == $par) {
  65. $this->set_error('Pole musi mieć mieć długość '.$par.'.');
  66. }
  67. break;
  68.  
  69. /* ORAZ INNE FILTRY */
  70. }
  71. }
  72.  
  73. private function set_error($str) {
  74. $this->errors[] = (empty($this->error) ? $str : '<b>'.$this->error.'</b> : '.$str);
  75. }
  76.  
  77. public function get_error($str) {
  78. return $this->errors[$str];
  79. }
  80.  
  81. public function get_errors() {
  82. foreach($this->errors as $err) {
  83. echo $err.'<br />';
  84. }
  85. }
  86.  
  87. public function is_valid() {
  88. return (count($this->errors) > 0) ? FALSE : TRUE;
  89. }
  90.  
  91. }
  92. ?>


Przykład użycia:

  1. include_once('class.form_validator.php');
  2.  
  3. $val = new FormValidator;
  4.  
  5. $val->validate($_POST['email'], 'Email', array('req' => TRUE, 'min' => 10, 'email' => TRUE));
  6.  
  7. if($val->is_valid()) {
  8. echo 'OK';
  9. }else {
  10. $val->get_errors();
  11. }
ShadowD
Klasa nie powinna mieć na sztywno odpowiedzi - to na pewno, może jakieś kody błędów?

Ponadto nie podoba mi się zwrot - 'email' => TRUE
Szczerze pomyślał bym nad możliwością wstawienia metody, mail, nr itd niż przesyłania w array - ale to moje widzimisie i nie wszystkim pewnie by odpowiadało. :-)
moto0095
Czyli lepiej by było :
  1. include_once('class.form_validator.php');
  2.  
  3. $val = new FormValidator;
  4.  
  5. $val->validate($_POST['email'], 'Pole email jest wymagane i nie może pozostać puste', 'req');
  6. $val->validate($_POST['email'], 'Pole email musi mieć minimum 10 znaków długości', 'min', 10);
  7. $val->validate($_POST['email'], 'Pole email zawiera błędny email', 'email');
  8.  
  9. if($val->is_valid()) {
  10. echo 'OK';
  11. }else {
  12. $val->get_errors();
  13. }

questionmark.gif

Ok. Poprawiłem trochę klasę. Teraz wygląda tak:
(Proszę o opinię i porady)
  1. <?
  2. class FormValidator {
  3. private $errors = array();
  4. private $field;
  5. private $label;
  6.  
  7. ### FILTRY ###
  8.  
  9. public function email($message = '') {
  10. $message = (empty($message)) ? 'Pole %s nie zawiera adresu email.' : $message;
  11. if(!filter_var($this->field, FILTER_VALIDATE_EMAIL)) {
  12. $this->register_error($message);
  13. }
  14. return $this;
  15. }
  16.  
  17. public function required($message = '') {
  18. $message = (empty($message)) ? 'Pole %s jest wymagane i nie może pozostać puste.' : $message;
  19. if(!isset($this->field) || empty($this->field)) {
  20. $this->register_error($message);
  21. }
  22. return $this;
  23. }
  24.  
  25. public function numeric($message = '') {
  26. $message = (empty($message)) ? 'Pole %s może zawierać wyłącznie znaki numeryczne.' : $message;
  27. if(!preg_match('/^[0-9 ]+$/', $this->field)) {
  28. $this->register_error($message);
  29. }
  30. return $this;
  31. }
  32.  
  33. public function minlength($minlen, $message = '') {
  34. $message = (empty($message)) ? 'Pole %s może mieć minimalną długość '.$minlen.'.' : $message;
  35. if(strlen(trim($this->field)) > $minlen) {
  36. $this->register_error($message);
  37. }
  38. return $this;
  39. }
  40.  
  41. public function maxlength($maxlen, $message = '') {
  42. $message = (empty($message)) ? 'Pole %s może mieć maxymalną długość '.$maxlen.'.' : $message;
  43. if(strlen(trim($this->field)) < $maxlen) {
  44. $this->register_error($message);
  45. }
  46. return $this;
  47. }
  48.  
  49. public function length($len, $message = '') {
  50. $message = (empty($message)) ? 'Pole %s musi mieć wartość '.$len.'.' : $message;
  51. if(strlen(trim($string)) !== $len) {
  52. $this->register_error($message);
  53. }
  54. return $this;
  55. }
  56.  
  57. public function matches($field, $label, $message='') {
  58. $message = (empty($message)) ? 'Pole %s nie zgadza się z polem '.$label.'.' : $message;
  59. if((string)$field !== (string)$this->field ) {
  60. $this->register_error($message);
  61. }
  62. return $this;
  63. }
  64.  
  65. ### /FILTRY ###
  66.  
  67. public function validate($key, $label = '') {
  68. $this->field = $key;
  69. $this->label = $label;
  70. return $this;
  71. }
  72.  
  73. private function register_error($message) {
  74. $message = (empty($message) ? '%s zawiera błędy.' : $message);
  75. $label = (empty($this->label) ? 'Formularz' : $this->label);
  76. $this->errors[] = sprintf($message, $label);
  77. }
  78.  
  79. public function get_error($key) {
  80. echo $this->errors[(int)$key];
  81. }
  82.  
  83. public function get_all_errors() {
  84. foreach($this->errors as $err) {
  85. echo $err.'<br />';
  86. }
  87. }
  88.  
  89. public function is_valid() {
  90. return (count($this->errors) < 0) ? TRUE : FALSE;
  91. }
  92.  
  93. }
  94. ?>

Przykład użycia:
  1. $f = new FormValidator;
  2.  
  3. $f->validate($_POST['email'], 'Email')->email('ERROR')->maxlength(1)->required()->numeric();
  4.  
  5.  
  6. if($f->is_valid()) {
  7. echo 'OK';
  8. }else {
  9. $f->get_all_errors();
  10. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.