Welcome Guest, Not a member yet? Register   Sign In
Handling multiple forms on a single view - what crafty ways are there to handle this?
#1

[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: www.freelanceplacement.com (current working)

and 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
#2

[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)
#3

[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 (?)
#4

[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.
#5

[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.
#6

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

[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
#8

[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.
#9

[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.
#10

[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).




Theme © iAndrew 2016 - Forum software by © MyBB