Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Problem z walidacja danych
lukasz_jaw
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 9.04.2012

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


Witam
Mam pewny przyklad z ksiazki 'PHP5 Zaawansowane programowanie' ktory nie moge rozwiazac problemu odnosnie sprawdzenia walidacji zmiennych.
plik class.Address.php

CODE

<?php
require_once('class.PropertyObject.php');

class Address extends PropertyObject {

function __construct($addressid) {
$arData = DataManager::getAddressData($addressid);

parent::__construct($arData);

$this->propertyTable['addressid'] = 'adres_id';
$this->propertyTable['id'] = 'adres_id';
$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['address1'] = 'sadres1';
$this->propertyTable['address2'] = 'sadres2';
$this->propertyTable['city'] = 'smiasto';
$this->propertyTable['zipcode'] = 'skod';
$this->propertyTable['type'] = 'styp';

}

function validate() {

if(strlen($this->zipcode) != 6) {
$this->errors['zipcode'] = 'Nalezy podac poprawny kod pocztowy';
}

if(!$this->address1) {
$this->errors['address1'] = 'Adres to pole wymagane';
}

if(!isset($this->city)) {
$this->errors['city'] = 'Miasto to pole wymagane';
}

if(sizeof($this->errors)) {
return true;
} else {
return false;
}
}

function __toString() {
return " $this->address1
$this->address2
$this->city
$this->zipcode";
}

}

?>


zalaczony plik class.PropertyObject.php tylko obsuguje funkcje magiczne get() i set().

Nastepnie rdzen przykladu znajduje sie tutaj:

CODE

<?php

require_once('class.PropertyObject.php');
require_once('class.PhoneNumber.php');
require_once('class.Address.php');
require_once('class.Email.php');

abstract class Entity extends PropertyObject {
private $_emails;
private $_addresses;
private $_phonenumbers;

public function __construct($entityID) {
$arData = DataManager::getEntityData($entityID);

parent::__construct($arData);

$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['id'] = 'jednostka_id';
$this->propertyTable['name1'] = 'snazwa1';
$this->propertyTable['name2'] = 'snazwa2';
$this->propertyTable['type'] = 'ctyp';

$this->_emails = DataManager::getEmailObjectsForEntity($entityID);
$this->_addresses = DataManager::getAddressObjectsForEntity($entityID);
$this->_phonenumbers = DataManager::getPhoneNumberObjectsForEntity($entityID);

}

function setID($val) {
throw new Exception('Wartosc pola ID nie moze byc zmieniana');
}

function setEntityID($val) {
$this->setID($val);
}

function phonenumbers($index) {
if(!isset($this->_phonenumbers[$index])) {
throw new Exception('Bledny numer telefonu');
} else {
return $this->_phonenumbers[$index];
}
}

function getNumberOfPhoneNumbers() {
return sizeof($this->_phonenumbers);
}

function addPhoneNumber(PhoneNumber $phone) {
$this->_phonenumbers[] = $phone;
}


function addresses($index) {
if(!isset($this->_addresses[$index])) {
throw new Exception('Bledny adres pocztowy');
} else {
return $this->_addresses[$index];
}
}

function getNumberOfAddresses() {
return sizeof($this->_addresses);
}

function addAddress(Address $address) {
$this->_address[] = $address;
}


function emails($index) {
if(!isset($this->_emails[$index])) {
throw new Exception('Bledny adres email');
} else {
return $this->_emails[$index];
}
}

function getNumberOfEmails() {
return sizeof($this->_emails);
}

function addEmail(Email $email) {
$this->_emails[] = $email;
}

public function validate() {
//tutaj kod odpowiedzialny za walidacje
}
}



?>


i metodzie validate() jest informacja ze musze napisac kod odpowiedzialny za walidacje danych.... to moje pytanie brzmi to po co w pliku class.Address.php zostala przedstawiona funkcja do walidacji danych (chociaz i ona rowniez nie dziala jak np nie ma w bazie danych podanego adresu to nie wyswietla bledu...)

tutaj mam plik ostateczny ktory wyswietla dane:

CODE

<?php

require_once('class.DataManager.php');

function printIn($data) {
print $data . "<br/>\n";
}

