如果加载两个模型,则表单验证将不起作用 - CodeIgniter

时间:2015-07-05 18:40:15

标签: php codeigniter

我有一个控制器并尝试加载两个模型( Usermodel Contentmodel ),我还需要加载表单验证库。我使用 Usermodel 为用户执行所有操作,例如登录和注册,我需要 Contentmodel 来处理我的网络内容。起初我能够登录和注册,我对表单验证库没有任何问题,但是当我添加一行$this->load->model('contentmodel');来加载 Contentmodel 时,我突然收到此错误:

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

如果我删除行$this->load->model('contentmodel');,一切都会恢复正常。

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');
        }
    }
}

而且,如果我删除/评论这些行:

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

一切也恢复正常。

请帮帮我。提前谢谢。

3 个答案:

答案 0 :(得分:1)

问题在于您的<input type="text" name="name" value="{{post.PostId}}" /> <button ng-click="getById(post.PostId)"></button> <h1>{{post.Title}}</h1> $scope.getById = function (id) { console.log(id); return $http.get('/api/Post/' + id); } 变量。如您所见,Form_validation库也在使用它($lang)。将其更改为其他内容,并将其设置为私有。通常,控制器内的任何变量都应设置为私有,否则您将遇到此类问题。

答案 1 :(得分:0)

您产生了一个奇怪的问题。如果加载两个模型 - CodeIgniter 错误,您的问题标题表单验证将无法工作。

表单验证库不会停止处理您加载的模型数量。你需要找到你做的错误。

您的错误

@CodeGodie已经提到过你为什么会遇到这个错误。还有更多的补充

如果从控制器构造函数中删除此代码$this->lang = $config['lang'];,它将起作用

<强>为什么吗

Codeigniter的控制器使用$lang作为CI_Lang类的对象.Form验证类使用(查看文件内部和错误消息给出的行号)该变量,它应该成为CI_Lang的对象。但是你在控制器构造函数中将它替换为字符串,这就是你得到错误的原因。

答案 2 :(得分:0)

在表单验证运行部分中,您使用===只能使用==

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

替换为。

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

此外,您在构造区域中有相当多的内容。

  • 自动加载网址和表单助手
  • 不要在__construct区域中加载视图我认为不好的做法

带回叫的控制器

    <?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;
        }

    }
}

在您的视图上,然后回显验证消息

<?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