Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 5 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();
}

function greater_than($str,$min)
{
    if(!is_numeric($min)){ return false; } // developer error
    if(!is_numeric($str)){ return false; }
    return $str > $min;
}

function less_than($str,$max)
{
    if(!is_numeric($max)){ return false; } // developer error
    if(!is_numeric($str)){ return false; }
    return $str < $max;
}

function equal_to($str,$eq)
{
    if(!is_numeric($eq)){ return false; } // developer error
    if(!is_numeric($str)){ return false; }
    return $str == $eq; 
}

function max_checked($str,$max)
{
    if(!is_numeric($max)){ return false; } // developer error
    return count($str) <= $max;
}

function min_checked($str,$min)
{
    if(!is_numeric($min)){ return false; } // developer error
    return count($str) >= $min;
}

function exact_checked($str,$eq)
{
    if(!is_numeric($eq)){ return false; } // developer error
    return count($str) == $eq;
}

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