Welcome Guest, Not a member yet? Register   Sign In
Form validation
#1

[eluser]Uplift[/eluser]
I am new to CI and have went through the video tutorials, i want to expand on that by adding validation to the form. Now the method in the user guide and the method in the video is different.

Code:
function comment_insert() {
    
        $this->load->library('form_validation');
        $this->form_validation->set_rules('body', 'Comment', 'required|xss_clean');
        $this->form_validation->set_rules('author', 'Name', 'trim|required|min_length[3]|max_length[15]|xss_clean');
        
        if ($this->form_validation->run() == FALSE) {
        
            $data['title'] = "My Comment Title";
            $data['heading'] = "My Comment Heading";
        
            $this->db->where('entry_id', $this->uri->segment(3));
            $data['query'] = $this->db->get('comments');
            
            redirect('blog/comments/'.$_POST['entry_id']);
        
        } else {
            
            $this->db->insert('comments', $_POST);
        
            redirect('blog/comments/'.$_POST['entry_id']);
            
        }
    

        
    }

If the form is VALID then it works fine. if the form is invalid it just refreshes the page and doesn't show the validation errors, i want the exact comments page and form to show, but with the error above it.

I have this in my comments_view

<?php echo validation_errors(); ?>

Why is it not showing the val errors?
#2

[eluser]cideveloper[/eluser]
First of all dont use $_POST, use $this->input->post
Secondly in either case you are just redirecting to

Code:
'blog/comments/'.$_POST['entry_id']

thus in both instances you will get the same result

you can do something like this even though I dont think its the best way.

Code:
function comment_insert() {

    $this->load->library('form_validation');
    $this->form_validation->set_rules('body', 'Comment', 'required|xss_clean');
    $this->form_validation->set_rules('author', 'Name', 'trim|required|min_length[3]|max_length[15]|xss_clean');

    if ($this->form_validation->run() == FALSE) {

        $data = array (
            'title' => 'My Comment Title',
            'heading' => 'My Comment Heading',
            'query' => $this->db->where('entry_id', $this->uri->segment(3))->get('comments'),
            'message' => (validation_errors()) ? validation_errors() : ''
        );
        $this->session->set_flashdata('message', $data['message']);
        redirect('blog/comments/'.$this->input->post('entry_id'));

    } else {
        
        $data = array (
            'body' => $this->input->post('body'),
            'author' => $this->input->post('author'),
        );
        $this->db->insert('comments', $data);
        $this->session->set_flashdata('message', 'Comment Added');
        redirect('blog/comments/'.$this->input->post('entry_id'));

    }

}

and in the view you would have

Code:
<?php echo $this->session->flashdata('message'); ?>


Like I said, I dont think this is the best way. usually you should have the form being displayed using load view when the form_validation is false.
#3

[eluser]Uplift[/eluser]
thanks i will give it a go.




Theme © iAndrew 2016 - Forum software by © MyBB