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

I have a small fix to make validation class more flexible.

[code] ... <form> <input type="text" name="search[query]"> </form> ... [/code]

In this case validation class is not really helpful as we can declare as a rule just field in the first level array. What if you want to have n level array ?

[b]TO DO:[/b]

[b]1.[/b] Add just after :

[code] class CI_Validation { ... [/code]

new var

[code] var $_inputdata = array(); [/code]

[b]2.[/b] Replace all $_POST vars to : (ctrl+h) [code]$this->_inputdata[/code]

[b]3.[/b] You need to add this function into Validation library : (system/libraries/Validation.php)

[code] function parse_rules($data) { if( ! is_array($data)) { return false; } else { foreach($data as $eval_array => $rules) { $to_eval = explode(",", $eval_array); $this->_inputdata[$eval_array] = eval('return $_POST[''.implode("']['", $to_eval).''];'); } } } [/code]

[b]4.[/b] in [i]set_rules()[/i] function add one line just before foreach :

[code] function set_rules($data, $rules = '') { if ( ! is_array($data)) { if ($rules == '') return;

        $data[$data] = $rules;
    }
    
    $this->parse_rules($data); // Add this line

    foreach ($data as $key => $val)
    {
        $this->_rules[$key] = $val;
    }
}

[/code]

[b]Sample : [/b]

[code] ... <form> <input type="text" name="search[query]"> </form> ... [/code]

[code] $rules['search,query'] = "required"; // You can add n level array i.e. $rules['level1,level2,level3,level4,...'] // what will be in fact $_POST['level1']['level2']['level3']['level4'][...]

$this->CI->validation->set_rules($rules);
    
if ($this->CI->validation->run() !== FALSE) {
     echo 'OK';
}

[/code]

Clone this wiki locally