Cian Woods Cian Woods - 1 month ago 12
PHP Question

CodeIgniter Auto Loading form_validation causes Error with Model files

I am using CodeIgniters form_validation library to validate a form in my app. Library loaded here:

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


Ever since including the library I cannot load any models in the app. I get the following error in my browser. The form is a registration form for users to sign up to the site. The form gets validated perfectly.

A PHP Error was encountered

Severity: Notice

Message: Undefined property: register_model::$load

Filename: libraries/Form_validation.php

Line Number: 147


Here is my function that tries to load the model:

public function validateRegForm(){
$this->load->model('register_model');
$this->load->helper('url');
$this->load->helper('form');

$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('phone', 'Phone', 'required');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm-password', 'Confirm Password', 'required|matches[password]');

if ($this->form_validation->run() == FALSE){
$data['title'] = 'Sign Up';

$this->load->view('template/header', $data);
$this->load->view('template/navigation');
$this->load->view('register_view');
$this->load->view('template/footer');
} else {

$data = array(
'firstName' => $this->input->post('firstname'),
'lastName' => $this->input->post('lastname'),
'address' => $this->input->post('address'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'username' => $this->input->post('username'),
'password'=> $this->input->post('password')
);

$this->register_model->insertUsers($data);
}
}


Register model file:

<?php
class register_model extends CI_Controller {

//Insert a new user to users table when registration form submits
function insertUser(){

$query = $this->db->query();

}

}

?>


Any help is appreciated :)

Answer

Remove form validation library from autoload, and load it inside of controller.

  public function __CONSTRUCT(){
    parent::_CONSTRUCT();
    $this->load->model('register_model');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation'); // load it from controller like this 
  }


    $this->form_validation->set_rules('firstname', 'First Name', 'required');
    $this->form_validation->set_rules('lastname', 'Last Name', 'required');
    $this->form_validation->set_rules('address', 'Address', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('phone', 'Phone', 'required');
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    $this->form_validation->set_rules('confirm-password', 'Confirm Password', 'required|matches[password]');

    if ($this->form_validation->run() == FALSE){
     $data['title'] = 'Sign Up';

     $this->load->view('template/header', $data);
     $this->load->view('template/navigation');
     $this->load->view('register_view');
     $this->load->view('template/footer');
    } else {

      $data = array(
        'firstName' => $this->input->post('firstname'),
        'lastName' => $this->input->post('lastname'),
        'address' => $this->input->post('address'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
        'username' => $this->input->post('username'),
        'password'=> $this->input->post('password')
      );

      $this->register_model->insertUsers($data);
    }
}