Dashboard > Maintain > Attic > db_dataobject Validating
Maintain Log In   View a printable version of the current page.
db_dataobject Validating
Added by Danny Robert, last edited by Frederic Wenzel on Sep 05, 2006  (view change)
Labels: 
(None)

Validation:
to validate a particular row, create a "validateROWNAME()" function in your db_dataobject SUBCLASS. When the "validate()" method is called, this will be accessed. The results of all row validations are stored in an associative array.
I created a simple database Table containing an Index, MAC, and IP. Here's my code that verified a correct MAC address:

//filename: index.php

require("DB/DataObject.php");
require("DB/DataObject/FormBuilder.php");
require("include.php");

$host = DB_DataObject::factory('Hosts');

$host->MAC = "00:0C:F1:9B:30:EG";
$host->IP = "128.193.0.135";

$values = $host->validate();
$mac_valid = $values['MAC'];

if($mac_valid) {
    $host->insert();
    echo "Host <b>$host->MAC</b> successfully inserted.";
} else {
    echo "Sorry, the MAC address you entered is invalid. Host not added to table.<br>\n";
}

here's the corresponding validate code in my subclass:

//filename: DataObjects/Hosts.php

/* Automagically created code section not shown*/

    function validateMAC()
    {
        $reg = "/^[0-9A-F][0-9A-F][:]{0,1}[0-9A-F][0-9A-F][:]{0,1}[0-9A-F][0-9A-F][:]{0,1}[0-9A-F][0-9A-F][:]{0,1}[0-9A-F][0-9A-F][:]{0,1}[0-9A-F][0-9A-F]/i";
        $result = preg_match($reg,$this->MAC);

        echo "MAC address is ";

        if($result) {
            echo "VALID\n";
            return true;
        } else  {
            echo "invalid<br />\n";
            return false;
        }

        return false;
    
    }//ValidateMAC

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.7 Build:#524 Jul 28, 2006) - Bug/feature request - Contact Administrators