2016-06-27 68 views
1

我遇到問題,我的codeigniter不顯示錯誤消息。Codeigniter表單驗證不會顯示錯誤

當我在$this->form_validation->run()上執行var_dump時,如果一切都完成,則返回true,否則返回false。

下面是代碼:

控制器

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

class Logon extends CI_Controller { 
public function __construct() { 
     parent::__construct(); 
     $this->load->model('logon_model', 'logon'); 
     } 

public function autenthicate() { 
     $this->form_validation->set_rules('usuario', 'Usuario', 'required'); 
     $this->form_validation->set_rules('senha', 'Senha', 'required'); 

     if ($this->form_validation->run() == TRUE): 
      #Array with data 
      $data = elements(array('user', 'password'), $this->input->post()); 
      $data['password'] = md5($data['password']); 

      #Send data to model 
      if ($this->logon->do_login($data)->result()): 

       $data = $this->logon->do_login($data)->result(); 

       $user_data = array(
        'id' => $data['id'], 
        'name' => $data['name'], 
        'logged' => TRUE 
       ); 

       $this->session->set_userdata($user_data); 
       redirect('sales'); 

       $this->session->set_flashdata('loginerror', 'Login failed! User or password is invalid.'); 
       redirect('logon'); 
      endif; 
     else: 
      $data = array(
       'title' => 'Logon', 
       'screen' => 'index', 
      ); 

      $this->load->view('index_view', $dados); 
     endif; 
    } 

} 

查看

<?php 
        echo form_open('logon/autenthicate'); 
        echo '<div class="form-group">'; 
        echo form_label("User"); 
        echo form_input(array('name' => 'user', 'class' => 'form-control')); 
        echo '</div>'; 

        echo '<div class="form-group">'; 
        echo form_label("passwor"); 
        echo form_password(array('name' => 'password', 'class' => 'form-control')); 
        echo '</div>'; 

        echo '<div class="form-group">'; 
        echo form_submit('logon', 'Logon', 'class="btn btn-default btn-block"'); 
        echo '</div>'; 

        #Show a alert div with validation errors 
        echo validation_errors('<div class="alert alert-danger alert-dismissible" role="alert"> 
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>', '</div>'); 
        echo form_close(); 
        ?> 

顯然,這一切都對,但是我無法找到錯誤。如果表格填寫完成並且註冊用戶,則它將被重定向到主頁,如果沒有回到登錄屏幕,但不顯示錯誤。

你能幫我嗎?

+0

爲什麼在第二個IF語句中有兩個重定向?另外,最後一個load-> view('index_view',$ dados);.不應該是'$ data'而不是'$ dados'? – KFE

+0

是的,是$ data而不是$ dados。 –

回答

2

變化這(最佳實踐

if ($this->form_validation->run() == TRUE) 

if ($this->form_validation->run() == FALSE) 

既然你還沒有告訴表單驗證類使之生效的,它默認返回FALSE(boolean false)。 The run()如果方法已成功應用您的規則而不會失敗,則該方法僅返回TRUE。

Check Why


控制器中添加此

$this->form_validation->set_error_delimiters('<div class="alert alert-danger alert-dismissible" role="alert"> 
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>', '</div>'); 

刪除此

echo validation_errors('<div class="alert alert-danger alert-dismissible" role="alert"> 
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>', '</div>'); 

Check Why


形式加入這一低於相應的輸入字段

echo form_error('usuario'); 
echo form_error('senha'); 

或者添加

<?php echo validation_errors(); ?> 

Check Why


CodeIgniter Form Validation Example

+1

謝謝你的朋友!現在它工作完美。 –