Skip to content
Derek Jones edited this page Jul 5, 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

<?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($str)){ return false; }
        return $str > $min;
    }
    
    function less_than($str,$max)
    {
        if(!is_numeric($str)){ return false; }
        return $str < $max;
    }
    
    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;
    }
    
    function exact_count($str,$eq)
    {
        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 = '';
            }
        }        
    }
    
}
Clone this wiki locally