add_field('uname', 'Username'); $User->add_rule('Required'); $User->add_rule('MinLength', 4); $User->add_rule('MaxLength', 20); $e =& $Registration->validate(); /* just shows the errors */ if(count($e)) { echo ''; } /* classes */ class ValidationMaster { var $fields; function ValidationMaster() {} function &add_field($form_name, $label) { $Field =& new Field($label, $_POST[$form_name]); $this->fields[] =& $Field; return $Field; } function validate() { $errors = array(); foreach($this->fields as $Field) { $errors = array_merge($errors, $Field->validate()); } } } class Field { var $label; var $data; var $rules; function Field($label, $data) { $this->label = $label; $this->data = $data; } function add_rule($name, $param = 0) { $name = $name . 'Rule'; $this->rules[] =new $name($param, $this->data, $this->label); } function &validate() { echo count($this->rules); $errors = array(); foreach($this->rules as $Rule) { $errors[] = $Rule->execute(); } return $errors; } } class Rule { var $param; var $data; var $label; function Rule($param, $data, $label) { $this->param = $param; $this->data = $data; $this->label = $label; } function execute() { die('Abstract function must be implemented'); } } class RequiredRule extends Rule { function RequiredRule($param, $data, $label) { parent::Rule($param, $data, $label); } function execute() { if(empty($this->data)) { return $this->label . ' is required & must have a value'; } } } class MinLengthRule extends Rule { function RequiredRule($param, $data, $label) { parent::Rule($param, $data, $label); } function execute() { if(strlen($this->data) < $this->param) { return $this->label . ' must be longer than' . $this->param; } } } class MaxLengthRule extends Rule { function RequiredRule($param, $data, $label) { parent::Rule($param, $data, $label); } function execute() { if(strlen($this->data) > $this->param) { return $this->label . ' must be shorter than' . $this->param; } } } ?>