我使用codeigniter的form_validation库来验证我的应用程序中的表单。库加载在这里:
$autoload['libraries'] = array('database','form_validation');
自从包含库以来,我无法在应用程序中加载任何模型。我在浏览器中得到以下错误。该表单是用户注册到站点的注册表单。表单被完美地验证了。
A PHP Error was encountered
Severity: Notice
Message: Undefined property: register_model::$load
Filename: libraries/Form_validation.php
Line Number: 147
下面是我尝试加载模型的函数:
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);
}
}
注册模型文件:
<?php
class register_model extends CI_Controller {
//Insert a new user to users table when registration form submits
function insertUser(){
$query = $this->db->query();
}
}
?>
任何帮助都是感激的:)
从自动加载中删除表单验证库,并将其加载到控制器中。
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);
}
}
改变这一行
class register_model extends CI_Controller {
并使类名的首字母大写。
class Register_model extends CI_Controller {
另外,文件名必须以大写字母开头。应该命名为Register_model.php