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:
<?php /** * The Form Validation class. * * @author * @copyright * @license * @version */ class Validate { /** * @property array $name The names of form fields */ private $name; /** *@property array $rules The rules of form validation * */ /** * @property array $param The parameters of the rule */ /** * @property array $method The method of form send */ /** * @property string $filters The filters to apply */ private $filters; /** * @property array $errors The errors from validation * */ public $errors; { $this-> method = $method; } /** * @method bool not_empty($value) checks if a field is empty * @property array $value The field's name */ private function not_empty($value) { foreach ($value as $field) { { return false; } } return true; } private function min_length($value, $length) { foreach ($value as $post) { foreach ($length as $param) { } } } private function max_length($value, $length) { foreach ($value as $post) { foreach ($length as $param) { } } } /** * @method object rule() The rules of form validation * @param string $field_name the form field's name * @param string $rule the rule for the form field */ public function rule($field, $rule, $param = NULL) { $this-> name[$field] = $field; $this-> rule[$field] = $rule; $this-> param[$field] = $param; return $this; } public function check() { foreach ($this-> rule as $option) { switch ($option) { case 'not_empty': if (! $this-> not_empty($this-> name)) { return false; } break; case 'min_length': if (! $this-> min_length($this-> name, $this-> param)) { return false;; } break; case 'max_length': if (! $this-> max_length($this-> name, $this-> param)) { return false; } break; } } return true; } } ?>
Przykład:
<?php require('classes/validate.php'); $post = new Validate($_POST); $post-> rule('username', 'not_empty'); $post-> rule('password', 'not_empty'); $post-> rule('password', 'min_length', 5); if ($post-> check()) { } else { } ?>