Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klasa walidacji, Nie dziala jesli jest wiecej zmiennych
marcio
post 3.11.2009, 00:30:06
Post #1





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Witam napisalem sobie szkielet klasy do walidacji formularzy.
Prosze nie patrzec na sam kod ani ze sa error'y na sztywno bo to sie zmienie lecz na sama logike:
  1. <?php
  2.  
  3. class Validation {
  4.  
  5. protected $type;
  6.  
  7. public $rules = array();
  8.  
  9. public $msg;
  10.  
  11. //$fieldname = 'login'
  12. //$label = 'Username'
  13. //$rules = lenght => 1,4
  14.  
  15. public function __construct($type = '') {
  16.  
  17. if(isset($type) && !empty($type)) {
  18.  
  19. $this -> type = $type;
  20.  
  21. }
  22.  
  23. else {
  24.  
  25. $this -> type = 'post';
  26.  
  27. }
  28.  
  29. }
  30.  
  31. public function getTypeValidation() {
  32.  
  33. return $this -> type;
  34.  
  35. }
  36.  
  37. public function Submitted() {
  38.  
  39. $submitType = ($this -> getTypeValidation() == 'post') ? $_POST : $_GET;
  40.  
  41. if(count($submitType) == 0)
  42. return false;
  43.  
  44. else
  45. return true;
  46.  
  47. }
  48.  
  49. public function AddRules($fieldname, $label, $rules = array()) {
  50.  
  51. if(empty($label)) {
  52.  
  53. $label = $fieldname;
  54.  
  55. }
  56.  
  57. if(empty($fieldname)) {
  58.  
  59. $fieldname = $label;
  60.  
  61. }
  62.  
  63. $submitType = ($this -> getTypeValidation() == 'post') ? $_POST : $_GET;
  64.  
  65. $this -> rules[$fieldname] = array($submitType[$fieldname] => $rules);
  66.  
  67. }
  68.  
  69.  
  70. public function Validation() {
  71.  
  72. foreach($this -> rules as $rule) {
  73.  
  74. foreach($rule as $post => $validRule) {
  75.  
  76. $methodRule = explode('|', $validRule);
  77. $methodRule = $methodRule[count($methodRule)-1];
  78.  
  79. switch($methodRule) {
  80.  
  81. case 'required':
  82.  
  83. if(method_exists(get_class($this), $methodRule)) {
  84.  
  85. $required = $this -> $methodRule($post);
  86.  
  87. }
  88.  
  89. if($required)
  90. return true;
  91.  
  92. else {
  93.  
  94. $key = array_keys($this -> rules);
  95. $this -> msg = 'Pole '.$key[0].' nie moze byc puste';
  96.  
  97. }
  98.  
  99. break;
  100.  
  101. }
  102.  
  103. }
  104.  
  105. }
  106.  
  107. }
  108.  
  109.  
  110. protected function required($string) {
  111.  
  112. return (!is_null($string) && $string != false && !empty($string)) ? true : false;
  113.  
  114. }
  115.  
  116.  
  117. }
  118.  
  119. ?>

Testuje jak narazie tylko na jednej funkcji analogicznie potem bedzie dzialalo i na reszcie.
No i robie taki maly test:
  1. $_POST['logins'] = 'vodka';
  2. $_POST['haslos'] = '';
  3.  
  4. $valid = new Validation;
  5. $valid -> AddRules('logins', 'username', 'required');
  6. $valid -> AddRules('haslos', 'username', 'required');
  7.  
  8. $valid -> Validation();
  9. if(!empty($valid -> msg)) echo('cos jest nie tak');
  10. else echo('Wszystko good');
  11.  
  12. print_r($valid);

Ten print_r() daje mi:
Cytat
Validation Object ( [type:protected] => post [rules] => Array ( [logins] => Array ( [vodka] => required ) [haslos] => Array ( [] => required ) ) [msg] => )

No i jak widac i msg jest puste i wyswietli mi sie info ze wszystko good.
Gdy ustawie tylko jedna zmienna np logins i ustawie ja jako pusta to dziala wyswietla error dla tego pola lub gdy nie jest puste nie wyswietla wiec robie cos zle w funckji Validation() jednak nie wiem zabardzo co chyba ze ogolnie zle zaprojektowalem tablice z danymi.

Prosze o pomoc i z gory dzieki.

EDIT:
Teraz tak przegladajac post z drugiego kompa zauwazylem ze jest blad:
  1. $methodRule = $methodRule[count($methodRule)-1];

Powinno byc:
  1. $methodRule = $methodRule[0];

Podajac jednak tylko required jako 3 argument to nic nie zmienia.

Ten post edytował marcio 3.11.2009, 01:43:29


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
Nattfarinn
post 3.11.2009, 10:28:49
Post #2





Grupa: Zarejestrowani
Postów: 136
Pomógł: 22
Dołączył: 19.09.2007
Skąd: Sosnowiec

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


Twoja sekcja:
  1. case 'required':
  2. (...)

Jest źle napisana. Sprawdzanie walidacji jest ograniczone do pierwszego poprawnego elementu (linia 89 i 90: return true;). Ten warunek jest tam zupełnie nie potrzebny. Nie zagłębiając się w resztę:
  1. (...)
  2. public function Validation() {
  3. foreach($this -> rules as $key => $rule) {
  4. foreach($rule as $post => $validRule) {
  5. $methodRule = explode('|', $validRule);
  6. $methodRule = $methodRule[count($methodRule)-1];
  7. switch($methodRule) {
  8. case 'required':
  9. if(method_exists(get_class($this), $methodRule)) {
  10. $required = $this -> $methodRule($post);
  11. if(!$required) {
  12. $this -> msg = 'Pole '.$key.' nie moze byc puste';
  13. }
  14. }
  15. break;
  16. }
  17. }
  18. }
  19. }
  20. (...)

I będzie śmigało... winksmiley.jpg

Ten post edytował Nattfarinn 3.11.2009, 10:29:24


--------------------
Code should run as fast as necessary, but no faster; something important is always traded away to increase speed.
-- R. Pattis
Go to the top of the page
+Quote Post
marcio
post 3.11.2009, 11:14:20
Post #3





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Ogolnie zrobilem dokladnie to co ty, spojrzalem jeszcze raz na klasy walidacji z kohany,CI i Mohebo i wtedy sie skaplem ze fakt faktem return konczy dzialanie metody validation() eh bylo juz pozno i robilem takie byki ze hej.




Ale dzieki za fatyge masz + tylko dzis dokoncze klase i jak bede mial jeszcze jakies watpliwosci to napisze.



--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 24.07.2025 - 16:13