Welcome Guest, Not a member yet? Register   Sign In
Repopulation after validation not wanted
#1

[eluser]bakpakka[/eluser]
I'm impressed the forum is so active, keep it up! If I could seek some assistance from the CI whizzes, it would be appreciated.

I currently have a blog script that displays an article with the ability the submit comments below. After posting a new comment, the validate class can return an error if a field is not filled. It will the repopulate any completed fields.

What if it was a successful transaction? I return the user to the article page, but do not want the fields repopulated. What can I do? Here's a snippet of code.

Thanks, all.

Code:
// Load up validation library, rules and fields
            $this->load->library('validation');
                
            $rules['comment'] = $rules['name_person'] = "trim|required";
            $this->validation->set_rules($rules);

            $fields['name_person'] = "Name";
            $fields['comment'] = "Comment";
            $this->validation->set_fields($fields);

            // Validation is passed, store in db
            if ($this->validation->run() == TRUE)
            {
                $insert_db = array('date_created'=>date('Y-m-d H:i:s'), 'article_id'=>$article_id, 'created_by'=>'1', 'status'=>'active', 'name_person'=>htmlspecialchars($_POST['name_person']), 'content'=>htmlspecialchars($_POST['comment']));
                $this->db->insert('articles_comments', $insert_db);
                $row['success_msg'] = "Your comment has been posted";
                // Stop the repopulation of data, then return to the view

            }

        $template['content'] = $this->load->view('articles/article_view', $row, true);
        $this->load->view('themes/moo/article', $template);
#2

[eluser]Seppo[/eluser]
You can redirect('your_controller') and you will stop the repopulation. Or you can send a boolean to the view telling not to repopulate the fields.
#3

[eluser]Michael Wales[/eluser]
I think the redirect is the easiest option by far. Maybe not the absolute cleanest but effective nonetheless.
#4

[eluser]bakpakka[/eluser]
Thanks for the speedy replies. I was hoping there would be a quick way such as:

$this->validation->unset_fields();
#5

[eluser]Michael Wales[/eluser]
I think redirect('comment'); is quicker. :-)
#6

[eluser]bakpakka[/eluser]
Do I need to put an exit; after the redirect? As this portion of code should theoretically still execute after it.

Code:
$template['content'] = $this->load->view('articles/article_view', $row, true);
$this->load->view('themes/moo/article', $template);

And I suppose I'll have to switch my $row['success_msg'] to a session now.
#7

[eluser]Seppo[/eluser]
The redirect function has an exit, so you don't need to do it.
About the success message, you should store in session to show it on the redirected page.
#8

[eluser]bakpakka[/eluser]
Awesome feedback. Thanks all CodeIgniters out there for the help - I'll be on my way to developing my first application then.
Hopefully a tad more useful than "Hello World"




Theme © iAndrew 2016 - Forum software by © MyBB