开发者

Codeigniter: How to redirect properly with form validation

I understand how to do it w/ a plain form w/o existing values, but let's say I have a view that I can call via http://domain.com/account/settings. let's say I have two fields, username, password and city, which are all pulled from the DB (except for password of course). So, if a user tries to submit the form and fails validation for whatever reason, where should I "redirect" them to? 开发者_开发问答Right now, I have it showing the same view but the problem is, it pulls the info from the DB again. Should I be creating two different views?

The second view would essentially show the information they tried to enter along w/ the error message.


You do not need two separate views. Check out Form Helper's functions set_value(), set_select(), set_checkbox() and set_radio(). These re-populate form after its submission and validation. So in your case, you should specify the fields this way:

<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']); ?>" />

By default, the input will have $user['city'] value. But after failed validation it will be re-populated with previously entered values (including incorrect ones).

Just remember that all fields you want to re-populate need to be passed through form_validation library:

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


On the same controller you could have something like this:

    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');

That's all there is to it.

The idea is to have it redirect to itself or wherever you want, when the validation returns 'true' so that we kind of refresh the page, hence, update the page.

If the validation returns 'false' then you won't have to do anything.


Redirect to the same form.

And in your view give error information to the visitor.

There are two ways you can do this.

  1. Use this error in your view. This will show validation error info.

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

  2. Or you can use flashdata()

In your controller

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

And in your view, you need to show it.

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


Had the same problem and discovered that a redirection makes you lose the data that would have been provided by form_error(...) or validation_errors(), except you store such data in a session or in an array being passed into the loaded view.

The point to note is that you should redirect only if the data you want passed around is in session, else you should just load a view. The latter ensures that you have your validation errors intact when you reach the loaded view.


Just load same view if form validation failed

controller

$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);
}

View page

<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>

This code display form errors and repopulate the form

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