2014-03-31 117 views
0

可能有一些我错过了。我环顾网络,因此没有任何答案可以帮我解决这个问题。这是我的问题:我在__construct方法中加载form_validation库仍然PHP抱怨validation_errors方法未定义。还有form助手是自动加载的。Codeigniter validation_errors函数未定义

... 
... 
function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('ion_auth'); 
    $this->load->library('form_validation'); 
    $this->load->helper('url'); 
.... 
.... 
function login() 
{ 
    $this->data['title'] = "Login"; 
    ..... 
    ..... 
    //the user is not logging in so display the login page 
     //set the flash data error message if there is one 
     $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); 

上面最后一行出现错误。 (此代码是ion_auth模块http://benedmunds.com/ion_auth/的一部分)

+0

您是否已经运行验证? –

+0

编号页面错误在第一次命中本身。 – nilobarp

回答

1

你可以试试这个:

$this->data['message'] = ($this->form_validation->run() == False) ? validation_errors() : $this->session->flashdata('message'); 

另外,还要确保你调用validation_errors()辅助函数之前,因为这个功能在这个帮助文件可helper (form_helper.php)被加载。要加载帮助程序文件,您可以使用:

$this->load->helper('form'); 
+0

嗯..但我以前使用过这个代码,并且是标准库的一部分,因此它看起来像当前配置的问题。该部分的任何暗示? – nilobarp

+0

我不确定'ion_auth',但如果错误消息说'validation_errors()'函数没有被定义,那么肯定没有加载helper,你甚至可以从'config/autoload.php'使用' $ autoload ['helper'] = array('form');'在每个请求中自动加载它。 –

+0

是的,它已经添加到自动加载。该函数仍然是未定义的。接下来,如果我注释掉错误行,我会得到一个'form_open()'错误' – nilobarp

0

您应该调用form_validation类的run()方法。你也应该定义表单变量。

$this->load->library('form_validation'); 
$this->form_validation->set_rules('username', 'Username', 'required'); 
$this->form_validation->set_rules('password', 'Password', 'required'); 
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); 
$this->form_validation->set_rules('email', 'Email', 'required'); 

if ($this->form_validation->run() == FALSE) 
    $this->data['message'] = $this->session->flashdata('message');