CodeIgniter Forums

Full Version: Handling multiple forms on a single view - what crafty ways are there to handle this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]nikefido[/eluser]
I'm trying to figure this out -
I'm using the Validation library and Form helper - I am trying to figure out how I can handle validation for both forms separately (both forms are on the same view).

Check it out: http://www.freelanceplacement.com (current working)

and http://www.freelanceplacement.com/FreelanceCI <-- trying to put it into CI framework.

Anyway, i followed the example in the Validation documentation, but that way would appear to work for only one form (as you need to set fields and rules for those fields)...

if i click submit on one, will it try to validate every field on both forms?
EDIT - the answer is yes, it attempts to validate every field regardless of what form it is Sad

El Forum

[eluser]nikefido[/eluser]
I'll ask this here also - the validation class docs have examples of dealing with check boxes, but should I not name the checkbox to an array (i.e. naming it something like "myCheckBox[]" which is the usual for reading checkboxes into php to allow for multiple items being checked)

El Forum

[eluser]nikefido[/eluser]
im feeling totally stone walled - tonanbarbarian has an interesting solution here:
http://ellislab.com/forums/viewthread/48535/#327544

But this doesn't seem to handle radio/checkboxes/select issues (?)

El Forum

[eluser]kaosweaver[/eluser]
I was under the impression you'd use different names for checkboxes and use the fieldName[] for a multiselect select form element. As for the validation, yeah, that is something I don't know how to do.

El Forum

[eluser]nikefido[/eluser]
I suppose I could pop the two forms (or one of them) into iFrames, but that's a less than ideal work around.

El Forum

[eluser]kaosweaver[/eluser]
How about have the forms be sent to different controllers?

El Forum

[eluser]nikefido[/eluser]
[quote author="kaosweaver" date="1205198029"]How about have the forms be sent to different controllers?[/quote]
that'll work for using the data, however for validation it is harder to implement.

I figured out a work around, but it's totally not a great one.

with in the two forms, I have the "name_error" (and other error fields) and "name" (and other fields" only print/echo/whatever when a certain form is set.

Within the controller, there is some flow logic of which fields to set and which rules to set also based on which form has been submitted.

I suppose it's "obvious" but I don't think it's what people had in mind when using the validation class Tongue

El Forum

[eluser]purpleparasol[/eluser]
I came looking here as I have the same problem.

I want more people to sign up to my email newsletter, and I want more people to request a brochure to be sent in the snailmail. Both forms need to be on the home page.

Having read your struggles with a similar issue, I wonder if the answer is to place two dummy forms on the page - ie just plain forms, no validation, nothing.
Then when the user fills one in, send them to a dedicated view with the form repeated, and carry through whatever they typed in on the first page to the dedicated form, then do the validation there.

I am off to to give it a go.

El Forum

[eluser]nikefido[/eluser]
You can basically make 2 hidden fields with different values (one hidden field per form). If that form field "isset" (aka $this->input->post('hiddenField1') != FALSE), then you know which form to validate.

You end up with two different sets of $rules / $fields within your index() function, but it works.

El Forum

[eluser]Michael Ekoka[/eluser]
You would handle them exactly the same as if you have one form in one view. When you submit a form to the server, it only handles elements form that form. So, your application stays blind to elements from the other form (their value never actually leave the client).

So you can point both forms to 2 different actions in your controller.

Code:
// the form view
&lt;form action='/form1' &gt; ... &lt;/form&gt;
&lt;form action='/form2' &gt; ... &lt;/form&gt;


// the controller
class The_Controller
{
    function home() {...}
    function form1(){...}
    function form2(){...}
}

The views in CodeIgniter are controller independent, so they are not bound to a specific action. You can use the same view from the three actions (home, form1, form2).
Pages: 1 2