我是Codeigniter的新手。当我尝试检查电子邮件是否存在时,我有错误。我在Stackoverflow和其他网站上看到了很多帖子。我无法得到任何结果。
当我尝试下面的编码我得到以下错误
无法访问与您的字段名称对应的错误消息 电子邮件(email_check)
请检查我的代码。
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('login');
}
public function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("email","Email","required|trim|callback_email_check");
$this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
public function members()
{
$this->load->model('model_users');
if($this->model_users->can_log_in())
{
return true;
}
else
{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
}
MODEL
<?php
class Model_users extends CI_Model
{
public function can_log_in()
{
$this->db->where->('email',$this->input->post('email'));
$this->db->where->('password',md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
}
?>
答案 0 :(得分:0)
我相信您错过了回调函数 email_check , set_message 应该与函数对应,而不是字段本身。
答案 1 :(得分:0)
您必须添加正在使用的2个回调函数:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('login');
}
public function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("email","Email","required|trim|callback_email_check");
$this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
public function email_check(){
//perform your validation here
if({your_validation_result}){
return true;
}else{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
public function password_check(){
//perform your validation here
if({your_validation_result}){
return true;
}else{
$this->form_validation->set_message('password_check', 'Incorrect Username/Password');
return false;
}
}
public function members()
{
$this->load->model('model_users');
if($this->model_users->can_log_in())
{
return true;
}
else
{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
}