2010-01-04 98 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> 

此代码显示形式的错误相同的观点和重新填充形式