【问题标题】:CodeIgniter Auto Loading form_validation causes Error with Model filesCodeIgniter 自动加载 form_validation 导致模型文件出错
【发布时间】:2016-10-13 16:31:09
【问题描述】:

我正在使用 CodeIgniters 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();

   }

}

?>

任何帮助表示赞赏:)

【问题讨论】:

  • 这是整个register_model 文件吗?
  • @DFriend 是的,这就是整个文件,我要做的就是将表单中的数据插入数据库。稍后我会将查询插入到查询函数中。
  • 错误信息究竟是什么时候出现的?当你加载页面?什么时候提交表格?
  • @DFriend 当表单通过所有验证并提交时
  • 如果从validateRegForm 中删除(或注释掉)$this-&gt;register_model-&gt;insertUsers($data); 会发生什么?仍然得到错误?

标签: php codeigniter model-view-controller


【解决方案1】:

从自动加载中移除表单验证库,并将其加载到控制器中。

  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);
    }
}

【讨论】:

  • 这成功了!太感谢了。这是有原因的吗?
【解决方案2】:

改变这一行

class register_model extends CI_Controller {

并将类名的首字母大写。

class Register_model extends CI_Controller {

此外,文件名必须以大写字母开头。它应该命名为Register_model.php

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 2013-06-05
    • 2011-10-06
    • 2012-12-08
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多