Codeigniter validation_errors函数未定义

时间:2014-03-31 19:16:24

标签: php codeigniter validation

可能有一些我错过的东西。我环顾网络,所以我找不到任何答案。这是我的问题:我在form_validation方法中加载__construct库仍然PHP抱怨validation_errors方法未定义。此外form帮助程序也是自动加载的。

...
...
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/

的一部分

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

在调用helper (form_helper.php)辅助函数之前,还要确保已加载validation_errors(),因为此函数在此帮助文件中可用。要加载帮助文件,您可以使用:

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

答案 1 :(得分: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');