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:
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:
/* 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;
}