如何将表单数据与自定义Codeigniter表单验证器中的数据库数据进行比较



编辑:在我自己解决它的尝试中,已经混合了一些命名。我已经修复了回调等命名,并且相同的错误仍然存在。

我正在尝试为我的CodeIgniter网站创建一个登录页面。我已经有一个注册页面,可以将用户名和密码正确输入到"用户"表中。我遇到了了解创建自定义表单验证器所需功能的语法的问题。

我的错误是"无法访问与您的字段名称相对应的错误消息"和用户名自定义验证器。

这是控制器" login_ctrl"的相关部分

    class login_ctrl extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_mdl');
}
function index() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_userCorrect');
//Validating Password Field 
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_passwordCorrect');
//variables to pass form input
$username = $this->input->post('username');
$password = $this->input->post('password'); 
//reload login page if validation fails
if ($this->form_validation->run() == FALSE) {
$this->load->view('login');
} else {
//functions for custom validation
function userCorrect($username) {
    $this->load->library('form_validation');
    //the loads the model that contains the function to compare input to database data
    $userExists = $this->login_mdl->userExists($username);
    if ($userExists) {
        $this->form_validation->set_message(
           'userCorrect', 'correct user.'
    );
        return true;
    } else {
        $this->form_validation->set_message(
            'userCorrect', 'not a valid user name.'
        ); 
        return false;
    }
}
function passwordCorrect($password) {
    $this->load->library('form_validation');
    $passwordExists = $this->login_mdl->passwordCorrect($password);
    if ($passwordExists) {
        $this->form_validation->set_message('passwordCorrect', 'correct password.');
        return true;
    } else {
        $this->form_validation->set_message('passwordCorrect', 'invalid password.'); 
        return false;
    }
}

这是相应的视图"登录"

<?php echo form_open('login_ctrl'); ?>
<h1>Login</h1><hr/>
<?php if (isset($message)) { ?>
<?php } ?>
<?php echo form_label('User Name :'); ?> <?php echo form_error('username'); ?><br />
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?><br />
<?php echo form_label('Password :'); ?> <?php echo form_error('password'); ?><br />
<?php echo form_input(array('id' => 'password', 'name' => 'password')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>

最后,这是相应的模型" login_mdl"(我认为问题可能在这个人中)。

<?php
class login_mdl extends CI_Model{
function __construct() {
parent::__construct();
}
function userExists($username) {
    $this->db->select('id');
    $this->db->where('username', $username);
    $query = $this->db->get('users');
    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    }
}
function passwordCorrect($password) {
    $this->db->select('password');
    $this->db->where('username', $username);
    $query = $this->db->get('users');
    $result = $query->row();
    return $result->password;
    if ('password' == $password) {
        return true;
    } else {
        return false;
    }
}
}
?>

我认为我的问题与DB呼叫有关,如果是陈述,但是我一直在阅读文档,并且未能修复此数小时,那么一双新的眼睛将不胜感激。

您需要在服装规则功能上使您的字段名称与功能回调相同。因此,应该这样:

  $this->form_validation->set_message(
           'userCorrect', 'correct user.'

在其他功能上做同样的事情。

最新更新