codeigniter form validation on array not form submit

1.7k views Asked by At

i want validate my form input field or you want to say i have an array and i want to validate that array using codeigniter

Example : i have array like :

$array['obj_type']='sample';
$array['obj_id']='44';
$array['user_id']='34566';

and my form validation config as like :

'validatedata' =>  array(
      array(
            'field' => 'obj_type',
            'label' => 'No Type Define here',
            'rules' => 'required'
        ),  
        array(
            'field' => 'obj_id',
            'label' => 'No any item selected here',
            'rules' => 'required|is_natural_no_zero'
        ),
        array(
            'field' => 'user_id',
            'label' => 'No user logged in',
            'rules' => 'required|is_natural_no_zero'
        ),
    ),

and when i use form validate its not validate array

if ($this->form_validation->run('validatedata')) {
} else {
echo validation_errors();
}

its print all error which define on on validatedata config array;

3

There are 3 answers

0
Pankaj Yadav On BEST ANSWER

i just use

$this->form_validation->set_data($array);

then i validate form

if ($this->form_validation->run('validatedata')) {
echo "sucess";
} else {
echo validation_errors();
}

now its works fine and good.

4
Nithin Krishnan P On

You have to load form validation library in your controller..

$this->load->library(array('form_validation'));
0
mgrueter On

You have to provide the data to the form_validation library:

$this->form_validation->set_data($array);

and then you can use

$this->form_validation->run('validatedata')

as intended.

If you want to validate multiple arrays, you'll have to call reset_validation() after validating each array.

Check system/libraries/Form_validation.php (around line 255, depending on your version of CI) for more information.