Welcome Guest, Not a member yet? Register   Sign In
Problem with posting and validating comments in an article
#1

[eluser]opel[/eluser]
I have almost finished coding my own blog system but I have one problem on validating the form and error messages when I post a comment related to an article.

My form appears in the view related to the article method and view e.g. site.com/blog/article/uniqueid

When I post the comment from the form I point the form to site.com/blog/comment. This what I have in my comment method :

Code:
function comment($id = NULL)
    {
            //set validation rules
    $this->form_validation->set_rules('comment_article_id', 'article id', 'required|integer');
    $this->form_validation->set_rules('comment_name', 'name', 'trim|max_length[100]');
    $this->form_validation->set_rules('comment_website', 'website', 'trim|max_length[100]|prep_url');
    $this->form_validation->set_rules('comment_email', 'email', 'trim|max_length[255]|valid_email');
    
    // set custom delimiters
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        
    if($this->form_validation->run())
    {    
        //insert
        $this->db->insert('comment', $this->form_validation->clean_post($_POST));
        //redirect
        $this->session->set_flashdata('message', 'Comment successfully added !');
            redirect('blog/article/'.$_POST['comment_slug'], 'refresh');
    }
    
    }


When a commment is succesfull it posts the person back to the article and submits the data to the db. The code above does this successfully.

The issue I am having is the the form validation and the error message is not displaying. I guess this is to do with one controller displaying and another validating. I want the errors to appear on my article method/view.

Any help appreciated, thanks.
#2

[eluser]TheFuzzy0ne[/eluser]
Use [url="http://ellislab.com/codeigniter/user-guide/libraries/sessions.html"]flashdata[/url].
#3

[eluser]Thorpe Obazee[/eluser]
It should go this way:

1. if validation is successful it saves the post to the table.
2. you set flashdata
3. you redirect.

Quote:When a commment is succesfull it posts the person back to the article and submits the data to the db. The code above does this successfully.
#4

[eluser]opel[/eluser]
Sorry I don't think I explained the problem correctly.

I can insert the comment and redirect. The issue is that I have the form displaying inside the article method of my blog class but my form is POSTing to the comment method. The validation from the comment method doesn't work.

Should my above code be moved into the article method or can I keep the comment code separate ?
#5

[eluser]Thorpe Obazee[/eluser]
I believe you have a very similar problem with this.

http://ellislab.com/forums/viewthread/114933/




Theme © iAndrew 2016 - Forum software by © MyBB