Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [slrypt] validator formularzy
lukaskolista
post
Post #1





Grupa: Zarejestrowani
Postów: 872
Pomógł: 94
Dołączył: 31.03.2010

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


Witam. Jest to moj pierwszy walidator, dlatego prosze o konstruktywne opinie i sugestie, co poprawic

  1. <?php
  2.  
  3. // Klasa z podstawowymi metodami walidacji
  4. class valid {
  5.  
  6. public function required($value) {
  7. return !empty($value);
  8. }
  9.  
  10. public function alpha($value) {
  11. return (bool) preg_match('/[a-zA-Z]/', $value);
  12. }
  13.  
  14. }
  15.  
  16. // Klasa do walidacji konkretnego formularza, w tym przypadku formularza logowania
  17. class user_valid extends valid {
  18.  
  19. public function username_valid($value) {
  20. return false;
  21. }
  22.  
  23. public function errors() {
  24. return array(
  25. 'username' => array(
  26. 'required' => 'Musisz podac nazwe uzytkownika',
  27. 'alpha' => 'Nazwa moze skladac sie jedynie ze znakow alfabetu',
  28. 'username_valid' => 'Test obiektow',
  29. ),
  30. 'password' => array(
  31. 'not_sended' => 'Nie otrzymano hasla',
  32. 'required' => 'Musisz podac haslo',
  33. ),
  34. );
  35. }
  36. }
  37.  
  38.  
  39. // Klasa validacji
  40. class validation {
  41.  
  42. private
  43. $fields = array(),
  44. $rules = array(),
  45. $errors = array(),
  46. $valid_object;
  47.  
  48. public function __construct($post, $object_name) {
  49. $this->fields = $post;
  50. $this->valid_object = new $object_name;
  51. }
  52.  
  53. public function add_rules($field_name, $rules) {
  54. if (isset($this->rules[$field_name])) {
  55. $this->rules[$field_name] = array_merge($this->rules[$field_name], $rules);
  56. } else {
  57. $this->rules[$field_name] = $rules;
  58. }
  59. }
  60.  
  61. public function add_error($field_name, $error_name) {
  62. if (isset($this->errors[$field_name])) {
  63. $this->errors[$field_name][] = $error_name;
  64. } else {
  65. $this->errors[$field_name] = array($error_name);
  66. }
  67. }
  68.  
  69. public function validate() {
  70. foreach ($this->rules as $field_name => $rules) {
  71. foreach ($rules as $method) {
  72. if (isset($this->fields[$field_name])) {
  73. if ($this->valid_object->$method($this->fields[$field_name]) === false) {
  74. $this->add_error($field_name, $method);
  75. }
  76. } else {
  77. $this->add_error($field_name, 'not_sended');
  78. }
  79. }
  80. }
  81. return empty($this->errors);
  82. }
  83.  
  84. public function errors() {
  85. $errors_array = $this->valid_object->errors();
  86. $output_errors = array();
  87. foreach ($this->errors as $field_name => $errors) {
  88. foreach ($errors as $error) {
  89. if (isset($errors_array[$field_name])) {
  90. $output_errors[$field_name][] = $errors_array[$field_name][$error];
  91. } else {
  92. $output_errors[$field_name] = array($errors_array[$field_name][$error]);
  93. }
  94. }
  95. }
  96.  
  97. return $output_errors;
  98. }
  99.  
  100. }
  101.  
  102.  
  103. // Obsluga
  104. $post = array(
  105. 'username' => '123',
  106.  
  107. );
  108.  
  109. $validation = new validation($post, 'user_valid');
  110. $validation->add_rules('username', array('required', 'alpha', 'username_valid'));
  111. $validation->add_rules('password', array('required'));
  112. if ($validation->validate()) {
  113. echo 'Poprawna nazwa';
  114. } else {
  115. echo 'Bledy:';
  116. echo '<ul>';
  117. foreach ($validation->errors() as $field_name => $errors) {
  118. foreach ($errors as $error) {
  119. echo '<li>'.$error.'</li>';
  120. }
  121. }
  122. echo '</ul>';
  123. }


Umiescilem wszystko w 1 pliku, aby bylo latwiej. Domyslnie klasy _valid beda w osobnym folderze
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
phpion
post
Post #2





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Albo mi się wydaje, albo zaczerpnąłeś "inspirację" z Kohany. Tak czy siak interesuje mnie czy da się (a chyba nie) napisać walidator matches, czyli sprawdzający czy podane pola mają taką samą wartość (najczęściej wykorzystywany do sprawdzania podanych haseł).
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: 5.10.2025 - 19:13