$arContacts = DataManager::getAllEntitiesAsObjects();
foreach($arContacts as $objEntity) {
if(get_class($objEntity) == 'Individual') {
print "<h1> Osoba - {$objEntity->__toString()}</h1>";
} else{
print "<h1>Organizacja {$objEntity->__toString()} </h1>";
}

if($objEntity->getNumberOfEmails()) {
print "<h2>Adresy email </h2>";

for($x=0; $x < $objEntity->getNumberOfEmails(); $x++) {
printIn($objEntity->emails($x)->__toString());
}

}

if($objEntity->getNumberOfAddresses()) {
print "<h2>Adresy pocztowe </h2>";

for($x=0; $x < $objEntity->getNumberOfAddresses(); $x++) {
printIn($objEntity->addresses($x)->__toString());
}

}

if($objEntity->getNumberOfPhoneNumbers()) {
print "<h2>Numery telefonu </h2>";

for($x=0; $x < $objEntity->getNumberOfPhoneNumbers(); $x++) {
printIn($objEntity->phonenumbers($x)->__toString());
}

}

print "<hr>\n";
}

foreach($arContacts as $objEntity) {
if(get_class($objEntity) == 'Individual') {
print "<h1> Pracodawca - {$objEntity->__toString()}</h1>";
} else{
print "<h1> Pracownik {$objEntity->__toString()} </h1>";
}
}




?>


Czy moze mi ktos wytlumaczyc jak ta walidacja powinna dzialac... co musze zrobic zeby mi to zadzialalo prawidlowo.
Dzieki wielkie za podpowiedzi.
Go to the top of the page
+Quote Post
em1X
post
Post #2





Grupa: Zarejestrowani
Postów: 984
Pomógł: 41
Dołączył: 16.03.2002
Skąd: Płock

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


1) Zamień ten cały "code" na kod php
2) Wklej kod z poprawnym formatowaniem (wcięcia itd.)
Go to the top of the page
+Quote Post
lukasz_jaw
post
Post #3





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 9.04.2012

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


Witam - OKI PROSZE BARDZO I PROSZE O PODPOWIEDZI (IMG:style_emoticons/default/smile.gif)
Mam pewny przyklad z ksiazki 'PHP5 Zaawansowane programowanie' ktory nie moge rozwiazac problemu odnosnie sprawdzenia walidacji zmiennych.
plik class.Address.php

Kod
<?php
require_once('class.PropertyObject.php');

class Address extends PropertyObject {
                    function __construct($addressid) {
                    $arData = DataManager::getAddressData($addressid);
                          parent::__construct($arData);

$this->propertyTable['addressid'] = 'adres_id';
$this->propertyTable['id'] = 'adres_id';
$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['address1'] = 'sadres1';
$this->propertyTable['address2'] = 'sadres2';
$this->propertyTable['city'] = 'smiasto';
$this->propertyTable['zipcode'] = 'skod';
$this->propertyTable['type'] = 'styp';

}

function validate() {

if(strlen($this->zipcode) != 6) {
                     $this->errors['zipcode'] = 'Nalezy podac poprawny kod pocztowy';
}

if(!$this->address1) {
                     $this->errors['address1'] = 'Adres to pole wymagane';
}

if(!isset($this->city)) {
                     $this->errors['city'] = 'Miasto to pole wymagane';
}

if(sizeof($this->errors)) {
             return true;
} else {
           return false;
       }
}

function __toString() {
           return "$this->address1
                      $this->address2
                      $this->city
                      $this->zipcode";
                              }

}

?>


zalaczony plik class.PropertyObject.php tylko obsuguje funkcje magiczne get() i set().

Nastepnie rdzen przykladu znajduje sie tutaj:

Kod
<?php

require_once('class.PropertyObject.php');
require_once('class.PhoneNumber.php');
require_once('class.Address.php');
require_once('class.Email.php');

abstract class Entity extends PropertyObject {
          private $_emails;
          private $_addresses;
          private $_phonenumbers;

