I have several questions regarding CI application design.
Q. When creating a new form and your using CI’s form_helper I’m creating arrays in the controller and passing it to the view/form_input() method. Should I be doing this in the controller, the view, or a separate file?
Q. In my controller, I create a method for my form i.e., new_user() and in my view/form_open() I specify a different method in my controller to handle the action (i.e., add(), edit(), delete() ..etc) & that method handles the validation. This is the way that I perfer; however, I’ve had a lot of difficulty passing the data around if the validation fails. Any suggestions?
Q. I have an instance or two that when I perform form validation I need to validate against two $_POST variables. An example would be, on validation I need to query the database to determine if the entered business already exists (based off business name and zip code) then redirect back to the view and persist the post variables. So far I haven’t been able to find a way to create a custom callback function to do this because you can only pass in one parameter. The only way that I’ve been able to get this to work is if validation passes, I then perform the database check and if the business exists I put the $_post in session/flashdata and use redirect to load the view again. The array that defines the form_input attributes calls set_value for that is where it pulls the flashdata for each record in the array.
$data['name'] = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name', $this->session->flashdata('name')),
'maxlength' => '200',
'size' => '79',
'class' => 'text'
I realize that this really comes down to preferences; however, I’m really wanting to gain some insight on what pitfalls I can expect and how others are designing their applications. I’ve downloaded sample applications and I’ve dome a good amount of searching but I really haven’t found much discussion. Any suggestions are greatly appreciated.
Thank you!
I’ll share my approach using CI
URI,_GET, and_POST. Then controller will pass required parameter to models, and get the result. After that, view file will be loaded and all variables required by the view will be passed.The only ‘fat’ processing in Controller is the form_validation. I have answer it in your other question, how I wrote my form_validation rules and how to use it.
Below is my answers for your question above:
I rarely using form_helper. This is because most of my view is came from fellow designer or provided by client as the HTML file. I only use
form_dropdownbecause it’s allow me to pass options as an array, instead of doforeach. For the other form element, I just use the one presented in the template file.When I create my application, I often only have 2 main methods in controller.
adminis for displaying list and handle delete, andformto display and handle add and edit. Let me give example with a product module.I will have product controller with these methods:
Using this approach, I can avoid duplicate code and can maintain all code to always up to date. Almost all add & edit have same view and form field. In case add & edit form differ (such as edit user, do not allow changing username), by have
$modevariable set to eitheraddoredit, I can put simpleifand display correct form, validation rules, and call appropriate model metods.You can create your own validation rules. To pass more than one parameter, you can open the file
system/libraries/Form_validation.phpthen see thefunction matches($str, $field)code. Your callback can have more than 1 parameter, andfunction matches($str, $field)code will show you how to read and use the second parameter.I hope this will help you in learning and using CI. Waiting great web application from you 😉