• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Validation shows error before submitting form

#1
[eluser]iateaduck[/eluser]
Hey, I'd really appreciate if anyone could help me figure out why this is going on, or even help me fix the problem.

Basically, I have a multi-step form. It starts at:

www.mydomain.com/formcontroller/step1 (where step1 is the name of the function)

there's no problem with the form there. validation works as expected. the first time that this is run:
if ($this->form_validation->run() == FALSE)
validation_errors() returns nothing, because it somehow knows that you havent submitted the form yet (anyone know how?)
and that's correct/expected behavior. if i actually click "next step" to submit the form, THEN the form errors will show up in form_validation if there are any. again, this is correct.

however, i go on to:
www.mydomain.com/formcontroller/step2
and validation_errors() automatically already runs through the rules (without clicking submit/next button). This means that when i get to step 2, the errors show up even before the user has a chance to enter values.

my code looks like this (these functions are in the same controller):

function step1()
{
$this->form_validation->set_rules('blahfield', 'Meow cats', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('step1_view');
}
else
{
// go to second step
redirect('formcontroller/step2');
}
}

function step2()
{
$this->form_validation->set_rules('zingfield', 'Woof cats', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('step2_view');
}
else
{
// go to finish step
redirect('formcontroller/step3');
}
}


as you can see, they are pretty much identical. but for some reason step2 displays errors before the user can submit. any advice? the view files are also pretty mcuh the same ones as the ones in the guide: http://ellislab.com/codeigniter/user-gui...ation.html

thanks,
eatingaduck

#2
[eluser]ramm[/eluser]
I think the validation library triggers the errors when there's post data, that's why you are getting errors in the second step, seems like it's getting the post data from step1.

An idea, is to set a hidden field and check if it's sent to the function before validate your data.

#3
[eluser]iateaduck[/eluser]
Thanks a lot!
You were right, the validation library checks only if there is post data.
I did a simple check to see if there was post data coming from the first step (checking the post field names) and if there was, the errors were surpressed in the view.

Thanks again ramm.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.