Skip to content

Instantly share code, notes, and snippets.

@dbowling
Created December 14, 2011 21:47
Show Gist options
  • Save dbowling/1478701 to your computer and use it in GitHub Desktop.
Save dbowling/1478701 to your computer and use it in GitHub Desktop.
Custom form validation subclass
<?php
class MY_Form_Validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
/**
* Error Array
*
* This function serves as a getter for the
* protected _error_array variable
*
* @access public
* @return array
*/
public function error_array(){
return $this->error_array();
}
/**
* Add Custom Validation Callback
*
* This method allows a user to set a method or function to be called when a form needs to be validated.
*
* Results from the callback are ignored if they are not an array.
*
* @param str $callback_function the function/method to call (must be public if not a rule in this class)
* @param str $label the label to be passed (allows better error messages), defaults to an empty string
* @param mixed $rules the rules to be passed to the function/method, defaults to an empty string
* @param str $helper the name of the helper to call, if the function is not available in $this context. The helper will be loaded automatically
* @return bool TRUE if there was post data and the function/method runs, FALSE if there is no POST data
* @access public
*/
public function add_custom_validation_callback($callback_function, $label = '', $rules = '', $helper = ''){
if(!isset($_POST) || empty($_POST) ){
return FALSE;
}
if($helper != ''){
$callback_result = $this->_run_callback_from_helper($callback_function, $label, $rules, $helper);
}else{
$callback_result = $this->_run_callback_from_this($callback_function, $label, $rules);
}
if(is_array($callback_result)){
foreach($callback_result as $error_name=>$error_message){
$this->_error_array[$error_name] = $error_message;
}
}
return TRUE;
}
protected function _run_callback_from_helper($callback_function, $label, $rules, $helper){
try{
$this->CI->load->helper($helper);
if( !function_exists($callback_function) ){
throw new Exception("The {$callback_function} callback does not exist in the {$helper} helper, or the {$helper} helper could not be loaded.");
}else{
return $callback_function($label, $rules);
}
}catch (Exception $e){
throw new Exception("The {$callback_function} helper callback threw an error: {$e}");
}
}
protected function _run_callback_from_this($callback_function, $label, $rules){
try{
if( !method_exists($this->CI, $callback_function) ){
throw new Exception("The {$callback_function} callback does not exist in the current context. Did you mean to load it from a helper?");
}else{
return $this->CI->$callback_function($label, $rules);
}
}catch (Exception $e){
throw new Exception("The {$callback_function} callback threw an error: {$e}");
}
}
// --------------------------------------------------------------------
// More custom validation rules (these rules are for the CodeIgniter
// set_rules() method, and cannot be called directly for the
// extended add_custom_validation_callback() method because they return
// a bool, instead of an error array
// --------------------------------------------------------------------
/**
* Valid Student ID (790 or equivelent)
*
* @access public
* @param string
* @return bool
*/
public function valid_net_id($str)
{
/*
Explaining the Regex pattern...
^[a-z]{1,2} this matches one or two letters at the start (first and last name, or just a last name)
[0-9]{6} this requires 6 digits following the initials
[ega]? matches the suffix of the netid (currently acceptible are nothing, e, a and g)
*/
if( !preg_match( '/^[a-z]{1,2}[0-9]{6}[ega]?$/' , strtolower($str)) ){
$this->set_message(__FUNCTION__, 'The %s field must contain a valid NetID');
return FALSE;
}
return TRUE;
}
// --------------------------------------------------------------------
/**
* Valid Student ID (790 or equivelent)
*
* @access public
* @param string
* @return bool
*/
public function valid_student_id($str)
{
if ( !is_numeric($str) || ($str < 100000000) || ($str > 999999999) )
{
$this->set_message(__FUNCTION__, 'The %s field must contain a valid Student ID number');
return FALSE;
}else{
return TRUE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment