[eluser]Unknown[/eluser]
Hi everyone!
I am new at CI and total noob, so this question maybe is stupid, but I don't get it. I created simple app for adding comments to news. Problem is, when somebody submit empty fields, error messages don't show. I used "echo validation_errors();". Can you help me to get this work?
Code:
<?php // SHOW FORM FOR COMMENTS // ?>
<div><?php echo validation_errors(); ?></div>
<?php echo form_open('news/comment_insert'); ?>
<div><?php echo form_hidden('news_id', $this->uri->segment(3)); ?></div>
<div><?php echo form_input(array('name' => 'author', 'type' => 'text', 'class' => 'font')); ?></div>
<div><?php echo form_textarea(array('name' => 'body', 'rows' => '10', 'cols' => '50', 'class' => 'font')); ?></div>
<div><?php echo form_input(array('value' => 'Submit Comment', 'type' => 'submit', 'class' => 'font')); ?></div>
<?php echo form_close(); ?>
Code:
function comment_insert()
{
// Set rules
$this->form_validation->set_rules('author', 'author', 'required');
$this->form_validation->set_rules('body', 'body', 'required');
if ($this->form_validation->run() == FALSE) {
// Redirect
redirect('news/comments/' . $_POST['news_id']);
} else {
// Insert comment in database
$this->db->insert('comments', $_POST);
// Redirect
redirect('news/comments/' . $_POST['news_id']);
}
}