EllisLab text mark
Advanced Search
1 of 2
1
   
Comment Loading Issue
Posted: 25 July 2007 05:14 PM   [ Ignore ]
Avatar
Joined: 2007-05-22
85 posts

Okay, I will explain this the best I can.

First, the way this is layed out is. I have a main controller that loads blog entries on the home page, inside that controller is also a function that loads the comments_view for each blog entry when you load the comments. On the comment page is a form that will let you add a new comment, this form has validation and other data. The problem I have is the form points to another controller which handles the validation which works fine, but if validation fails I need it to load the view and load the data for that blog entry, which works via entry_id. Now, the issue at hand here is that when something fails I don’t know how to load the view with the data for that ID. I’ve tried a few different ways to do this. Below part of my code to try to help with explaining the issue. Thanks.

Comment’s Load Function:

function comments()
    
{
    
        $data[
'base_url'$this->config->item('base_url');
        
$this->db->where('entry_id'$this->uri->segment(3));
        
$data['query'$this->db->get('comments');
        
        
$fields['comment']    'Username';
        
$fields['name']    'Password';
        
$fields['email']    'Email Address';
    
        
$this->validation->set_fields($fields);

        
        
$this->load->view('pages/comment_view'$data);
        
    

Comment Submit code (only the part of the code that is the problem)

if ($this->validation->run() == FALSE)
        
{
            $data[
'base_url'$this->config->item('base_url');
            
            
redirect('home/comments/'.$_POST['entry_id']);
          
//neither of these work
            
$this->load->view('pages/comment_view'$data);
        

If your lost or confused on the issue tell me so I can try to explain this better. Thanks for any help you can give! smile

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 25 July 2007 07:27 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-17
24 posts

This might not be what you’re looking for, but consider the following:

if ($this->validation->run() == false{
    
// validation failed, prepare to redisplay form
    // define any variable data needed by the form
}
else {
    
// validation successful, save comment
    // finish up doing whatever, redirect wherever
}

$this
->load->view('myview'$data); 
 
Posted: 25 July 2007 07:44 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-06
743 posts
CodyPChristian - 25 July 2007 09:14 PM

The problem I have is the form points to another controller which handles the validation

Therein lies the problem. Typically you’d post-back to the same controller. If validation succeeds, then you can redirect wherever. If validation fails, however, you can’t redirect to another controller because then you’d lose state and all the form field values and error messages stored in the validator.

 Signature 

“I am the terror that flaps in the night”

 
Posted: 25 July 2007 09:30 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts
Rick Jolly - 25 July 2007 11:44 PM
CodyPChristian - 25 July 2007 09:14 PM

The problem I have is the form points to another controller which handles the validation

Therein lies the problem. Typically you’d post-back to the same controller. If validation succeeds, then you can redirect wherever. If validation fails, however, you can’t redirect to another controller because then you’d lose state and all the form field values and error messages stored in the validator.

Actully, I have to have it in another controller for my call backs to work. I had the same issue with callbacks on another CI project. Me and a few others figured out the work around on IRC, by sticking the validation in another controller. The reason why is because the call backs only work if the validation controller is index(), if its anything other then that then the call backs don’t work.

Edit: This method works very very good on anther site, just doesn’t on this one because this one relies on a dynamic page instead of a hard-coded page.

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 25 July 2007 09:34 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts
Bacteria Man - 25 July 2007 11:27 PM

This might not be what you’re looking for, but consider the following:

if ($this->validation->run() == false{
    
// validation failed, prepare to redisplay form
    // define any variable data needed by the form
}
else {
    
// validation successful, save comment
    // finish up doing whatever, redirect wherever
}

$this
->load->view('myview'$data); 

I will take a look at this doing it this way, Thanks smile

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 25 July 2007 11:07 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts

Okay, everything is back in one controller and validation is working fine but the main problem still exists. It can’t get the entry_id passed to the view and the view is loaded like this:

$this->load->view('pages/comment_view'$data); 

So if anyone can figure this out let me know please, thanks

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 25 July 2007 11:19 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-17
24 posts

An extremely common and simple way to accomplish this is to use a hidden input.

In your controller you can do

$data['entry_id '$entry_id

and in your form view do

<input type="hidden" name="entry_id" value="<?=$entry_id?>/> 
 
Posted: 25 July 2007 11:22 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-17
24 posts
Rick Jolly - 25 July 2007 11:44 PM

Therein lies the problem. Typically you’d post-back to the same controller. If validation succeeds, then you can redirect wherever. If validation fails, however, you can’t redirect to another controller because then you’d lose state and all the form field values and error messages stored in the validator.

You’re absolutely correct, which is why he’d be better off restructuring his flow.

 
Posted: 26 July 2007 12:03 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts

I’ve already fixed your last posting their bacteria, and thats not the issue and as far as the other post goes there is already one in place:

<?=form_hidden('entry_id'$this->uri->segment(3)); ?> 

Everything works fine until validation fails and it loads the page, then after that it no longer has the entry_id in the uri or in the form to so when its submitted again it doesn’t work.

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 26 July 2007 12:09 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-26
43 posts

sounds like a similar problem to what i had..

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

maybe it will help.

 
Posted: 26 July 2007 12:16 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts
woopsicle - 26 July 2007 04:09 AM

sounds like a similar problem to what i had..

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

maybe it will help.


Yes exactly the exact same problem! I can’t wait to see what you figure out for this, please get back to me with how this works out woopsicle.

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 26 July 2007 12:17 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-06
743 posts

Since the ‘entry_id’ hidden field isn’t part of the validation, you would have to reset it before loading the page when validation fails.

if ($this->validation->run() == FALSE)
{
    $data[
'base_url'$this->config->item('base_url');
    
$data['entry_id'$this->input->post('entry_id');
     
    
$this->load->view('pages/comment_view'$data);
 Signature 

“I am the terror that flaps in the night”

 
Posted: 26 July 2007 12:19 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts
Rick Jolly - 26 July 2007 04:17 AM

Since the ‘entry_id’ hidden field isn’t part of the validation, you would have to reset it before loading the page when validation fails.

okay.. have a example or something cause I don’t fully understand that.

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 26 July 2007 12:20 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-06
743 posts

Added an example to the post above. I’m wondering why you are doing this:

<?=form_hidden('entry_id'$this->uri->segment(3)); ?> 

instead of this:

<?=form_hidden('entry_id'$entry_id); ?> 

You might want to initialize entry_id in the controller when the form first loads instead of using $this->uri->segment(3); so that it will work on first load and on post-back when validation fails.

 Signature 

“I am the terror that flaps in the night”

 
Posted: 26 July 2007 12:28 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts

Okay tried that still not working, here is my URI’s and controller before and after a failed validation.

controller:

if ($this->validation->run() == FALSE)
        
{
            $data[
'base_url'$this->config->item('base_url');

            
$data['entry_id'$this->input->post('entry_id');

            
$this->db->where('entry_id'$this->uri->segment(3));
            
$data['query'$this->db->get('comments');
            
            
            
$this->load->view('pages/comment_view'$data);
        

uri before:
home/comments/39

uri after:
home/comment_insert

so its not passing the entry_id onto the uri for the validation.

here is the view:

<? $this->load->view('default/header'$data); ?>
                
                
<div id="blog">

                
<? if ($query->num_rows() > 0): ?>
                
                    <? 
foreach($query->result() as $row): ?>
                    
                    
<p><?=$row->comment?></p>
                    <
p><?=$row->name?></p>
                                        
                    
                    <
hr />
                            
                    
<? endforeach; ?>
                    
                <? 
endif; ?>
                
                
<p><?=anchor('home''Back to Blog');?></p>
                
                
<?=$this->validation->error_string?>
            
                <?
=form_open('home/comment_insert');?>
                
                <?
=form_hidden('entry_id'$this->uri->segment(3)); ?>
                
                
<p>Comment:<br /><textarea name="comment" rows="6" cols="20" ><?=$this->validation->comment;?></textarea></p>
                <
p>Name:<input type="text" name="name" value="<?=$this->validation->name;?>" /></p>
                <
p>Email:<input type="text" name="email" value="<?=$this->validation->email;?>" /></p>
                <
p><input type="hidden" name="ip" value="<? echo $_SERVER["REMOTE_ADDR"]; ?>" /></p>
                <
p><input type="submit" value="Submit Comment" /></p>
                
                </
form>
                
                </
div>
<? $this->load->view('default/footer'$data); ?> 

and finally here is the outputted HTML before and after:

before:

<form action="http://localhost:8888/codypchristiannew/home/comment_insert" method="post">                
                <
input type="hidden" name="entry_id" value="39" />                
                <
p>Comment:<br /><textarea name="comment" rows="6" cols="20" ></textarea></p>
                <
p>Name:<input type="text" name="name" value="" /></p>
                <
p>Email:<input type="text" name="email" value="" /></p>
                <
p><input type="hidden" name="ip" value="::1" /></p>
                <
p><input type="submit" value="Submit Comment" /></p>
                
                </
form

after:

<form action="http://localhost:8888/codypchristiannew/home/comment_insert" method="post">                
                <
input type="hidden" name="entry_id" value="" />                
                <
p>Comment:<br /><textarea name="comment" rows="6" cols="20" ></textarea></p>
                <
p>Name:<input type="text" name="name" value="" /></p>
                <
p>Email:<input type="text" name="email" value="" /></p>
                <
p><input type="hidden" name="ip" value="::1" /></p>
                <
p><input type="submit" value="Submit Comment" /></p>
                
                </
form
 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
Posted: 26 July 2007 12:32 AM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-22
85 posts
Rick Jolly - 26 July 2007 04:20 AM

Added an example to the post above. I’m wondering why you are doing this:

<?=form_hidden('entry_id'$this->uri->segment(3)); ?> 

instead of this:

<?=form_hidden('entry_id'$entry_id); ?> 

You might want to initialize entry_id in the controller when the form first loads instead of using $this->uri->segment(3); so that it will work on first load and on post-back when validation fails.

okay, I didn’t see this till after I made that big post, I gave this a try and so far so good well sorta, it now loads the form when it fails and shows the errors like it has been, but this time I get a value for entry_id in the form hidden line which is good. But I do not get the posts at the top of the page from the query that is also on that page.

Edit: last problem is now fixed smile

 Signature 

Thanks, CodyPChristian

More about me here: CodyPChristian.net
Host of LearnCI - learn-codeigniter.com

 
1 of 2
1