CodeIgniter自定义验证功能不起作用

时间:2013-03-12 14:20:04

标签: codeigniter validation codeigniter-2

我正在使用CodeIgniter 2.3.1并在form_validation.php中创建了config文件,内容如下。

<?php
  $config = array(
array(
    'field' => 'firstname',
    'label' => 'First Name',
    'rules' => 'required'
),
array(
    'field' => 'lastname',
    'label' => 'Last Name',
    'rules' => 'required'
),
array(
    'field' => 'email',
    'label' => 'Email',
    'rules' => 'required|valid_email|callback_unique_email'
),
array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => 'required|matches[confirm_password]'
),
array(
    'field' => 'confirm_password',
    'label' => 'Confirm Password',
    'rules' => 'required'
)
 );

function unique_email($email) {
if($email == 'm@gmail.com') {
    $this->form_validation->set_message('unique_email', 'Hello World !');
    return false;
}
 }

?>

并检查form_validation控制器的register功能中的user。代码如下。

public function register() {
    $this->load->helper('form');
    $data['message'] = '';
    if($this->input->post('submit')) {
        $this->load->library('form_validation');
        if($this->form_validation->run() == FALSE) {
            $data['message'] = 'User could not be saved.';
        } else {
            $user_data['firstname'] = $this->input->post('firstname');
            $user_data['lastname'] = $this->input->post('lastname');
            $user_data['email'] = $this->input->post('email');
            $user_data['password'] = md5($this->input->post('password'));
            if($this->user_model->insert($user_data)) {
                if($this->user_model->login($user_data)) {
                    $this->session->set_flashdata('message', 'User saved successfully.');
                    redirect('/user', 'refresh');
                }
            }
        }
    } 

    $this->load->view('user/register', $data);
}

但我没有收到自定义方法的验证消息。请建议我怎么做?这项工作更受赞赏。

2 个答案:

答案 0 :(得分:3)

查看以下文档:http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

正如您在文档中看到的那样,自定义验证函数实际上属于控制器,而不是配置文件中。通过将验证功能移动到控制器,回调函数应该开始被调用。

另一个有趣的事实是,人们可以通过网址(即http://yoursite.com/index.php/user/unique_email)访问此unique_email函数。为了避免这种情况,我们可以通过简单地在函数的开头放置一个下划线来将函数编写为私有函数,如下所示:

function _unique_email($email) {
    ...
}

然后,您可以使用配置中的新函数名称在验证中调用该函数(注意回调中的额外下划线:

array(
    'field' => 'email',
    'label' => 'Email',
    'rules' => 'required|valid_email|callback__unique_email'
)

最后,您的控制器应该类似于以下内容:

class User extends CI_Controller {

    public function register() {
        $this->load->helper('form');
        $data['message'] = '';
        if($this->input->post('submit')) {
            $this->load->library('form_validation');
            if($this->form_validation->run() == FALSE) {
                $data['message'] = 'User could not be saved.';
            } else {
                $user_data['firstname'] = $this->input->post('firstname');
                $user_data['lastname'] = $this->input->post('lastname');
                $user_data['email'] = $this->input->post('email');
                $user_data['password'] = md5($this->input->post('password'));
                if($this->user_model->insert($user_data)) {
                    if($this->user_model->login($user_data)) {
                        $this->session->set_flashdata('message', 'User saved successfully.');
                        redirect('/user', 'refresh');
                    }
                }
            }
        } 

        $this->load->view('user/register', $data);
    }

    function _unique_email($email) {
        if($email == 'm@gmail.com') {
            $this->form_validation->set_message('unique_email', 'Hello World !');
            return false;
        }
    }

}

您的配置与以下内容类似:

$config = array(
    array(
        'field' => 'firstname',
        'label' => 'First Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'lastname',
        'label' => 'Last Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'email',
        'label' => 'Email',
        'rules' => 'required|valid_email|callback__unique_email'
    ),
    array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => 'required|matches[confirm_password]'
    ),
    array(
        'field' => 'confirm_password',
        'label' => 'Confirm Password',
        'rules' => 'required'
    )
);

答案 1 :(得分:1)

我使用了这个答案并得到了错误:

无法访问与您的字段名称对应的错误消息。

在函数_unique_email而不是set_message('unique_email','Hello World!');应该是set_message('_ unique_email','Hello World!');像这样:

function _unique_email($email) {
    if($email == 'm@gmail.com') {
        $this->form_validation->set_message('_unique_email', 'Hello World !');
        return false;
    }
}