0

我的视图包含一个具有 3 个类似输入的表单:

<input type="text" name="email1" />
<input type="text" name="email2" />
<input type="text" name="email3" />

我添加了一个回调来控制每个文本框都不会收到当前会话电子邮件。

public function check_session_email($email){
    if($this->session){
        if ($this->session->userdata('email') != $email){
            return TRUE;
        }else{
            $this->form_validation->set_message('check_session_email', 'You can't include your own email address.');
            return FALSE;
        }
    }
}

如果我使用当前会话电子邮件,错误消息会显示两次(三个相同)。

在此处输入图像描述

当然,这听起来合乎逻辑......但不是很用户友好。所以我的问题是:如何只包含一条规则的错误消息?

4

2 回答 2

1

您最好的选择是在表单验证库之外进行验证。

控制器方法:

// general validation rules

if( $this->input->post('email1') == $this->session->userdata('email') || $this->input->post('email2') == $this->session->userdata('email') || $this->input->post('email3') == $this->session->userdata('email') )
{
    $data['own_mail_error'] = true;
}

if ($this->form_validation->run() == FALSE || isset($data['own_mail_error']))
{
  $this->load->view('myform', $data);
}
else
{
    $this->load->view('formsuccess');
}

而你的观点:

<?php echo validation_errors(); ?>
<?php if(isset($own_mail_error)): ?>
<p>You can't include your own email address.</p>
<?php endif; ?>
于 2012-02-25T20:39:20.327 回答
0
    public function check_session_email($email)
    {
        if($this->session)
        {
            if ($this->session->userdata('email') != $email)
            {
                return TRUE;
            }
        }   
        else
        {
            if($this->form_validation->_field_data['email1']['error']=='' && $this->form_validation->_field_data['email2']['error']=='' && $this->form_validation->_field_data['email3']['error']=='')
            {
                $this->form_validation->set_message('check_session_email', 'You can\'t include your own email address.');
            }
            return FALSE;
        }
    }
于 2012-02-25T20:50:44.220 回答