2

I have one controller and try to load two models (Usermodel and Contentmodel) and also I need to load Form Validation Library. I use Usermodel to do everything with user such as login and register, and I need Contentmodel to do everything with my web content. At first I was able to login and register and I had no problem with Form Validation Library, but then when I add a line $this->load->model('contentmodel'); to load Contentmodel, I suddenly get this error:

Call to a member function load() on a non-object in /var/www/html/webappadmin/system/libraries/Form_validation.php on line 455

If I remove the line $this->load->model('contentmodel'); everything goes back to normal again.

Controller (Controll.php):

defined('BASEPATH') OR exit('No direct script access allowed');

class Controll extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */

public $lang;
public $logo;
public function __construct () {
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->model('contentmodel');
    $this->load->model('usermodel');
    $this->load->library('session');
    $this->load->library('form_validation');
    /*get all user sessions data*/
    $this->sesi = $this->session->all_userdata();
    $config = $this->contentmodel->load_config();
    $this->lang = $config['lang'];
    $this->logo = $config['image_logo_path'];
    $data['lang'] = $this->lang;
    $this->load->view('/header/header');
}

public function panel(){
    $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('cred', 'Password', 'required');

    if($this->form_validation->run() === false){
        echo '<center style="position: relative;z-index:10000;font-family: \'Roboto\', sans-serif;color:white;top: 62%;">'.validation_errors().'</center>';
        $this->load->view('login');
    }else{
        $user = $this->usermodel->login();
        if($user == 0){
            echo '<center class="logerror" style="position: relative;z-index:10000;font-family: \'Roboto\', sans-serif;color:white;top: 62%;">Username or Password incorect. Please try again</center>';
            $this->load->view('login');
        }else{
            $data['data'] = 2;
            $data['user'] = $user;

            $this->load->view('/header/navbar',$data);
            $this->load->view('panel');
            $this->load->view('/footer/footer');
        }
    }
}

And also, if I remove/comment these lines:

$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('cred', 'Password', 'required');
/* ... */
if($this->form_validation->run() === false){
/* ... */
}else{
/* ... */
}

Everything goes back to normal again as well.

Please help me. Thanks in advance.

Subkhan Sarif
  • 459
  • 9
  • 20
  • if you open `libraries/Form_validation.php` and go to line 455, what do you see? – CodeGodie Jul 05 '15 at 18:50
  • There are couple things that would help us help you. More information about that error you showed in a screenshot, such as what `load()` was called on, and what it's current value is (Is it undefined? If `$this` is not an object, then what is it?) Also, if you could pare this down to an [MCVE](http://stackoverflow.com/help/mcve), the problem might become obvious. It looks like `$this` has some sketchy behavior in PHP, check out [this question](http://stackoverflow.com/questions/5494436/php-this-variable) where someone got tripped up by `$this` automatically initializing members to `null`. – Dan Ross Jul 05 '15 at 18:53
  • @CodeGodie it's this line: `$this->CI->lang->load('form_validation');` – Subkhan Sarif Jul 05 '15 at 18:53
  • Okey so the problem is that I have declared public variable $lang in my controller and it seems CI also tries to access that $lang instead. If I change $lang to anything else it works just fine. – Subkhan Sarif Jul 05 '15 at 18:58
  • that's the answer I provided below. – CodeGodie Jul 05 '15 at 18:59

3 Answers3

1

The problem is your $lang variable. As you can see, the Form_validation Library is also using it ($this->CI->lang->load('form_validation');) . Change it to something else, and set it to private. As a rule, any variable inside your controller should be set to private, or else you will have such issues.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

You produced a strange problem.Your question title Form Validation Won't Work if Load Two Models - CodeIgniter is wrong.

Form validation library does not stop working how many model you load. You need to find what mistake you do.

Your mistake

@CodeGodie already mentioned why you got that error.Little more addition

If you remove this code $this->lang = $config['lang']; from your controller construct function it will work

why?

Codeigniter's controller uses $lang as an object of CI_Lang class.Form validation class uses(look inside the file and the line number that your error message gave) that variable and it should be object of CI_Lang. But you replacing it as string at your controller construct function that's why you got that error.

Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
0

On your form validation run part you use === try only with ==

`if($this->form_validation->run() === false){`

replace with.

`if($this->form_validation->run() == false){`

Also You have quite a bit in the construct area.

  • Auto load url and form helper
  • Don't load a view in __construct area bad practice I think

Controller With Callback

    <?php

    defined('BASEPATH') OR exit('No direct script access allowed');

    class Controll extends CI_Controller {

    public function __construct () {
        parent::__construct();

        $this->load->helper('url'); // Autoload it
        $this->load->helper('form'); // Autoload it
        $this->load->library('session'); // Autoload it

        $this->load->model('contentmodel');
        $this->load->model('usermodel');

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

        // Removed View From Construct not good idea to have in construct area.
    }

   // change panel to index.

    public function index() {

        $this->form_validation->set_rules('email', 'Email', 'required|callback_user_login');
        $this->form_validation->set_rules('cred', 'Password', 'required');

        if ($this->form_validation->run() == TRUE) {

            // You could redirect to another controller once login 
            redirect('success_page');

        }

        // http://www.codeigniter.com/userguide2/general/views.html

        // If not data pass through these views then you will need to use 
        // something like $this->load->view('header', null, true); 
        // or with data $this->load->view('header', $data, true);

        $this->load->view('header', null, true);

        //$this->load->view('header', $data, true);

        $this->load->view('login'); // if you need to pass data through to login page then  $this->load->view('login', $data);

        $this->load->view('footer', null, true);

        //$this->load->view('footer', $data, true);
    }


    public function user_login() {
         $user = $this->usermodel->login();

        if ($user == TRUE) { 
           return TRUE; 
        } else {
            $this->form_validation->run('user_login', 'Incorrect Username Or Password');
            return FALSE;
        }

    }
}

On your view then echo the validation messages

<?php echo validation_errors(); ?>

CI2 http://www.codeigniter.com/userguide2/libraries/form_validation.html

CI3 http://www.codeigniter.com/user_guide/libraries/form_validation.html