Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Problem z klasą walidacji formularzy
Forum PHP.pl > Forum > Przedszkole
mentoos
Mam problem z klasą do walidacji formularzy.

Jeśli chodzi o sprawdzanie czy pole jest puste, to wszystko jest ok. Sprawdza bez najmniejszego problemu.
Niestety, przy sprawdzaniu czy liczba znaków w polu formularza zgadza się z tą którą ustawiłem w metodzie, sprawdzanie w ogóle nie działa.
Dotyczy to metody min_length() i max_length().

Co może być powodem, że nie działa sprawdzanie długości znaków w formularzu?
Czy ta klasa pod względem kodu jest w miarę dobra?

Klasa:
  1. <?php
  2. /**
  3.  * The Form Validation class.
  4.  *
  5.  * @author
  6.  * @copyright
  7.  * @license
  8.  * @version
  9.  */
  10.  
  11. class Validate
  12. {
  13.  
  14. /**
  15.   * @property array $name The names of form fields
  16.   */
  17.  
  18. private $name;
  19.  
  20. /**
  21.   *@property array $rules The rules of form validation
  22.   *
  23.   */
  24. private $rules = array();
  25.  
  26. /**
  27.   * @property array $param The parameters of the rule
  28.   */
  29.  
  30. private $param = array();
  31.  
  32. /**
  33.   * @property array $method The method of form send
  34.   */
  35.  
  36. private $method = array();
  37.  
  38. /**
  39.   * @property string $filters The filters to apply
  40.   */
  41. private $filters;
  42.  
  43. /**
  44.   * @property array $errors The errors from validation
  45.   *
  46.   */
  47. public $errors;
  48.  
  49. public function __construct(array $method)
  50. {
  51. $this-> method = $method;
  52. }
  53.  
  54. /**
  55.   * @method bool not_empty($value) checks if a field is empty
  56.   * @property array $value The field's name
  57.   */
  58.  
  59. private function not_empty($value)
  60. {
  61. foreach ($value as $field)
  62. {
  63. if (empty($this-> method[$field]))
  64. {
  65. return false;
  66. }
  67. }
  68.  
  69. return true;
  70. }
  71.  
  72. private function min_length($value, $length)
  73. {
  74. foreach ($value as $post)
  75. {
  76. foreach ($length as $param)
  77. {
  78. return strlen($this-> method[$post]) >= $param;
  79. }
  80. }
  81. }
  82.  
  83. private function max_length($value, $length)
  84. {
  85. foreach ($value as $post)
  86. {
  87. foreach ($length as $param)
  88. {
  89. return strlen($this-> method[$post]) <= $param;
  90. }
  91. }
  92. }
  93.  
  94. /**
  95.   * @method object rule() The rules of form validation
  96.   * @param string $field_name the form field's name
  97.   * @param string $rule the rule for the form field
  98.   */
  99. public function rule($field, $rule, $param = NULL)
  100. {
  101. $this-> name[$field] = $field;
  102. $this-> rule[$field] = $rule;
  103. $this-> param[$field] = $param;
  104.  
  105. return $this;
  106. }
  107.  
  108. public function check()
  109. {
  110. foreach ($this-> rule as $option)
  111. {
  112. switch ($option)
  113. {
  114. case 'not_empty':
  115. if (! $this-> not_empty($this-> name))
  116. {
  117. return false;
  118. }
  119. break;
  120. case 'min_length':
  121. if (! $this-> min_length($this-> name, $this-> param))
  122. {
  123. return false;;
  124. }
  125. break;
  126. case 'max_length':
  127. if (! $this-> max_length($this-> name, $this-> param))
  128. {
  129. return false;
  130. }
  131. break;
  132. }
  133. }
  134.  
  135. return true;
  136. }
  137.  
  138. }
  139.  
  140. ?>


Przykład:

  1. <?php
  2.  
  3. require('classes/validate.php');
  4.  
  5. $post = new Validate($_POST);
  6. $post-> rule('username', 'not_empty');
  7. $post-> rule('password', 'not_empty');
  8. $post-> rule('password', 'min_length', 5);
  9.  
  10. if ($post-> check())
  11. {
  12. echo 'OK.';
  13. }
  14. else
  15. {
  16. echo 'Error.';
  17.  
  18. }
  19.  
  20. ?>


  1. <!DOCTYPE HTML>
  2. <title>A title</title>
  3. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  4.  
  5. </head>
  6.  
  7. <form action="post.php" method="post">
  8. <div>
  9. <label>Username <input type="text" name="username" /></label>
  10. <label>Password <input type="password" name="password" /></label>
  11. <input type="submit" value="Sign in" />
  12. </div>
  13. </form>
  14.  
  15. </body>
  16. </html>
nitr0
Może to nie jest bezpośrednio odpowiedź na Twoje pytanie, ale może Ci pomóc.

Zamiast tworzyć od podstaw swoją walidacje, ja wykorzystałem

jQuery - Validation
http://docs.jquery.com/Plugins/Validation

Są w nim dostępne różne sposoby walidacji, między innymi ilość znaków min/max. A użycie jest dość proste jak już pojmie się zasade działania.

Powodzenia.
jareeny
tak nitr0, a potem wystarczy wyłączyć obsługę javascript i tyle z Twojej walidacji smile.gif
nitr0
W sumie fakt, ale na ponad 100 osób zarejestrowanych tylko 1 wpis był "bezsensu". Więc chyba się sprawdziło.

Najlepiej było by zabezpieczyć formularz w oba sposoby, tak więc, pomóżcie koledze. smile.gif
nospor
Cytat
W sumie fakt, ale na ponad 100 osób zarejestrowanych tylko 1 wpis był "bezsensu". Więc chyba się sprawdziło.
Tu nie chodzi o te 99 osob. Tu chodzi o tę jedną osobę, która akurat może byc hakerem i zrobic co jej się zywnie podoba, bo ty masz tylko walidację js.
jareeny
walidację w JS można robić jako estetyczny dodatek do strony, ale po stronie serwera MUSI być.

klasa jest jakas zagmatwana, skoro masz funkcję check(), to czemu ilość znaków sprawdzasz już w min_length i max length? Ja w ogóle nie mogę zrozumieć kodu, naprawdę
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.