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]nicholas.byfleet[/eluser]
Instead of using a redirect, I would recommend loading a view file to deal with the input, since I am pretty sure that the validation errors will not keep in memory between multiple page loads. Does what I am saying make sense?
#3

[eluser]felixk[/eluser]
Probably, but even if i'm filling in the form correctly, so it shouldn't be any errors, it's loading the "error-view"... So it might be another problem as well. But I'll try your tip first.
#4

[eluser]felixk[/eluser]
No, you were right, that solved the problem! Thanks a lot Smile
#5

[eluser]nicholas.byfleet[/eluser]
No problem. Happy coding!
#6

[eluser]phused[/eluser]
If you still want to use redirects you can store the validation errors on a flashdata item.

Code:
$this->session->set_flashdata('form_errors', validation_errors());

You can then echo the errors on your view:
Code:
echo $this->session->flashdata('form_errors');

(I'm not sure if this is the best approach, but if I'm not mistaken your session can only hold 4KB of data, so if your form is large, using this method isn't a good idea).




Theme © iAndrew 2016 - Forum software by © MyBB