Php 致命错误:调用成员函数集_rules()

Php 致命错误:调用成员函数集_rules(),php,codeigniter,Php,Codeigniter,第17行出现以下错误致命错误:对非对象调用成员函数set_rules() 我有自动加载的表单验证库 public function login() { $this->form_validation->set_rules('userName','userName', 'required|valid_email|trim|max_length[99]|xss_clean'); $this->form_validatio

第17行出现以下错误
致命错误:对非对象调用成员函数set_rules()

我有自动加载的
表单验证

    public function login()
    {
            $this->form_validation->set_rules('userName','userName', 'required|valid_email|trim|max_length[99]|xss_clean');
            $this->form_validation->set_rules('userPassword','userPassword', 'required|trim|max_length[200]|xss_clean|callback__checkUser');

            if($this->form_validation->run() === TRUE) {
            // set CLEAN data in the session.
                redirect('admin/dashboard');
            }else{

            $this->index();
        }
    }
控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {


    public function index()
    {
        $data['companyName'] = $this->core_model->companyName();
        $data['pageTitle'] = "Administration Login";
        $this->load->view('admin/assets/header', $data);
        $this->load->view('admin/login.php', $data);
        $this->load->view('admin/assets/footer');
    }

    public function login()
    {
            $this->form_validation->set_rules('userName','userName', 'required|valid_email|trim|max_length[99]|xss_clean');
            $this->form_validation->set_rules('userPassword','userPassword', 'required|trim|max_length[200]|xss_clean|callback__checkUser');


            if($this->form_validation->run() === TRUE) {
            // set CLEAN data in the session.
                redirect('admin/dashboard');
            }else{

            $this->index();
        }
    }

    function _checkUser(){ // Private function 
        extract($_POST);

        $login = $this->admin_model->check_login($userName,$userPassword,$userGroup,$userEmail,$userActive);

        if(! $login){
            $this->session->set_flashdata('login_error', TRUE); 
            $this->form_validation->set_message('_checkUser','Sorry your %s is not correct');

            return FALSE;

    }else{
        if($user->userGroup == "admin" && $user->userActive == "yes")
        {
        $this->session->set_userdata('logged_in', TRUE);
        $this->session->set_userdata('userID', $user->id);
        $this->session->set_userdata('userName',$user->userName);
        $this->session->set_userdata('firstName',$user->userFirstName);
        $this->session->set_userdata('lastName',$user->userLastName);
        $this->session->set_userdata('userEmail',$user->userEmail);
        $this->session->set_userdata('userGroup',$user->userGroup);
        $this->session->set_userdata('userActive',$user->userActive);
        }else{
        $this->session->set_flashdata('login_error', TRUE); 
        $this->form_validation->set_message('_checkUser','Sorry your %s is not correct');

        return FALSE;
    }

    }

    }
}

/* End of file login.php */
/* Location: ./application/controllers/admin/login.php */

$this->form\u validation->set\u rules
在您所在的类中不存在。那是你自己上的课吗?它是否扩展了CI_控制器

我对CI没有太多经验,但如上所述,“它试图告诉您,$this->form_validation不是当前类中的对象”

更新:

您需要加载库:

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

您没有加载表单验证库。包括

$this->load->library('form_validation');
在类的方法或构造函数中。或者,您可以通过编辑application/config/autoload.php自动加载它

改变

$autoload['libraries'] = array();


来自CodeIgniter教程:您必须添加

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

加载库。

是否忘记编写构造函数

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }    

    ...

尝试检查您的模型文件

我和你有同样的错误,当我检查它时,我在我的模型类中写

Class Mymodel extends CI_Controller
当我变成

Class Mymodel extends CI_Model

问题已解决

在application/config/autoload.php文件中加载表单验证库

啊!!我遇到了同样的问题,并尝试了上述解决方案,但没有一个是我的情况,相反,我必须在回调函数的第一行中输入“return false”。我建议使用这样的格式,然后进行测试运行,在其中实现您的代码,以确保set_规则在运行:

    public function checkField($str)
    {
            if ($str == 'test')
            {
                    return FALSE;
            }
            else
            {
                    return TRUE;
            }
    }

好的,但是你会在任何地方设置$this->form\u验证吗?它试图告诉你$this->form\u validation不是一个对象。+1@Dan的评论,如果你
var\u dump($this->form\u validation)
它很可能会说
NULL
FALSE
@Dan你能给我一个设置示例吗。
$this->form\u validation=new form\u validation()最好在构造函数中完成,因为您的类似乎依赖于它。您需要添加以下内容:
$this->load->library('form_validation')$this->load->helper(数组('form','url')。你看过教程了吗?没有必要重写。如果你不定义一个构造函数并扩展一个类,它会调用父类的构造函数作为默认值issue@JessMcKenzie这不可能是问题所在,除非您以前在不调用父构造函数的情况下重写了构造函数。如果在使用多态性时不重写构造函数,则会自动调用父构造函数。如果需要手动访问form_验证,是否可以访问它?您是否尝试过创建一个空的controller/application/controller/test.php并检查$this->form_验证是否存在?您可以使用自动加载的其他库吗?
    public function checkField($str)
    {
            if ($str == 'test')
            {
                    return FALSE;
            }
            else
            {
                    return TRUE;
            }
    }