Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 18 revisions

Category:Libraries::Extended

Instead of using callbacks to add rules you can add rules by extending the validation library.

All additions are welcome

[code] <?php class MY_Validation extends CI_Validation {

var $_display_fieldname = array();

function MY_Validation()
{
    parent::CI_Validation();
}
/**
* numeric rules
*
*/
function greater_than($str,$min)
{
    if(!is_numeric($str)){ return false; }
    return $str > $min;
}

function less_than($str,$max)
{
    if(!is_numeric($str)){ return false; }
    return $str < $max;
}
/*
*  checkbox rules
*
* use with required rule
*/
function equal_to($str,$eq)
{
    if(!is_numeric($str)){ return false; }
    return $str == $eq; 
}

function max_count($str,$max)
{
    return (count($str) <= $max);
}

function min_count($str,$min)
{
    return count($str) >= $min;
}
/**
*  email rules
*
*/
function domain_email($str,$params)
{
    $parts = explode("@", $str);
    $domain = $parts[1];
    $allowed_array = explode(',',$params);
    return in_array($domain, $allowed_array);
}
/**
*   general rules
*
*/
function exact_count($str,$eq)
{
   if(is_string($str))
   { 
       return (strlen($str) == $eq)?true:false;
   }
   else
   {
      return count($str) == $eq;
   }
}

/*
*  set fields alternative
*
*/
function set_fields($data = '', $field = '', $separator = '_') {    
    if ($data == '') {
        if (count($this->_fields) == 0 && count($this->_rules) == 0) {
            return FALSE;
        }
    } else {
        if ( ! is_array($data)) {
            $data = array($data => $field);
        }
        
        if (count($data) > 0) {
            $this->_fields = $data;
        }
    }
    
    foreach($this->_rules as $key => $val) {                  
        $text = ucwords(str_replace($separator, ' ', $key));             
        $auto_fields[$key] = $text;     
    }
    
    $this->_fields = array_merge($auto_fields, $this->_fields);
    
    foreach($this->_fields as $key => $val) {        
        $this->$key = ( ! isset($_POST[$key]) OR is_array($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
        
        $error = $key.'_error';
        if ( ! isset($this->$error)) {
            $this->$error = '';
        }
    }        
}

} [/code]

Clone this wiki locally