遇到PHP错误:消息:未定义属性:NewsController :: $ validation

时间:2013-10-31 10:18:58

标签: php codeigniter validation

我得到了这个erorr:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: NewsController::$validation

Filename: controllers/NewsController.php

Line Number: 169
  
    

致命错误:在第169行的C:\ AppServ \ www \ News \ application \ controllers \ NewsController.php中的非对象上调用成员函数_set_fields()

  

...

class NewsController extends CI_Controller{
// num of records per page
private $limit = 10;

    function News(){
    parent::Controller();

    // load library
    $this->load->library(array('table','validation'));

    // load helper
    $this->load->helper('url');

    // load model
    $this->load->model('NewsModel','',TRUE);
    }
   function _set_fields(){
    $fields['id'] = 'id';
    $fields['title'] = 'title';
    $fields['image'] = 'image';
    $fields['discreption'] = 'discreption';

    $this->validation->_set_fields($fields);
    }
        // validation rules
   function _set_rules(){
    $rules['title'] = 'trim|required';
    $rules['image'] = 'trim|required';
    $rules['discreption'] = 'trim|required';

    $this->validation->set_rules($rules);

    $this->validation->set_message('required', '* required');
    $this->validation->set_message('isset', '* required');
    $this->validation->set_error_delimiters('<p class="error">', '</p>');
  }

 }

1 个答案:

答案 0 :(得分:1)

这是因为_set_fields()不是您正在调用的validation库的成员。

愿它有所帮助

使用$this->load->library('form_validation');

而不是$this->load->library('validation')

现在将$this->validation替换为$this->form_validation

用户指南有更好的解释 codeigniter-form_validation

让我知道我是否理解你。

修改

如果您希望在form_validation错误后输入字段中保留值并发送回输入页面,请在视图中使用set_value ,如

<input type="text" name="name" value="<?php echo set_value('name');?>">输入name="name"

<强> EDIT1

function __construct() {
parent::__construct();
  // load library,helper and models here. so you can use them all over inside this class
 $this->load->library('form_validation');    // load library
 $this->load->library('table');
 $this->load->helper('url');// load helper
   // load model
 $this->load->model('NewsModel');
}

 function news(){
  }
    //and other codes