CI 3.1.8 form_validation不起作用?

时间:2018-04-17 09:07:21

标签: codeigniter

为什么这个简单的form_validation总是返回false?它是来自CI的错误吗?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Authentication extends CI_Controller {
    public function __construct()
    {
        parent::__construct();

        $method = $this->router->fetch_method();
        $redirect = array(
            '', 'index', 'login',
        );

        if (in_array($method, $redirect))
        {
            if ($this->session->userdata('logged_in') === true)
                redirect('pages');
        }

        $this->load->model('user_model', 'user');
    }

    public function index()
    {
        $this->load->view('login');
    }

    public function login() 
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

        if ($this->form_validation->run() == FALSE) echo "FALSE"; 

        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
        );
        exit();
    }

    public function logout()
    {
        $this->session->unset_userdata('logged_in');
        redirect(base_url());
    }
}

编辑:

<?php echo validation_errors(); ?>
<?php echo form_open('login', array('class' => 'form-horizontal')); ?>

    <div class="form-group m-b-20">
        <div class="col-xs-12">
            <label for="username">Username</label>
            <input name="username" class="form-control" type="text" id="username" required="" placeholder="Username">
        </div>
    </div>

    <div class="form-group m-b-20">
        <div class="col-xs-12">
            <a href="pages-forget-password.html" class="text-muted pull-right font-14">Forgot your password?</a>
            <label for="password">Password</label>
            <input name="password" class="form-control" type="password" required="" id="password" placeholder="Enter your password">
        </div>
    </div>

    <div class="form-group m-b-30">
        <div class="col-xs-12">
            <div class="checkbox checkbox-primary">
                <input id="checkbox5" type="checkbox">
                <label for="checkbox5">
                    Remember me
                </label>
            </div>
        </div>
    </div>

    <div class="form-group account-btn text-center m-t-10">
        <div class="col-xs-12">
            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign In</button>
        </div>
    </div>

<?php echo form_close(); ?>

2 个答案:

答案 0 :(得分:2)

xss_clean不再是Codeigniter 3中表单验证的一部分。对于xss_clean,您必须使用安全类,如

$data = $this->security->xss_clean($data);

更多细节 https://www.codeigniter.com/user_guide/libraries/security.html

您也可以使用安全助手

https://www.codeigniter.com/user_guide/helpers/security_helper.html

修订了login方法

public function login() 
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() === FALSE) {
       echo 'FALSE';return
    } 

    $data = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password')
    );
   //now process $data
}

答案 1 :(得分:1)

在视图部分:你直接调用控制器的功能: <?php echo form_open('login', array('class' => 'form-horizontal')); ?>。应该是这样<?php echo form_open('Authentication/login', array('class' => 'form-horizontal')); ?>希望它能帮到你