error: Undefined property: News::$form_validation

2019-08-12 17:34发布

问题:

I am new to the codeigniter and trying the tutorials in the user guide. But i stuck i the third tutorial which is for the create news items. Here in my controller section it is giving the following error:

Severity: Notice

Message: Undefined property: News::$form_validation

Filename: controllers/news.php

Line Number: 44

and the code that i used in the controller section is

public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a news item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');

if ($this->form_validation->run() === FALSE)
{
    $this->load->view('templates/header', $data);   
    $this->load->view('news/create');
    $this->load->view('templates/footer');

}
else
{
    $this->news_model->set_news();
    $this->load->view('news/success');
}
}

Please help me.

Thanks in advance.

回答1:

Propably you don't specify loading model. So include this before

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

after that you can call

$this->news_model->set_news();

or edit config\autoload.php and set your model, then you can use after successfully validation

news_model::set_news();

or you can that in constructor, like this:

class MySomeClas extend CI_Controller
 {
  parent::__construct();
  $this->load->model('news_model');
 }

and call models right