2014-03-31 161 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');