Wednesday, April 16, 2014

[Codenigter][Solved] $this- form_validation- run() not working , always return false

The idea to solve the problem is from stackoverflow. The link was posted at the end of this post but the solution of my case is not exactly same as the answer.

Firstly, let have an overviews of my case:
function _slider_validation($item_no) {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('slider_title', 'TITLE NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_url', 'URL NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_photo', 'PHOTO NOT OKAY', 'required');
        return $this->form_validation->run();
    }

And referenced to Stackoverflow, I found I missed loading form helper in my case:

function _slider_validation($item_no) {
        $this->load->helper('form'); //<-- I missed this line !
        $this->load->library('form_validation');
        $this->form_validation->set_rules('slider_title', 'TITLE NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_url', 'URL NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_photo', 'PHOTO NOT OKAY', 'required');

        return $this->form_validation->run();
    }

So If your $this- form_validation- run() not working , always return false, try checking these points:
1) Remember checking you have load the form helper
2) load form_validation library
3) and set validation rules

Official's reason about why form_validation return false:
Since you haven't told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() function only returns TRUE if it has successfully applied your rules without any of them failing.

Reference:
http://stackoverflow.com/questions/17000803/codeigniter-form-validation-run-always-returns-false

No comments :

Post a Comment