我得到了这个 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>');
}
}
这是因为_set_fields()
不是您正在调用validation
库的成员。
愿它有帮助
使用$this->load->library('form_validation');
而不是$this->load->library('validation')
现在将$this->validation
替换为$this->form_validation
用户指南有更好的解释 代码点火器-form_validation
如果我理解错了,请告诉我。
编辑
如果您想在form_validation
错误并发送回输入页面后将值保留在输入字段中,请在视图中使用 set_value
输入name="name"
的<input type="text" name="name" value="<?php echo set_value('name');?>">
编辑1
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