1

我正在使用 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);
}

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

4

2 回答 2

3

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

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

另一个有趣的事实是,人们可以通过 url 访问这个 unique_email 函数(即http://yoursite.com/index.php/user/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'
    )
);
于 2013-03-12T15:53:59.547 回答
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;
    }
}
于 2013-03-31T13:47:51.330 回答