Welcome Guest, Not a member yet? Register   Sign In
validation_errors() when using three forms in one view
#1

[eluser]Casperlarsen94[/eluser]
How can i do so, when the validation in the first form not will be displayed in form number three and four?

I hope you understand me
#2

[eluser]LuckyFella73[/eluser]
You mean you have 3 forms and when you fill all the input
of all 3 forms and submit you get only error messages or
repopulated fields of one form?
If thats your case: you can validate and repopulate only
one form per time because only the data from the form you
submit is send to the controller.
#3

[eluser]Casperlarsen94[/eluser]
I have three forms in ONE view file, and when i for example submit the second form, then my validation_errors(); in the view, print out the errors for form 2 in all three forms.
#4

[eluser]Casperlarsen94[/eluser]
My page look like this: http://i1008.photobucket.com/albums/af20...dwwddw.png

When i'm posting one of the forms (all three of them have different form actions) then it print out the errors in all three forms.
I know it does it, because i use validation_errors(); in all three of dem.

Is there a way to do so it only print out they errors in the submited form?

Sorry for my english
#5

[eluser]LuckyFella73[/eluser]
Maybe you could set some if/else statemnts in your controller
and check which form was submitted. Set a hidden input field
for each form and check which form was send. Then in your
controller:
Code:
if ( isset($this->input->post('form_1')) and strlen($this->input->post('form_1'))>1)
{
// validate form 1
}elseif( isset($this->input->post('form_2')) and strlen($this->input->post('form_2'))>1)
{
// validate form 2
}elseif( isset($this->input->post('form_3')) and strlen($this->input->post('form_3'))>1)
{
// validate form 3
}
Didn't test that but it should work.
#6

[eluser]nuwanda[/eluser]
Three forms is the same as one form.

Each form has an action. It submits to a unique controller method.

Each method handles the appropriate form.

No conflicts.
#7

[eluser]Casperlarsen94[/eluser]
Thanks for the answers.

I created a view to each form instead Smile
#8

[eluser]Madmartigan1[/eluser]
[quote author="LuckyFella73" date="1292809499"]Maybe you could set some if/else statemnts in your controller
and check which form was submitted. Set a hidden input field
for each form and check which form was send. Then in your
controller:
Code:
if ( isset($this->input->post('form_1')) and strlen($this->input->post('form_1'))>1)
{
// validate form 1
}elseif( isset($this->input->post('form_2')) and strlen($this->input->post('form_2'))>1)
{
// validate form 2
}elseif( isset($this->input->post('form_3')) and strlen($this->input->post('form_3'))>1)
{
// validate form 3
}
Didn't test that but it should work.[/quote]

This would work, but $this->input->post() will return false if it's empty or not set, so all you would need is:
Code:
if ($this->input->post('form_1'))
{
// validate form 1
}
if ($this->input->post('form_2'))
{
// validate form 2
}

I use this sometimes when I have 2 or more forms that post to the same controller.
#9

[eluser]edjon2000[/eluser]
Hello,

I am having similar issues, I am trying to understand how best to use form validation when there is more than one form on a page.

[quote author="Madmartigan1" date="1293916742"]
This would work, but $this->input->post() will return false if it's empty or not set, so all you would need is:
Code:
if ($this->input->post('form_1'))
{
// validate form 1
}
if ($this->input->post('form_2'))
{
// validate form 2
}

I use this sometimes when I have 2 or more forms that post to the same controller.[/quote]

this solution seems to be the best idea although, I am not sure how to implement it, sorry for the dumb questions but I am still relatively new to CodeIgniter and MVC in general.

would it be possible to expand on this slightly

Jon
#10

[eluser]nuwanda[/eluser]
Jon, each form has an action attribute. That is, the address that the form submits to. In CI, your action submits to a controller/method.

If you have two forms on a page, simply set the form action to submit to the appropriate controller/method and process the form inputs as needed.




Theme © iAndrew 2016 - Forum software by © MyBB