Codeigniter不验证登录表单

时间:2019-03-08 20:45:44

标签: php codeigniter

我已经对Codeigniter非常满意,因此我对自己的代码充满信心。但是,我的表单验证不起作用。它永远不会通过验证,也不会显示任何错误。我已经放弃了CI实例,可以看到我的验证设置正确,但是没有运气。

这是我控制器中的功能

$this->load->helper(array('form', 'url', 'html'));
$this->load->library('form_validation');
$data = array();

$this->form_validation->set_rules('email', 'Email', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');

$validation_errors = validation_errors();
if( $validation_errors !== FALSE ):
    $data["validation_errors"] = $validation_errors;
endif;

if ($this->form_validation->run() == FALSE):
    $this->load->view('common/head-auth', $data);
    $this->load->view('pages/page-auth-login', $data);
    $this->load->view('common/foot-auth');
else:
    $this->load->model("site/users_model");
    $this->users_model->login();
endif;

这是我的视图文件

<main class="auth">
<header id="auth-header" class="auth-header"></header>

<?php
    $atts = array("class" => 'auth-form');
    echo form_open('auth/login', $atts);
?>
    <div class="form-group">
        <img src="<?php echo base_url( "assets/images/rplogo.png" ); ?>" alt="" width="100%">
    </div>
    <?php
        if( isset( $validation_errors ) ):
            $error_messages = "<div class=\"alert alert-danger alert-dismissible fade show\">";
            $error_messages .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>";
            $error_messages .= $validation_errors;
            $error_messages .= "</div>";
            echo $error_messages;
        endif;
    ?>
    <div class="form-group">
        <div class="form-label-group">
            <input type="text" id="inputEmail" class="form-control" placeholder="Email Address" name="email" value="<?php echo set_value('email'); ?>">
            <label for="inputUser">Email Address</label>
        </div>
    </div>
    <div class="form-group">
        <div class="form-label-group">
            <input type="password" id="inputPassword" class="form-control" placeholder="Password" name="password">
            <label for="inputPassword">Password</label>
        </div>
    </div>
    <div class="form-group">
        <button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
    </div>
    <div class="text-center pt-3">
        <a href="auth-recovery-password.html" class="link">Forgot Password?</a>
    </div>

</form>
<footer class="auth-footer"> © 2019 All Rights Reserved. </footer>

是的,我没有解决任何其他有关该主题的帖子。

预先感谢

1 个答案:

答案 0 :(得分:3)

if ($this->form_validation->run() == FALSE):
    $validation_errors = validation_errors();

    if( $validation_errors !== FALSE ):
        $data["validation_errors"] = $validation_errors;
    endif;

    $this->load->view('common/head-auth', $data);
    $this->load->view('pages/page-auth-login', $data);
    $this->load->view('common/foot-auth');
else:
    $this->load->model("site/users_model");
    $this->users_model->login();
endif;