forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
Validation Class
Derek Jones edited this page Jul 5, 2012
·
7 revisions
I have a small fix to make validation class more flexible.
...
<form>
<input type="text" name="search[query]">
</form>
...
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 ?
TO DO:
1. Add just after :
class CI_Validation {
...
new var
var $_inputdata = array();
2. Replace all $_POST vars to : (ctrl+h)
$this->_inputdata
3. You need to add this function into Validation library : (system/libraries/Validation.php)
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).'\'];');
}
}
}
4. in set_rules() function add one line just before foreach :
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;
}
}
**Sample : **
...
<form>
<input type="text" name="search[query]">
</form>
...
$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';
}