Multiple Forms on Same Page |
Hello All.
I have joined this forum a few minutes after I downloaded and installed CI 3.0.6. And this is the first time I am using anything like CI. Though I have been working on Core PHP and Wordpress, I have never used any MVC before and I am quite a few hours into it only. So here is my problem - I have been asked to implement a website for my newly joined job and lets start with the home page first - The home page has 3 Forms - a. Login form - via the Main Menu b. Subscribe form and c. Signup form (b and c on the lower end of the page). Gladly, the help on Internet did help me integrate Bootstrap into Code Igniter and also the documentation helped me get started. But now, I am stuck up with these forms. Code Snippets- The Home Controller Class PHP Code: class Home extends CI_Controller{ The Home View - This has the Nav Bar (with a dropdown Login Form) and then there are two forms - Subscribe and Signup Now, the problem that crops up is - When I submit the Subscribe form with an intentional error (that violates the rules), the error messages are displayed on all 3 forms. I did read on Internet that it is not advisable to have 3 forms in one view and also, if there is no way out, then, I should use Hidden fileds to identify which form has been submitted. But somehow, as I am quite very new here, I would love to read what the Experienced Community on this forum thinks. What are the best practices in such situations. Thanks in advance for your patience to read through this and helping me become proficient in using CI. Warm regards, Kevin.
You can have more then one form on a page. Your form field names should be unique for each form. You can display the errors specific to the exact field - study this:
http://www.codeigniter.com/user_guide/li...dividually (07-28-2016, 11:19 AM)cartalot Wrote: You can have more then one form on a page. Your form field names should be unique for each form. You can display the errors specific to the exact field - study this: Thanks for this. I might not have found this segment of the document that easily. Now, if I do this, do you think it would be a better idea to load the form validation library locally in the function to validate - for instance, the subscribe() or the dologin()? At the moment, I think because I am loading the Helper and also the Library in the constructor, the error messages are displayed for all existing forms. Thanks again for the URL. I will be studying this in detail tonight.
Where you load the library/helper doesn't determine where the error messages are displayed. The error messages are displayed wherever you happen to call the function(s) to display them.
If you use the validation_errors() function, it's going to spit out whatever errors are there. So, in this case, you either want to display it in a prominent location on the page which isn't necessarily associated with a specific form, or not use the function at all. Then you use the form_error() function to display the errors on a per-field basis within the form(s), but this will require that the names of the fields in the forms be unique (which isn't a requirement, otherwise). If you pass a variable back to the page to indicate which form was processed, you could check that before calling validation_errors() on each form (and the form_error() function if you duplicate your field names) to prevent the errors from being displayed on the wrong form(s). I also use the form_error() function to set an error class on my fields so I can use CSS to draw attention to them, like this: PHP Code: <div class='control-group<?= form_error("username") ? " {$errorClass}" : ""; ?>'> (07-28-2016, 12:10 PM)Petersk Wrote: Now, if I do this, do you think it would be a better idea to load the form validation library locally in the function to validate - for instance, the subscribe() or the dologin()? If the form validation fails the best experience for your user is to just show the form they are trying to fill out with the error message next to the relevant field so its as clear as possible. |
Welcome Guest, Not a member yet? Register Sign In |