Welcome Guest, Not a member yet? Register   Sign In
Form validation doesn't work!
#1

[eluser]felixk[/eluser]
Hi!
I've got a problem with my form validation, it always returns error, but no error displayed on the view. Here's the code:
Controller
Code:
// I include the necessary helpers and librarys in my autoload-file...
function Add_Comment()
{
        //Formulärregler
        $this->form_validation->set_rules('author', 'Namn', 'required|min_length[2]|max_length[40]|htmlspecialchars');
        $this->form_validation->set_rules('mail', 'E-post', '|required|valid_email|htmlspecialchars');
        $this->form_validation->set_rules('url', 'Hemsida', 'htmlspecialchars');
        $this->form_validation->set_rules('comment', 'Kommentar', 'required|min_length[2]|htmlspecialchars');    
        
        //Om error
        if ($this->form_validation->run() == FALSE)
        {
            redirect('blogg/id/'.$this->input->post('id'));
        }
        //Om inget error
        else
        {
            $query = array
            (
                'blogg_id' => ($this->input->post('id')),
                'name' => ($this->input->post('author')),
                'url' => ($this->input->post('url')),
                'mail' => ($this->input->post('mail')),
                'text' => ($this->input->post('comment')),
                'date' => date('Y-m-d, H:i'),
            );
            
            $this->blogModel->Add_Comment($query);
            redirect('blogg/id/'.$this->input->post('id'));
    
        } //Slut på "Om inget error (för formuläret)
}

The viewer:
Code:
<?php echo validation_errors(); ?>
               <form action="<?php echo base_url(); ?>blogg/Add_Comment/<?php echo $row->id; ?>" method="post" id="commentform">
                    <p>&lt;input type="text" name="author" id="author" value="" tabindex="1" style="margin-right:10px;" /&gt;&lt;label for="author">Namn *</label></p>
                    <p>&lt;input type="text" name="email" id="email" value="" tabindex="2" style="margin-right:10px;"  /&gt;&lt;label for="email">E-post *</label></p>
                    <p>&lt;input type="text" name="url" id="url" value="" tabindex="3" style="margin-right:10px;" /&gt;&lt;label for="url">Webbplats</label></p>
                    <p class="comment_box">
                        &lt;textarea name="comment" id="comment" tabindex="4" cols="40" rows="8"&gt;&lt;/textarea>
                    </p>
                    <p>
                        &lt;input name="submit" class="form_submit" type="submit" id="submit" tabindex="5" value="Skicka" /&gt;
                        &lt;input type="hidden" name="id" value="&lt;?php echo $row-&gt;id; ?&gt;" id="id" />
                    </p>
                &lt;/form&gt;
                </div>
There is some foreach-loops before the form, but that's irrelevant for now.

Any ideas?

//Felix Karlsson
#2

[eluser]Jamie Rumbelow[/eluser]
Because you're redirecting (after the $this->form_validation->run() call), the validator looses the state of the run and so when you call validation_errors(); in the view no errors are returned.

You can save the errors in the flash session store, then pulling it from that as opposed to calling the helper method:

Code:
//Controller
        //Om error
        if ($this->form_validation->run() == FALSE)
        {
            $this->session->set_flashdata('validation_errors', validation_errors());
            redirect('blogg/id/'.$this->input->post('id'));
        }

//View
&lt;?php echo $this->session->flashdata('validation_errors');

Alternatively, instead of redirecting when an error occurs, you could just load the view with $this->load->view Smile

Jamie




Theme © iAndrew 2016 - Forum software by © MyBB