Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: How to redirect properly with form validation

Tags:

codeigniter

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.

like image 632
sdot257 Avatar asked Jan 04 '10 16:01

sdot257


3 Answers

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', '');
like image 61
Cinnamon Avatar answered Nov 15 '22 04:11

Cinnamon


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.

like image 25
Teej Avatar answered Nov 15 '22 03:11

Teej


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>";
}
?>
like image 20
shin Avatar answered Nov 15 '22 05:11

shin