自动加载form_validation库

时间:2018-12-26 06:17:15

标签: codeigniter

我在Codeigniter中自动加载form_validation库时遇到问题。我有一个带有创建功能的控制器Posts,效果很好。

public function create(){
       $data['title']='New Post';

              $this->load->view('templates/header');
              $this->load->view('posts/create', $data);
              $this->load->view('templates/footer');   
}

现在我要进行表单验证。像这样:

public function create(){
       $data['title']='New Post';
       $this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
       $this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
       if($this->form_validation->run==FALSE){
              $data['errors']=validation_errors();
              $this->load->view('templates/header');
              $this->load->view('posts/create', $data);
              $this->load->view('templates/footer');   
       }else {
              $this->load->view('templates/header');
              $this->load->view('posts/success', $data);
              $this->load->view('templates/footer'); 
       }
}

目前,我没有调用任何model来存储数据,只是通过加载posts/success视图来显示成功消息。但是,即使在我使用验证代码(即上面的代码)编码create函数之前,我还是在form_validation中添加autoload.php的那一刻(即使是第一个代码)也是如此:

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

我遇到以下错误:

  

消息:未定义的属性:Post_model :: $ load

     

文件名:libraries / Form_validation.php

     

行号:147

     

回溯:

     

文件:C:\ xampp \ htdocs \ ciblog \ index.php行:292功能:   require_once

我不理解该错误,因为我什至没有在方法中使用Post_model

我的Post_model.php是:

class Post_model extends CI_Controller {
public function get_post($slug=NULL){
   if(!$slug) {
     $query = $this->db->get('posts');  
     return  $query->result();

  } else {
     $this->db->where('slug',$slug);
     $query = $this->db->get("posts");
     return $query->result();
  }
 } 
}

我完整的帖子控制器是这样的:

<?php
class Posts extends CI_Controller {
public function index(){
    $data['title']='Latest Posts';
    $posts=$this->post_model->get_post();
    $data['posts']=$posts;
    $this->load->view('templates/header');
    $this->load->view('posts/index',$data);
    $this->load->view('templates/footer');
}
public function view($slug){
    $posts=$this->post_model->get_post($slug);
    if(empty($posts)){
        show_404();
    } else {
       $data['posts']=$posts;
       $this->load->view('templates/header');
       $this->load->view('posts/view',$data);
       $this->load->view('/templates/footer');
    }   
}
public function create(){
       $data['title']='New Post';
       $this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
       $this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
       if($this->form_validation->run()==FALSE){
              $data['errors']=validation_errors();
              $this->load->view('templates/header');
              $this->load->view('posts/create', $data);
              $this->load->view('templates/footer');   
       }else {
              $this->load->view('templates/header');
              $this->load->view('posts/success', $data);
              $this->load->view('templates/footer'); 
       }
   }
}

我对正在运行的模型进行了自动浇铸。我搜索了其他帖子,其中大部分与模型名称的首字母不在大写中有关,这不是我的情况。有人可以让我了解出什么问题了吗?

2 个答案:

答案 0 :(得分:1)

您在不加载模型的情况下在控制器中使用了模型,必须先加载模型:

$this->load->model('post_model');

这里的另一个错误是模型扩展了CI_Model

class Post_model extends CI_Model

答案 1 :(得分:0)

如文档所述ref,您必须像这样进行验证:

我看到了 run()方法

背后的错误

请使用:

对其进行修复

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

您还可以通过更新构造函数直接加载form_validation库进行检查

public function  __construct()
  {
     parent::__construct();
     $this->load->library('form_validation');
     $this->load->helper('form');
  }
相关问题