Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message: Undefined property: Users::$form_validation

im currently trying to change my application from MySQL database to MongoDB database. My project requires to compare two of these, and my MySQL code was working and now it gives me this error.

Stack Trace:

A PHP Error was encountered Severity: Notice Message: Undefined property: Users::$form_validation Filename: controllers/users.php Line Number: 26

Error line:

$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');   

Code:

<?php
class Users extends CI_Controller{
    public function register(){ 
        $this->form_validation->set_rules('first_name','First Name','trim|required|max_length[50]|min_length[2]|xss_clean');
        $this->form_validation->set_rules('last_name','Last Name','trim|required|max_length[50]|min_length[2]|xss_clean');
        $this->form_validation->set_rules('email','Email','trim|required|max_length[100]|min_length[5]|xss_clean|valid_email');
        $this->form_validation->set_rules('username','Username','trim|required|max_length20]|min_length[6]|xss_clean');
        $this->form_validation->set_rules('password','Password','trim|required|max_length[20]|min_length[6]|xss_clean');
        $this->form_validation->set_rules('password2','Confirm Password','trim|required|max_length[50]|min_length[2]|xss_clean|matches[password]');
        if($this->form_validation->run() == FALSE){
            $data['main_content']='users/register';
            $this->load->view('layouts/main',$data);    
        }else{
            if($this->User_model->create_member()){ 
                $this->session->set_flashdata('registered','Registered successfully');
                redirect('home/index'); 
            }
        }
    }

    public function login(){
        //$this->load->model('user_model');
        $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');      
        $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean');        

        $collection = $this->database->users;

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

            $this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
            redirect('home/index');
        } else {

        $username=$_POST['login'];
        $password=$_POST['password'];

        $user_id = $collection->findOne(array('username' => $username, 'password' => md5($password)));
          // $username = $this->input->post('username');
          // $password = $this->input->post('password');


           $user_id = $this->User_model->login_user($username,$password);


           if($user_id){

               $user_data = array(
                       'user_id'   => $user_id,
                       'username'  => $username,
                       'logged_in' => true
                );

               $this->session->set_userdata($user_data);

               redirect('home/index');
            } else {

                $this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
                redirect('home/index');
            }
        }
    }

    public function logout(){

        $this->session->unset_userdata('logged_in');
        $this->session->unset_userdata('user_id');
        $this->session->unset_userdata('username');
        $this->session->sess_destroy();

        redirect('home/index');
    }
}
like image 706
Vinci Avatar asked Mar 17 '26 02:03

Vinci


1 Answers

Changing config/autoload.php worked for me, just add form_validation in autoload :

$autoload['libraries'] = array('form_validation');
like image 71
Vinci Avatar answered Mar 18 '26 16:03

Vinci