* @version 1 * @copyright djomla */ /** * rules * not_empty * is_numeric * is_email * is_checked */ class FormValidate { /** * Rules holder * * @var array */ private $_rules; /** * Return valid form * * @var boolean */ private $validate = true; /** * Holder of error msg's * * @var array */ private $error_msgs; public function setRules($rules) { $this->_rules = $rules; } /** * Main method to trigger validation * * @return boolean */ public function validate() { foreach ($_POST as $key => $val) { if ($skey = $this->hasRule($key)) { $rule = $this->_rules[$skey]; if (method_exists($this, $rule['rule'])) { if (!call_user_method($rule['rule'], $this, $val)) { $this->validate = false; $this->error_msgs[] = $rule['msg']; } } else { $this->error_msgs[] = "Unsuported metod {$rule['rule']}"; } } } return $this->validate; } /* * Method to check if rule exists * * @return boolean */ private function hasRule($key) { if (!is_array($this->_rules) OR count($this->_rules) < 1) { return false; } foreach ($this->_rules as $k => $rule) { if (eregi($k, $key)) { return $k; } } return false; } public function getErrorMsg() { return $this->error_msgs; } /** * Method to check if value is empty * * @return boolean */ private function not_empty($field = null) { return (!empty($field) OR !strlen(trim($field)) < 1); } /** * Method to check if value is numeric * * @return boolean */ private function is_numeric($field = null) { return (is_numeric($field)); } /** * Method to check if value is email * * @return boolean */ private function is_email($field) { return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $field); } /** * Method to check if value is checked * * @return boolean */ private function is_checked($field = null) { return (!is_null($field)); } } ?>