2

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 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

4 Comments

The No Type Define here field is required. The No any item selected here field is required The No user logged in field is required
if you submit the form with all fields empty then it will show validation error
at this time i have no any form i just have an array and i want to check this array is validate or not.
form validation will works only form submit....you wrote the rules for the array which will get after the form submit
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.