2010-01-04 25 views
5

我知道如何做到這一點W/O現有的值,但我們可以說我有一個觀點,我可以通過http://domain.com/account/settings調用。比方說,我有兩個字段,用戶名,密碼和城市,這些都是從數據庫中提取的(當然除了密碼)。因此,如果用戶試圖提交表單並因任何原因未通過驗證,我應該在哪裏「重定向」它們?現在,我已經看到了相同的觀點,但問題是,它再次從DB中獲取信息。我應該創造兩種不同的觀點嗎?Codeigniter:如何使用表單驗證正確重定向

第二個視圖基本上顯示他們試圖沿着錯誤消息輸入的信息。

回答

4

您不需要兩個單獨的視圖。退房Form Helper's功能set_value(),set_select(),set_checkbox()set_radio()。這些表單在提交和驗證後會重新填充表單。所以你的情況,你應該指定的字段是這樣的:

<input type="text" 
     name="username" 
     value="<?php echo set_value('username', $user['username']); ?>" /> 
<input type="text" 
     name="city" 
     value="<?php echo set_value('city', $user['city']); ?>" /> 

默認情況下,輸入將使$user['city']值。但驗證失敗後,將重新填入之前輸入的值(包括錯誤值)。

只要記住,你希望所有字段重新填充需要通過form_validation庫進行傳遞:

$this->form_validation->set_rules('username', 'Username', 'required'); 
$this->form_validation->set_rules('city', 'City', ''); 
2

在同一個控制器,你可以有這樣的事情:

if ($this->form_validation->run('create_comment') === TRUE) 
    { 
     $this->comments_model->name  = $this->input->post('name', TRUE); 
     $this->comments_model->email = $this->input->post('email', TRUE); 
     $this->comments_model->website = $this->input->post('website', TRUE); 
     $this->comments_model->comment = $this->input->post('comment', TRUE); 
     $this->comments_model->create_comment(); 
        redirect(current_url()); 
    } 

    $this->load->view('your_view'); 

這一切有它。

這個想法是讓它重定向到自己或任何你想要的,當驗證返回'真',以便我們刷新頁面,因此,更新頁面。

如果驗證返回'false',那麼您將不必執行任何操作。

1

重定向到相同的表單。

並在您的視圖中給訪問者提供錯誤信息。

有兩種方法可以做到這一點。

  1. 在您的視圖中使用此錯誤。這將顯示驗證錯誤信息。

    echo validation_errors('< p class =「error」>','</p>');

  2. 或者你可以使用flashdata()

在你的控制器

... 
... 
$this->session->set_flashdata('msg', 'All fields are required. or other useful info here. Please try again!'); 
redirect('yourcontroller'); 

而且在你看來,你需要證明它。

<?php 
if ($this->session->flashdata('msg')){ //change! 
    echo "<div class='message'>"; 
    echo $this->session->flashdata('msg'); 
    echo "</div>"; 
} 
?> 
1

有同樣的問題,並發現重定向使你失去了本來應該由form_error(...)提供的數據...)或validation_errors(),除非您將這些數據存儲在會話中或傳遞到加載視圖中的數組中。

需要注意的是,只有當你想傳遞的數據在會話中時,你才應該重定向,否則你應該只加載一個視圖。後者確保您在到達加載的視圖時確保您的驗證錯誤完好無損。

0

只加載如果表單驗證失敗

控制器

$userData=array(
'username'=NULL, 
'password'=NULL 
); 
#set form validation rules 
$this->form_validation->set_rules('username', 'Username', 'required'); 
$this->form_validation->set_rules('password', 'Password', 'required'); 
#get all posted data ,this helps you in two ways, 
# 1. Get all form data and can be use here server side 
# 2. repopulating the form data by passing to view page 
$userData=$this->input->post(NULL, TRUE); 
#check form validation result 
if ($this->form_validation->run() == TRUE) { 
//do the operation 
redirect(URL); 

}else{ 
    $this->load->view($view, $userData); 
} 

查看頁面

<form method=post action='URL'> 
<input type='text' name='username'value='<?php echo $username?>'/> 
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?> 
<input type='text' name='password' value='<?php echo $password?>'/> 
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?> 
<input type='submit' value='submit'/> 
</form> 

此代碼顯示形式的錯誤相同的觀點和重新填充形式