Here is a zencart's class I have been using to validate user submitted data
PHP Code:
<?php
class _yvalidation{
// TODO: use 'private' when ZC moves to PHP5
var $default_messages = array( 'validation_is_not_empty' => 'is empty',
'validation_is_number' => 'is not a number',
'validation_is_positive' => 'is not a positive number',
'validation_is_email' => 'is not a valid email address',
'validation_is_date' => 'is not a valid date',
'validation_is_url' => 'is not a valid url');
var $rules;
var $params = array();
var $error_count = 0;
var $validationErrors = array();
var $class = 'validation';
// class is used to later display error message
// if ($messageStack->size(class) > 0) echo $messageStack->output(class);
function set_error_class($class){
$this->class = $class;
}
function set_rules($rules){
$this->rules = $rules;
}
function add_rules($rules){
foreach($rules as $key => $value){
if(array_key_exists($key, $this->rules))
$this->rules[$key][] = $value;
else
$this->rules[$key] = $value;
}
}
function set_messages($messages){
$this->default_messages = $messages;
}
function add_messages($messages){
$this->default_messages[] = $messages;
}
function set_params($params){
$this->params = $params;
}
function add_params($params){
$this->params[] = $params;
}
function validate_fields($params){
foreach($this->rules as $field => $rules){
if(isset($params[$field]) && is_array($params[$field])){
$this->validate_fields($params[$name]);
}
else{
foreach($rules as $method => $message){
if(method_exists($this, $method)){
if(!call_user_func_array(array($this, $method), $params[$field])){ // value of the field is passed as the rule's parameters
// sounds off the alarm
if(isset($message) && !empty($message))
$this->set_error($message, $this->class);
else
$this->set_error("$field ".$this->default_messages[$method], $this->class);
}
}
else{
$this->set_error(sprintf("Could not call function '%s' to validate field '%s' with the value '%s'",$method,$field,$params[$field]), $this->class);
// sounds off the alarm
}
}
}
}
}
function set_error($message, $class, $type='error'){
$this->validationErrors[] = array('message' => $message, 'class' => $class, 'type' => $type);
$this->error_count++;
}
function set_zen_error($is_admin_side){
global $messageStack;
foreach($this->validationErrors as $error)
if(!$is_admin_side) $messageStack->add($error['class'], $error['message'], $error['type']);
else $messageStack->add($error['message'], $error['type']);
}
function run($is_admin_side = false){
$this->validate_fields($this->params);
$this->set_zen_error($is_admin_side);
return $this->error_count;
}
// TODO: Let the user define new validation functions somewhere else.
// Edit/Add new validation functions here:
function validation_is_not_empty($param){
return !empty($param);
}
function validation_is_number($param){
return is_numeric($param);
}
function validation_is_positive($param){
if($this->validation_is_number($param))
return (int)$param > 0;
return false;
}
//YYYY-MM-DD
function validation_is_date($date){
if (ereg ("([0-9]{4})-([0-9]{2})-([0-9]{2})", $date, $regs))
if((int)$regs[3]<=31 && (int)$regs[3]>0) // check date
if((int)$regs[2]<=12 && (int)$regs[2]>0) // check month
if((int)$regs[1]>0) // check year
return true;
return false;
}
function validation_is_email($email){
return zen_validate_email($email);
}
function validation_is_url($url){
$urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
if (eregi($urlregex, $url))
return true;
else
return false;
}
// End of editable zone
}
Usage:
PHP Code:
$yvalidation->set_params($_POST);
$rules = array('customers_email_address' => array('validation_is_not_empty'=>'You have to fill in your email address',
'validation_is_email'=>'You email address is not valid'),
'customers_name' => array('validation_is_not_empty'=>'You have to fill in your contact name'),
'customers_needs' => array('validation_is_not_empty'=>'You have to fill in your inquiry message'),
'customers_inquiry_topic' =>array('validation_is_not_empty'=>''),
'customers_inquiry_message' =>array('validation_is_not_empty'=>'Please fill in your inquiry'),
);
$yvalidation->set_rules($rules);
$error_counter = $yvalidation->run();
if($error_counter == 0){
// do something
}
I have found it to be very useful for me, you can write another class to extend this class then put your own validation methods in.
Just simply set the fields you want to validate, the error message (or you can set default error message as well), and the class will do all the validation for you.
Hope this helps someone.