2015-02-24 52 views
0

我正在使用Parse + CodeIgniter開發一個項目。 我有兩個屏幕來管理ParseUsers:allUsers.php,列出所有用戶。 和detailUser.php,創建/編輯用戶。CodeIgniter:表單驗證良好做法

當用戶提交表單(新的ParseUser)時,我調用save方法。包含代碼:

try{ 

       if ($user_id) 
        $user->save(true); // Update 
       else 
        $user->signUp(); // Insert 

       $this->session->set_flashdata('success', 'Success'); 
       redirect('all-users');    
      } 
      catch(Parse\ParseException $error){ 
       $error_message = $error->getMessage(); 
       $this->session->set_flashdata('error', 'Error: ' . $error_message); 
       $this->data['user'] = $user; 
       $this->view = 'user/userDetail'; 
      }   

而在看,我有這樣的(例如用戶名):

<input type="text" id="name" name="name" class="form-control" required value="<?php if(isset($user)): echo $user->name; endif;?>"> 

形式保持正確填寫的字段,但flashdata它是空的,如果我刷新頁面,比flashdata出現。 所以,我搜索後發現有人說flashdata只能用於重定向。

如果我重定向我的頁面,則會出現flashdata。但是,這些字段沒有填充。

我讀了關於set_value,這是維護字段的正確方法?但是,我喜歡只有兩個視圖,一個列出所有和另一個創建/編輯用戶。那麼,如果我使用set_value,這是可能的嗎? 最佳做法是什麼?

回答

0

使用負載,而不是重定向

 try{ 

      if ($user_id) 
       $user->save(true); // Update 
      else 
       $user->signUp(); // Insert 

      $this->session->set_flashdata('success', 'Success'); 
      $this->load->view('path');//use load instead of redirect    
     } 
     catch(Parse\ParseException $error){ 
      $error_message = $error->getMessage(); 
      $this->session->set_flashdata('error', 'Error: ' . $error_message); 
      $this->data['user'] = $user; 
      $this->view = 'user/userDetail'; 
     }