我是使用 Codeigniter 的新手,需要设置表单验证规则和自定义验证消息,以检查电子邮件地址是否存在于以忘记密码表单提交的数据库中。如果电子邮件地址存在,它会发送一封电子邮件,如果不存在,则应重新加载表单,将字段值设置为用户输入的内容并提供电子邮件地址不存在的错误。
我在控制器中有以下内容......
public function forgot_password()
{
$this->form_validation->set_rules('email_address','Email Address','trim|required|valid_email');
if($this->form_validation->run() == FALSE)
{
$this->forgot_password_form();
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->val_forgot_password())
{
$data['main_content'] = 'forgot_password_sent';
$this->load->view('includes/template', $data);
}
else
{
$this->form_validation->set_message('email_address','The email address you provided does not exist.');
$this->forgot_password_form();
}
}
}
在模型中...
public function val_forgot_password()
{
$this->db->where('email_address', $this->input->post('email_address'));
$query = $this->db->get($this->members);
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
形式是...
<?php
echo form_open('login/forgot_password');
echo "Email Address: " . form_input('email_address', set_value('email_address', ''));
echo br(2);
echo form_submit('submit','Send Email');
echo form_close();
echo br(1);
echo validation_errors('<p>Error: ');
?>
当使用有效的电子邮件地址提交表单时,它会正确转到成功页面,但如果电子邮件地址不存在,它似乎会重新加载表单但不会给出错误。
请帮忙!:D