CodeIgniter Forums
Need assist Filtering Comments/Returning Errors to appropriate view. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Need assist Filtering Comments/Returning Errors to appropriate view. (/showthread.php?tid=18824)

Pages: 1 2


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]dancrew32[/eluser]
Hey CodeIgniters, I'm a little new and trying to add some filtering to the inputs of content along the lines of this CMS tutorial, but I am having some difficulty:

- Inserting filtered (xss_filter / prep_for_form) content to the database
- Returning the errors (associated with the defined $rules) in the appropriate "Comment View" ($this->validation->error_stringWink
- Obtaining the correct "Comment View" after performing an erroneous insert_comment() method.

Here is my code:
----
Controller: http://pastie.org/483538
Model: http://pastie.org/483540
View: http://pastie.org/483542
Comment View: http://pastie.org/483543

Any assists to point me in the right direction would be highly appreciated. Thank you for your time.


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]Thorpe Obazee[/eluser]
First of all, try using the new Form_Validation Library. The Validation Library you are using has been deprecated.


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]dancrew32[/eluser]
[quote author="bargainph" date="1242807007"]First of all, try using the new Form_Validation Library. The Validation Library you are using has been deprecated.[/quote]

I loaded that new Form_Validation library and updated the validation in the view and controller.

here is the new movie_comment_view.php and Movie.php

Still not sure how to get back to the add/display comments view after passing invalid data:

Code:
if ($this->form_validation->run() == FALSE) {
    $this->load->view('front/movie_comment_view', $data); // Pass Back invalid data
        // Need some kind of way to get back to 'movie/comments/'. $data['movie_id'] with error messages
} else {
    $this->Movie_model->insertRow('movie_comments', $data);
    redirect('movie/comments/'. $data['movie_id']);    
}

Here are the errors that I'm getting after entering just a ['comment'] and leaving ['author'] blank (in an attempt to input invalid data: Errors.png


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]Thorpe Obazee[/eluser]
Code:
if ($this->form_validation->run() === TRUE) {
    $this->Movie_model->insertRow('movie_comments', $data);
    redirect('movie/comments/'. $data['movie_id']);    
}
Try this


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]dancrew32[/eluser]
[quote author="bargainph" date="1242809196"]
Code:
if ($this->form_validation->run() === TRUE) {
    $this->Movie_model->insertRow('movie_comments', $data);
    redirect('movie/comments/'. $data['movie_id']);    
}
Try this[/quote]

That works fine when passing in valid data, but when passing invalid data, it just goes to a blank page with this header: (index.php/movie/insert_comment)

I need a way to get back to index.php/movie/comments/X after passing invalid data (X being the original comment page I was on).

Here are the errors that I was getting in the previous post after entering just a [‘comment’] and leaving [‘author’] blank (in an attempt to input invalid data: Errors.png

(Thanks for helping btw, you rock.)


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]Thorpe Obazee[/eluser]
Hmm... I think I get it now. where do you point your 'form_open()'? I mean to which method does it submit? I'd submit it to the comments method then do the 'add' logic there.


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]dancrew32[/eluser]
form_open() points to movie/insert_comment

in movie_comment_view.php:

Code:
<p>&lt;?php echo $this->form_validation->error_string; ?&gt;</p>

&lt;?php echo form_open('movie/insert_comment'); ?&gt;

&lt;?php echo form_hidden('movie_id', $this->uri->segment(3)); ?&gt;

<p><label for="comment">Comment:</label>
&lt;textarea name="comment" rows="8" cols="40"&gt;
    
&lt;/textarea&gt;&lt;/p>    

<p><label for="author">Author:</label>
    &lt;input type="text" name="author" value=""&gt;&lt;/p>    

<p>&lt;input type="submit" value="Submit Comment"&gt;&lt;/p>


I thought there was a way to keep all of the functions for inserting, updating, deleting entries in separate functions.. I think if I nest all of them inside of comment(), it will get kinda messy.. does that make sense? There must be a way to keep them all separate and tidy =/


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]Thorpe Obazee[/eluser]
You can still do that but I wouldn't submit to movie/insert_comment. Submit to movie/comments then call the method, insert_comment from there via:

Code:
$success = $this->insert_comment();

if ($success)
{
    // do stuff
}

edit: it's probably better to set insert_comment to private if you do this option.


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]dancrew32[/eluser]
I threw in some pseudocode in comments() because I'm not particularly sure how to do form_open('movie/comments'); and trigger some kind of conditional statement to run through insert_comment($data).

Code:
function comments()
{
    $data['title']    = 'Comments List';
    $data['comments'] = $this->Movie_model->fetchRow($this->uri->segment(3), 'movie_id', 'movie_comments');
    
        if post is detected {
        
        // Data Input
        $data['comment']  = $this->input->post('comment', TRUE); // XSS Filtered?
        $data['author']   = $this->input->post('author', TRUE);
        $data['movie_id'] = $this->input->post('movie_id', TRUE);
        
        insert_comment($data);

    }
    
    $this->load->view('front/movie_comment_view', $data);
}

private function insert_comment($data)
{
    $this->form_validation->set_rules('comment', 'Comment', 'required|min_length[1]|max_length[100]|xss_clean|prep_form');
    $this->form_validation->set_rules('author', 'Author', 'required|min_length[1]|max_length[40]|xss_clean|prep_form');
            
    if ($this->form_validation->run() == FALSE) {
        $this->load->view('front/movie_comment_view', $data);        
    } else {
        $this->Movie_model->insertRow('movie_comments', $data);
        redirect('movie/comments/'. $data['movie_id']);    
    }
}

Sorry for noobin ya..


Need assist Filtering Comments/Returning Errors to appropriate view. - El Forum - 05-19-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="dancrew32" date="1242812094"]I threw in some pseudocode in comments() because I'm not particularly sure how to do form_open('movie/comments'); and trigger some kind of conditional statement to run through insert_comment($data).

Code:
function comments()
{
    $data['title']    = 'Comments List';
    $data['comments'] = $this->Movie_model->fetchRow($this->uri->segment(3), 'movie_id', 'movie_comments');
    

        
        $this->_insert_comment();
    
    $this->load->view('front/movie_comment_view', $data);
}

private function _insert_comment()
{
    $this->form_validation->set_rules('comment', 'Comment', 'required|min_length[1]|max_length[100]|xss_clean|prep_form');
    $this->form_validation->set_rules('author', 'Author', 'required|min_length[1]|max_length[40]|xss_clean|prep_form');
            
    if ($this->form_validation->run() === TRUE) {
        $this->Movie_model->insertRow('movie_comments', $data);
        redirect(current_url());    
    }
}

Sorry for noobin ya..[/quote]

Code:
// in your view
&lt;?php echo form_open('movie/comments'); ?&gt;

untested but please try this.