Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Validator, klasa, php5
Strzałek
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 384
Pomógł: 6
Dołączył: 11.09.2004
Skąd: Grodzisk Mazowiecki

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


Prosta klasa do walidowania danych pochodzących od użytkownika.

  1. <?php
  2. /**
  3.  * Validator
  4.  * 
  5.  * @author Strzałek
  6.  * @package Tellur
  7.  * @subpackage Model
  8.  * @version 1.0.0
  9. */
  10.  
  11.  class Validator {
  12.  
  13.  /**
  14. * Bledy ktore wystpaia podczas
  15. * walidacji danych
  16. *
  17. * @var array
  18. */
  19.  private $errors = array();
  20.  
  21.  /**
  22. * Stan walidacji
  23. *
  24. * @var bool
  25. */
  26.  private $valid = true;
  27.  
  28.  /**
  29. * Metoda sprawdzajaca dlugosc
  30. *
  31. * @param string $string
  32. * @param int $min
  33. * @param int $max
  34. * @param string $error
  35. * @return bool
  36. */
  37.  public function lenght($string, $min, $max, $error){
  38.  
  39.  $lenght = strlen($string);
  40.  
  41.  if($lenght > $min){  
  42.  
  43.  if($lenght < $max){
  44. return true;
  45.  }else{
  46. return $this -> setError($error);
  47.  }
  48.  
  49.  }else{
  50. return $this -> setError($error);
  51.  }
  52.  
  53.  }
  54.  
  55.  /**
  56. * Metoda sprawdzajaca identycznosc
  57. * dwoch stringow
  58. *
  59. * @param string $string1
  60. * @param string $string2
  61. * @param string $error
  62. * @return bool
  63. */
  64.  public function same($string1, $string2, $error){
  65.  
  66.  if($string1 == $string2){
  67. return true;
  68.  }else{
  69. return $this -> setError($error);
  70.  }
  71.  
  72.  }  
  73.  
  74.  /**
  75. * Sprawdzanie stringa wyrazeniem
  76. * regularnym
  77. *
  78. * @param string $string
  79. * @param string $regex
  80. * @param string $error
  81. * @return bool
  82. */
  83.  public function pattern($string, $regex, $error){
  84.  
  85.  if(preg_match($regex, $string)){
  86. return true;
  87.  }else{
  88. return $this -> setError($error);
  89.  }
  90.  
  91.  }  
  92.  
  93.  /**
  94. * Sprawdzanie czy zwalidowano
  95. *
  96. * @return bool
  97. */
  98.  public function isValid(){
  99.  return $this -> valid;
  100.  }
  101.  
  102.  /**
  103. * Ustawienie bledy walidacji
  104. *
  105. * @param string $message
  106. * @return bool
  107. */
  108.  private function setError($message){
  109.  $this -> errors[] = $message;
  110.  return $this -> valid = false;
  111.  }
  112.  
  113.  /**
  114. * Zwrocenie tablicy z bledami
  115. *
  116. * @return array
  117. */
  118.  public function getErrors(){
  119.  return $this -> errors;
  120.  }
  121.  
  122.  }
  123.  
  124.  /* Przykład użycia */
  125.  
  126.  $v = new Validator();
  127.  $v -> lenght('L', 2, 32, 'Login musi mieć od 2 do 32 liter');
  128.  $v -> same('moje1tajne2hasl', 'moje1tajne2haslo', 'Hasla nie sa identyczne');
  129.  $v -> pattern('heniek@ze-wsi.com', '#^[A-Za-z0-9._-]+@[[A-Za-z0-9.-]+$#', 'To nie jest poprawny adres email');
  130.  
  131.  if($v -> isValid()){
  132.  //reestrujemy Heńka
  133.  echo 'rejestracja';
  134.  }else{
  135.  var_dump($v -> getErrors()); //wyswietlamy błedy ;)
  136.  }
  137.  
  138.  
  139. ?>
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 22.08.2025 - 22:37