Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problemy z walidacją OOP
Panicz74
post
Post #1





Grupa: Zarejestrowani
Postów: 39
Pomógł: 1
Dołączył: 28.08.2015

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


Witam,

Mam problem z formularzem rejestracji. Walidacja działa jedynie w przypadku pola 'username', w pozostałych przypadkach niestety nie. Zupełnie nie mam pojęcia gdzie może być błąd. Może wy coś doradzicie?

register.php
  1. <?php
  2.  
  3. require_once 'core/init.php';
  4.  
  5. if(Input::exists())
  6. {
  7. $validate = new Validate();
  8. $validation = $validate->check($_POST, array(
  9. 'username' => array(
  10. 'required' => true,
  11. 'min' => 2,
  12. 'max' => 20,
  13. 'unique' => 'users'
  14. ),
  15. 'password' => array(
  16. 'required' => true,
  17. 'min' => 6,
  18. ),
  19. 'password_again' => array(
  20. 'required' => true,
  21. 'matches' => 'password'
  22. ),
  23. 'name' => array(
  24. 'required' => true,
  25. 'min' => 2,
  26. 'max' => 20
  27. ),
  28. ));
  29.  
  30. if($validation->passed())
  31. {
  32. echo "Registered";
  33. }
  34. else
  35. {
  36. print_r($validation->errors());
  37. }
  38. }
  39.  
  40. ?>


validate.php
  1. <?php
  2. class Validate
  3. {
  4. private $_passed = false;
  5. private $_errors = array();
  6. private $_db = null;
  7.  
  8. public function __construct()
  9. {
  10. $this->_db = DB::getInstance();
  11. }
  12.  
  13. public function check($source, $items = array())
  14. {
  15. foreach($items as $item => $rules)
  16. {
  17. foreach($rules as $rule => $rule_value)
  18. {
  19. $value = $source[$item];
  20.  
  21. if($rule === 'required' && empty($value))
  22. {
  23. $this->addError("{$item} is required");
  24. }
  25. else
  26. {
  27. $this->_passed = true;
  28. }
  29. return $this;
  30. }
  31. }
  32. if(empty($this->_errors))
  33. {
  34. $this->_passed = true;
  35. }
  36. //return $this;
  37. }
  38.  
  39. private function addError($error)
  40. {
  41. $this->_errors[] = $error;
  42. }
  43.  
  44. public function errors()
  45. {
  46. return $this->_errors;
  47. }
  48.  
  49. public function passed()
  50. {
  51. return $this->_passed;
  52. }
  53. }
  54. ?>
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: 23.08.2025 - 11:02