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

[eluser]callumd[/eluser]
Hi there,

CodeIgniter noob here, still trying to learn the ropes.

So I've been playing with CI's Form_Validation class, and according to the user manual, the following is proper:

Quote:$this->load->helper(array('form', 'url'));

$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');

What struck me as odd about this is that all the form rules are being set before the system even knows if this is a form submission attempt. Seems like a real waste of resources, and bad logic.

Is there a better way to do it, and if so, why doesn't it appear in the documentation?

Thanks in advance.
#2

[eluser]McNoggin[/eluser]
The first thing that set_rules does is check to see if there is any post data.

Code:
function set_rules($field, $label = '', $rules = '')
        {
                // No reason to set rules if we have no POST data
                if (count($_POST) == 0)
                {
                        return;
                }
              .....
#3

[eluser]callumd[/eluser]
Ahh, nice.

Ok, that's *a bit* better..
#4

[eluser]McNoggin[/eluser]
If you are worried about the overhead from the function calls to set_rules you could always do that check yourself in the controller.

if (count($_POST) > 0) {
set_rules(...)
}

That way you are only doing count($_POST) once for an empty form instead of each rule. On the other hand for a form with data it will add an extra call to it.
#5

[eluser]callumd[/eluser]
Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB