致命错误:调用成员函数set_rules()

时间:2012-02-11 21:52:22

标签: php codeigniter

我在第17行Fatal error: Call to a member function set_rules() on a non-object

上收到以下错误

我在自动加载上有form_validation库。

    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 */

7 个答案:

答案 0 :(得分:11)

你忘了写构造函数吗?

class Login extends CI_Controller {

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

    ...

答案 1 :(得分:3)

尝试检查您的模型文件。

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

Class Mymodel extends CI_Controller

当我换上

Class Mymodel extends CI_Model

问题解决了

答案 2 :(得分:1)

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

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

在您的类的方法或构造函数中。或者,您可以通过编辑 application / config / autoload.php 来自动加载。

更改

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

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

答案 3 :(得分:0)

您所在的班级中不存在

$this->form_validation->set_rules。这是您自己上课的吗?它是否延伸CI_Controller

我对CI没有很多经验,但如上所述,“它试图告诉你$ this-&gt; form_validation不是当前类中的对象”。

<强>更新

您需要加载lib:

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

http://codeigniter.com/user_guide/libraries/form_validation.html#thecontroller

答案 4 :(得分:0)

来自CodeIgniter Link的教程:您必须添加

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

加载库。

答案 5 :(得分:0)

在application / config / autoload.php文件中加载form_validation库

答案 6 :(得分:0)

啊!我遇到了同样的问题,并尝试了上述解决方案,但我遇到的都不是,而是我必须在callback_function的第一行中添加“ return false”。我建议使用这样的格式并在此之后进行测试运行,在其中实现代码,以确保set_rule正常运行:

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