[eluser]ReasonMan[/eluser]
I've got my controller setup right now with 2 main functions that handle the brunt of the work, commentInsert and showArticle. As you can imagine, commentInsert is responsible for the validation and insertion of user comments into a database, and show article is responsible for getting that data and presenting it back to the user. My validation is pretty simple, make sure the name and body fields aren't empty, if they are, display the error on the same page. My uri naming is like this:
http://domain.ext/blog/year/month/article-title
When I submit a comment that will fail, it does validate and display the errors on the correct page, but ignores my routes and displays the following url:
http://domain.ext/blog/commentInsert
I should say the only way i've been able to get it to even display the error messages is to manually call showArticles on a failed validation. I feel like i'm overlooking something simple, and i'm over complicating things. I don't think I can create a route for it since showArticles accepts 3 parameters, the year, month and title.
[eluser]Thorpe Obazee[/eluser]
show code and someone will probably see the 'problem'
[eluser]ReasonMan[/eluser]
Oops, yeah that might help.
commentInsert:
Code: function commentInsert()
{
$data['header'] = $this->load->view('header', '', true);
$data['footer'] = $this->load->view('footer', '', true);
//make sure our fields are filled
$this->form_validation->set_rules('author', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('body', 'Comment', 'required');
$this->db->where('id', $this->input->post('postid'));
$this->db->select('postyear, postmonth, permalink, commentcount');
$QueryResult = $this->db->get('blog');
$Row = $QueryResult->row();
if($this->form_validation->run() == false)
{
$this->showArticle($Row->postyear, $Row->postmonth, $Row->permalink);
}
else
{
$this->form_validation->set_rules('name', 'Name', 'trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean');
$this->form_validation->set_rules('body', 'Comment', 'trim|xss_clean');
//get relevant info from comment table so we can create a comment permalink
$CommentRow = $this->db->get('comments');
$CommentData = array(
'postid' => $this->input->post('postid'),
'author' => $this->input->post('author'),
'body' => $this->input->post('body'),
'date' => mdate('%F %d, %Y'),
'time' => mdate('%h:%i %a'),
'website' => $this->input->post('website'),
'permalink' => 'comment-' . $CommentRow->num_rows());
//insert new comment
$this->db->insert('comments', $CommentData);
$commentcount = $Row->commentcount;
$commentcount++;
//update selected article with new commentcount
$this->db->where('id', $this->input->post('postid'));
$this->db->update('blog', array('commentcount' => $commentcount));
redirect('blog/' . $Row->postyear . '/' . $Row->postmonth . '/' . $Row->permalink);
}
}
showArticle:
Code: function showArticle($ArticleYear, $ArticleMonth, $ArticleTitle)
{
$data['header'] = $this->load->view('header', '', true);
$data['footer'] = $this->load->view('footer', '', true);
//get article to display with comments
$this->db->select('id, title, date, author, body, commentcount, postyear, postmonth');
$this->db->where('permalink', $ArticleTitle);
$data['blogQuery'] = $this->db->get('blog');
//does article exist?
if($data['blogQuery']->num_rows() > 0)
{
$Article = $data['blogQuery']->row();
//get comments for associated article(postid)
$this->db->select('body, author, date, time, permalink, website');
$this->db->where('postid', $Article->id);
$data['commentsQuery'] = $this->db->get('comments');
$data['CommentCounter'] = 1;
$this->load->view('comment_view', $data);
}
//redirect to home if not
else
redirect('');
}
Form:
Code: <?=form_open('blog/commentInsert');?>
<?=form_hidden('postid', $blogArticle->id);?>
<p>
<?=form_error('author')?>
<input type="text" name="author" id="author" value="<?=set_value('author')?>" tabindex="1"/>
<label for="author">
Name(required)
</label>
</p>
<p>
<?=form_error('email')?>
<input type="text" name="email" id="email" value="<?=set_value('email')?>" tabindex="2"/>
<label for="email">
Email(will not be published)(required)
</label>
</p>
<p>
<?=form_error('website')?>
<input type="text" name="website" id="website" value="<?=set_value('website')?>" tabindex="3"/>
<label for="website">
Website
</label>
</p>
<?=form_error('body')?>
<p><textarea id="body" tabindex="4" name="body" value="<?=set_value('body')?>"></textarea></p>
<p><input type="submit" value="Post" /></p>
</form>
I think that's all the relevant code, if you need anything else let me know.
[eluser]Thorpe Obazee[/eluser]
Sorry, working.
Anyway, Why do you set the rules twice?
Code: $this->form_validation->set_rules('name', 'Name', 'trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean');
$this->form_validation->set_rules('body', 'Comment', 'trim|xss_clean');
You end up with the uri, http://domain.ext/blog/commentInsert cause your form sends you there.
I am feeling a hint of CakePHP.
[eluser]ReasonMan[/eluser]
Hmmm, now that i'm looking at it, i'm not sure. I guess I had a reason but i've put the preping in the first group of setters.
I know that's where my form sends me, but before I put the validation in there, after a submission commentInsert would redirect a uri I formed using information pulled from the db. Now with the validation in place, I need to end up on the same page, but with the error messages. If I redirect after a validation failure, I don't get my error messages. If I directly call showArticle, I get my messages, but I lose my uri naming. It seems like a route would fix this, but I don't really know where to begin. I'm still new to CI, only about a week in.
[eluser]Thorpe Obazee[/eluser]
I suggest you post to:
Code: form_open('controller/function/' . $this->uri->segment(3) . '/' . $this->uri->segment(4) . '/' . $this->uri->segment(5)
The 'controller/function', you can change depending on your routing, and of course $this->uri->segment(3), etc would change depending also on your routing.
or just use
[eluser]ReasonMan[/eluser]
Ah ok that did the trick. Thanks alot.
[eluser]Thorpe Obazee[/eluser]
No problem
|