未显示Codeigniter表单验证

时间:2013-07-22 06:28:21

标签: codeigniter login

以某种方式我的codeigniter表单验证没有显示在登录页面上 以下是我在视图中的登录页面

            <div class="full_w">
        <p>Food4U</p>
        <?php echo validation_errors(); ?>
            <?php echo form_open('staff/valid_login'); ?>
                <label for="city">Select City</label>
                <select name="city" class="text">
                    <option value="">Select City</option>
                    <?php echo $this->front_model->get_Fcities(); ?>
                </select>
                <label for="login">Username:</label>
                <input id="login" name="username" class="text" />
                <label for="pass">Password:</label>
                <input id="pass" name="password" type="password" class="text" />
                <div class="sep"></div>
                <button type="submit" class="ok">Login</button>
                <a class="button" href="">Change Password</a>
            <?php echo form_close(); ?>
            </form>
        </div>

以下是控制器中的函数

    function valid_login()
{
    $this->form_validation->set_rules('city','City','required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('username','Username','required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('password','Password','required|trim');
    if($this->form_validation->run())
    {
        $data = array(
                'username' =>$this->input->post('username'),
                'is_loggedin' => 1
                );
            $this->session->set_userdata($data);
            redirect('staff/member');
    }
    else 
    {
        $this->load->view("dashboard/login");
    }

 }

 function member()
 {   
    $row['content']=$this->load->view("dashboard/member",array(),true);
    $this->load->view("dashboard/template", $row);
 }

function validate_credentials() 
{
    if($this->staff_model->can_login())
    {
        return true;
    }
    else
    {
        $this->form_validation->set_message('validate_credentials','Incorrect Username or Password');
        return false;
    }
}

我的模特函数是

    function can_login()
{
    $this->db->where('DB_User_City',$this->input->post('city'));
    $this->db->where('DB_User_Login',$this->input->post('username'));
    $this->db->where('DB_User_Pass',$this->input->post('password'));

    $query= $this->db->get('frp_db_user');

    if($query->num_rows() == 1)
    {
        return true;
    }
    else 
    {
        return false;
    }
}

如果用户只需点击提交按钮,用户就会看到一条消息,如果用户提供的任何单个值不正确,那么用户还必须看到登录错误,以便用户可以提供正确的信息

2 个答案:

答案 0 :(得分:0)

您的代码似乎没问题。试试这个

if($this->form_validation->run() == FALSE)
{ 
    //redirect to your login form
}
else
{
    //redirect to dashboard
}

如果您遇到任何问题,请告诉我。

答案 1 :(得分:0)

在您的视图中为您设置的每个字段验证打击其字段或其他位置 写下这些代码来显示验证错误。

      <div class="full_w">
    <p>Food4U</p>
    <?php echo validation_errors(); ?>
        <?php echo form_open('staff/valid_login'); ?>
            <label for="city">Select City</label>
            <select name="city" class="text">
                <option value="">Select City</option>
                <?php echo $this->front_model->get_Fcities(); ?>
            </select>
            <br />
            <?=form_error('city')?>
            <br />
            <label for="login">Username:</label>
            <input id="login" name="username" class="text" />
            <br />
            <?=form_error('username')?>
            <br />
            <label for="pass">Password:</label>
            <input id="pass" name="password" type="password" class="text" />

            <br />
            <?=form_error('password')?>
            <br />

            <div class="sep"></div>
            <button type="submit" class="ok">Login</button>
            <a class="button" href="">Change Password</a>
        <?php echo form_close(); ?>
        </form>
    </div>