Php 表格验证获胜';如果加载两个型号,则无法工作-CodeIgniter

Php 表格验证获胜';如果加载两个型号,则无法工作-CodeIgniter,php,codeigniter,Php,Codeigniter,我有一个控制器,尝试加载两个模型(Usermodel和Contentmodel),还需要加载表单验证库。我使用Usermodel对用户进行所有操作,如登录和注册,我需要Contentmodel对我的web内容进行所有操作。起初,我可以登录和注册,表单验证库也没有问题,但当我添加一行$this->load->model('contentmodel')要加载Contentmodel,我突然出现以下错误: 如果我删除行$this->load->model('contentmodel')一切又恢复正常

我有一个控制器,尝试加载两个模型(UsermodelContentmodel),还需要加载表单验证库。我使用Usermodel对用户进行所有操作,如登录和注册,我需要Contentmodel对我的web内容进行所有操作。起初,我可以登录和注册,表单验证库也没有问题,但当我添加一行
$this->load->model('contentmodel')
要加载Contentmodel,我突然出现以下错误:

如果我删除行
$this->load->model('contentmodel')一切又恢复正常

控制器(Controll.php):

一切又恢复了正常


请帮帮我。提前感谢。

问题在于您的
$lang
变量。如您所见,表单验证库也在使用它(
$this->CI->lang->load('Form\u validation');
)。将其更改为其他内容,并将其设置为private。通常,控制器中的任何变量都应设置为私有,否则会出现此类问题。

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

表单验证库不会停止处理加载的模型数量。你需要找出你犯了什么错误

你的错误

@CodeGodie已经提到了你为什么会犯这样的错误,再补充一点

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

为什么?


Codeigniter的控制器使用
$lang
作为
CI_lang
类的对象。表单验证类使用(查看文件内部以及错误消息给出的行号)该变量应该是CI_Lang的对象。但您在控制器构造函数中将其替换为字符串,这就是为什么会出现该错误。

在表单验证运行部分,您使用
==
仅尝试使用
=

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

`if($this->form_validation->run() == false){`
此外,在构造方面,您也有相当多的经验

  • 自动加载url和表单帮助器
  • 不要在构建区域加载视图我认为这是一个糟糕的做法
带回调的控制器

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

    }
}

CI2


CI3

如果您打开
库/Form_validation.php
并转到第455行,您会看到什么?有几件事情可以帮助我们。有关您在屏幕截图中显示的错误的更多信息,例如调用了什么
load()
,它的当前值是什么(是否未定义?如果
$this
不是对象,那么它是什么?)此外,如果您可以将其缩减为一个,问题可能会变得很明显。看起来,
$this
在PHP中有一些粗略的行为,检查一下有人被
$this
自动将成员初始化为
null
@CodeGodie的地方:
$this->CI->lang->load('form_validation')好的,问题是我在控制器中声明了公共变量$lang,而CI似乎也尝试访问该$lang。如果我把$lang换成其他任何东西,它就可以正常工作。这就是我在下面提供的答案。
`if($this->form_validation->run() == false){`
    <?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(); ?>