2011-05-23 20 views
0

是否有可能如何顯示控制器字符串以在codeigniter中查看? 即時進行表單驗證,以檢查數據庫中是否存在某個記錄。如何在codeigniter中使用FormValidation

這裏是我的控制器

function create_customer() 
     { 
      // field name, error message, validation rules 
      $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_username_exists'); 
      $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email'); 
      $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
      $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); 

      if($this->form_validation->run() == FALSE) 
      { 
       $this->load->view('admin/users/admin_form'); 
      } 

      else 
      {   
       if($query = $this->usermodel->create_customer()) 
       { 
         redirect('userslist'); 
       } 
       else 
       { 
        $this->load->view('admin/users/admin_form.php');    
       } 
      } 

     } 

      function username_exists($key) 
     { 
      $this->usermodel->username_exists($this->input->post('username')); 
     } 

代碼在這裏對我的模型

function create_customer() 
    { 
      $new_member_insert_data = array(
       'username' => $this->input->post('username'), 
       'email' => $this->input->post('email'), 
       'password ' => md5($this->input->post('password '))  
      ); 
      $insert = $this->db->insert('users', $new_member_insert_data); 
      return $insert; 
    } 

    function username_exists($key) 
    { 
     $this->db->where('username',$this->input->post('username')); 
     $query = $this->db->get('users'); 
     if ($query->num_rows() > 0){ 

      return true; 
     } 
     else{ 
      return false; 
     } 
    } 

什麼,我想發生是代碼;我將如何顯示預期的錯誤消息,如「用戶名已存在」,我想在我的視圖中顯示它。這可能嗎?或者我應該嘗試採取另一種方法?

回答

1

你可以學習here。但爲了您更好的理解,我會解釋整個事情。你控制器應該是這樣的

function create_customer() 
{ 
    // field name, error message, validation rules 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_username_exists'); 
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
    $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); 

    if($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('admin/users/admin_form'); 
    } 
    else 
    {   
     if($query = $this->usermodel->create_customer()) 
     { 
      redirect('userslist'); 
     } 
     else 
     { 
      $this->load->view('admin/users/admin_form.php');    
     } 
    } 
} 

function username_exists($key) 
{ 
    if($this->usermodel->username_exists($key)) 
    { 
     $this->form_validation->set_message('username_exists', 'User Name already Exists'); 
     return FALSE; 
    } 
    else 
    { 
     return TRUE; 
    } 
} 

下面的代碼爲您型號

function username_exists($key) 
{ 
    $this->db->where('username',$key); 
    $query = $this->db->get('users'); 
    if ($query->num_rows() > 0){ 

     return true; 
    } 
    else{ 
     return false; 
    } 
} 

查看應包含以下行:

<?php echo validation_errors(); ?> 

如果仍有問題要了解請讓我k現在。

-1

由於您正在使用Form Validation類,因此會顯示錯誤消息。您只需使用form_error()即可撥打錯誤消息。欲瞭解更多詳情,請訪問http://codeigniter.com/user_guide/libraries/form_validation.html

也傳遞變量查看,你需要當你調用視圖來顯示 $this->load->view('YOUR_VIEW', array('VARIABLE_NAME' => "VARIABLE_VALUE"));

+0

你能解釋一下這裏有什麼問題嗎? – Tapos 2011-05-23 18:06:32

0

通過它您可以使用

在控制器: $this->session->set_flashdata('message', '<div class="message">YES!</div>');

然後只需撥打您的看法:<?php echo $this->session->flashdata('message'); ?>

希望有幫助

-1

您可以在控制器類中定義一個變量。

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

    public function home() 
    { 
     //This variable will be automaticly passed to the view. 
     $this->data = "some data"; 

     $this->load->view('home_main'); 
    } 

home_main.php:

<html> 
<head> 
    <title>My Page!</title> 
</head> 
<body> 
    <?php echo $this->data ?> 
</body> 
</html> 

它是一種有用的方法。

+0

我想你錯過了正確理解問題。 – Tareq 2011-05-23 17:55:31

相關問題