Codeigniter似乎总是将validation-> run设置为FALSE

时间:2018-09-02 02:36:25

标签: codeigniter-3

控制器:User.php

public function index()
{
log_message('debug', ' Index User');    
    die("should never get here");
}

public function login()
{
log_message('debug', ' Login Entered');    
echo '<pre> Printing Login data:';
    print_r($_POST);
    echo "</pre>";
    $data['pageHeader'] = "Login";
    $data['message'] = 'temporary message - This will be the login stuff';
    $this->session->set_flashdata('flashInfo', 'Login form will go here');

    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    if ($this->form_validation->run() === FALSE)
    {
    log_message('debug', ' validation run FALSE');    
    $this->load->helper('form');
        $this->render_page('user/login', 'public_header', 'footer', $data);
    }
    else
    {
        log_message('debug', ' validation run TRUE');    
        $remember = (bool) $this->input->post('remember');
        if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember))
        {
            log_message('debug', ' redirect Dashboard');    
            redirect('dashboard');
        }
        else
        {
            $_SESSION['auth_message'] = $this->ion_auth->errors();
            $this->session->mark_as_flash('auth_message');
            log_message('debug', ' redirect user login');    
            redirect('user/login');
        }
    }




}

日志已知: 输入功能登录名,  $ _POST始终为“空”, 表单验证->始终为FALSE,从而记录调试消息并重新显示表单

表格user / login.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<div class="container">
<?php echo $message;?>
<h1>Login</h1>

<form action="#" method="post">
    <div class="imgcontainer">
        <img src="http://www.hdkumdo.com/smen/assets/crow2.png" alt="Swordsmen Martial Arts">
    </div>

    <div class="container">
        <label for="username"><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="username" id="username" required autofocus>

        <label for="password"><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="password" id="password" required>

        <button type="submit" name="login" id="login" value"login" >Login</button>
        <label>
        <input type="checkbox" checked="checked" name="remember" id="remember"> Remember me
        </label>
    </div>

    <div class="container" style="background-color:#f1f1f1">
        <button type="button" class="cancelbtn">Cancel</button>
        <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
</form>

</div>

https://www.codeigniter.com/user_guide/libraries/form_validation.html复制了一个示例 并在表单中发布数据,但是如果我将函数索引更改为登录则无法正常工作。

菜单链接:登录正常。 一切似乎都已连接...显示登录表单,输入了控制器功能登录名,但似乎没有POST数据,因此只需重新显示该表单即可。我有2条简单的验证规则,因此,如果我仅在字段中输入任何数据,似乎validate-> run应该返回true。 我敢肯定这很简单,但是我一生都看不到。

1 个答案:

答案 0 :(得分:0)

First of all make sure you have loaded form validation library

$this->load->library('form_validation');
//set field validation
$this->form_validation->set_rules('fieldname','Field Name','required');

        if($this->form_validation->run())     
        {  
//then process form
//redirect
}
else {
//load view
}