                  public function __construct($entityID) {
                        $arData = DataManager::getEntityData($entityID);
                        parent::__construct($arData);

$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['id'] = 'jednostka_id';
$this->propertyTable['name1'] = 'snazwa1';
$this->propertyTable['name2'] = 'snazwa2';
$this->propertyTable['type'] = 'ctyp';

$this->_emails = DataManager::getEmailObjectsForEntity($entityID);
$this->_addresses = DataManager::getAddressObjectsForEntity($entityID);
$this->_phonenumbers = DataManager::getPhoneNumberObjectsForEntity($entityID);

                                }

function setID($val) {
                  throw new Exception('Wartosc pola ID nie moze byc zmieniana');
}

function setEntityID($val) {
                 $this->setID($val);
}

function phonenumbers($index) {
                  if(!isset($this->_phonenumbers[$index])) {
                            throw new Exception('Bledny numer telefonu');
} else {
                  return $this->_phonenumbers[$index];
          }
}

function getNumberOfPhoneNumbers() {
               return sizeof($this->_phonenumbers);
}

function addPhoneNumber(PhoneNumber $phone) {
             $this->_phonenumbers[] = $phone;
}


function addresses($index) {
                  if(!isset($this->_addresses[$index])) {
                             throw new Exception('Bledny adres pocztowy');
} else {
              return $this->_addresses[$index];
          }
}

function getNumberOfAddresses() {
               return sizeof($this->_addresses);
}

function addAddress(Address $address) {
               $this->_address[] = $address;
}


function emails($index) {
              if(!isset($this->_emails[$index])) {
                         throw new Exception('Bledny adres email');
} else {
            return $this->_emails[$index];
          }
}

function getNumberOfEmails() {
            return sizeof($this->_emails);
           }

function addEmail(Email $email) {
             $this->_emails[] = $email;
}

public function validate() {
            //tutaj kod odpowiedzialny za walidacje
          }
}

?>


i metodzie validate() jest informacja ze musze napisac kod odpowiedzialny za walidacje danych.... to moje pytanie brzmi to po co w pliku class.Address.php zostala przedstawiona funkcja do walidacji danych (chociaz i ona rowniez nie dziala jak np nie ma w bazie danych podanego adresu to nie wyswietla bledu...)

tutaj mam plik ostateczny ktory wyswietla dane:

Kod
<?php

               require_once('class.DataManager.php');

              function printIn($data) {
                          print $data . "<br/>\n";
                        }

               $arContacts = DataManager::getAllEntitiesAsObjects();
                         foreach($arContacts as $objEntity) {
                                 if(get_class($objEntity) == 'Individual') {
                                         print "<h1> Osoba - {$objEntity->__toString()}</h1>";
                                 } else{
                                           print "<h1>Organizacja {$objEntity->__toString()} </h1>";
                                         }

                       if($objEntity->getNumberOfEmails()) {
                                print "<h2>Adresy email </h2>";

                                    for($x=0; $x < $objEntity->getNumberOfEmails(); $x++) {
                                     printIn($objEntity->emails($x)->__toString());
                                     }
                            }

                        if($objEntity->getNumberOfAddresses()) {
                               print "<h2>Adresy pocztowe </h2>";

                                       for($x=0; $x < $objEntity->getNumberOfAddresses(); $x++) {
                                           printIn($objEntity->addresses($x)->__toString());
                                            }
                                }

                          if($objEntity->getNumberOfPhoneNumbers()) {
                                       print "<h2>Numery telefonu </h2>";

                                             for($x=0; $x < $objEntity->getNumberOfPhoneNumbers(); $x++) {
                                                   printIn($objEntity->phonenumbers($x)->__toString());
                                                }
                                 }
             print "<hr>\n";
}

                 foreach($arContacts as $objEntity) {
                            if(get_class($objEntity) == 'Individual') {
                                      print "<h1> Pracodawca - {$objEntity->__toString()}</h1>";
                                   } else{
                                      print "<h1> Pracownik {$objEntity->__toString()} </h1>";
                                    }
                        }

?>


Czy moze mi ktos wytlumaczyc jak ta walidacja powinna dzialac... co musze zrobic zeby mi to zadzialalo prawidlowo.
Dzieki wielkie za podpowiedzi.
Go to the top of the page
+Quote Post

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 - 05:41