与自定义的回调函数espected笨表单验证不起作用(Codeigniter form valida

2019-09-30 10:22发布

我用笨的内置表单验证库以验证电子邮件输入字段。

这是我的验证规则:

$this->form_validation->set_rules('email', '<b>email</b>', 'trim|htmlspecialchars|required|valid_email');

当使用上述验证规则一切都在工作,我espected的方式。

例如:

  • 如果输入字段为空它表明:电子邮件是必需的。
  • 如果用户输入的不是一个有效的电子邮件,它表明:电子邮件地址无效。

然后,我通过添加回调函数添加自定义表单验证规则。

这是我修改过的验证规则:

$this->form_validation->set_rules('email', '<b>email</b>', 'trim|htmlspecialchars|required|valid_email|callback_mail_check');

而且,这是我的回调函数:

public function mail_check() {

        if (!$this->users_model->get_user_by_email_address($this->input->post('email', TRUE))) {

            $this->form_validation->set_message('mail_check', 'Your <b>email</b> could not be found.');
            return FALSE;

        } else {
            return TRUE;
        }

    }

现在,当我提交表单没有填写电子邮件字段或无效的电子邮件提交,它总是出把自定义的回调函数的验证消息(您的电子邮件无法找到。)。

但它没有办法的办法,我想。

我想先验证电子邮件字段为空值,则有效的电子邮件,该CALLBACK_FUNCTION后。

Answer 1:

您正在使用的验证规则是正确的。 只是删除那些不需要一些规则

$this->form_validation->set_rules('email', '<b>email</b>', 'required|valid_email|callback_mail_check');

回调自动添加当前验证的参数。 你并不需要从GET / POST方法读取它。

public function mail_check($email)
{
    if (!$this->users_model->get_user_by_email_address($email)) {

        $this->form_validation->set_message(__FUNCTION__, 'Your <b>email</b> could not be found.');
        return false;
    }
    return true;
}


文章来源: Codeigniter form validation not work as espected with custom callback function