Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:04:16+00:00 2026-05-13T14:04:16+00:00

I have several questions regarding CI application design. Q. When creating a new form

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:04:16+00:00Added an answer on May 13, 2026 at 2:04 pm

    I’ll share my approach using CI

    1. I create Controller as slim as possible. The controller main job will only get parameter through 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.
    2. All process logic, related with database, email sending, etc, is handled in the model. Model will get parameter, do query, process query result if needed, then return an array, resultset, boolean, or integer. Controller that get the returned value pass it directly to view, without reprocessing it.
    3. In view, it will process the variable in order to displaying it. There will be loop to display list of data, get the column field from array then display it as a form default value, etc. View and model often developed in pair, because all needed field in view must be provided by the query in the Model.

    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:

    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?

    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_dropdown because it’s allow me to pass options as an array, instead of do foreach. For the other form element, I just use the one presented in the template 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?

    When I create my application, I often only have 2 main methods in controller. admin is for displaying list and handle delete, and form to display and handle add and edit. Let me give example with a product module.

    I will have product controller with these methods:

    class Product extends MY_Controller {
    
      function index()
      {
        //for front page, display list of product
      }
    
      function detail()
      {
        //for front page, display single product detail
        //product id is passed as 3rd URI segment
        $id = intval($this->uri->rsegment(3));
      }
    
      function admin()
      {
        //for admin, display product list
        //receive id in _POST then do delete
        //after delete, do redirect to self, best practise
      }
    
      function form()
      {
        //for admin, handle add and edit
        $id = intval($this->uri->rsegment(3));
        //if id given and product detail data can be loaded, then it in 'edit' mode
        //else it in 'add' mode
        //after validation success, and insert/update success, redirect to product/admin
      }
    
    }
    

    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 $mode variable set to either add or edit, I can put simple if and display correct form, validation rules, and call appropriate model metods.

    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.

    You can create your own validation rules. To pass more than one parameter, you can open the file system/libraries/Form_validation.php then see the function matches($str, $field) code. Your callback can have more than 1 parameter, and function 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 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 294k
  • Answers 294k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a design pattern: The Provider model. There is some… May 13, 2026 at 6:38 pm
  • Editorial Team
    Editorial Team added an answer [Disclaimer: I work at Typemock] You have three alternatives: Create… May 13, 2026 at 6:38 pm
  • Editorial Team
    Editorial Team added an answer Try using the Tidy library which can be used to… May 13, 2026 at 6:38 pm

Related Questions

In C#, I am reading insert SQL statements from a text file and then
I have several questions regarding where to handle nulls. Let me set up a
I have been studying SOAP and WSDL in preparation for implementing a web service.
(Note: I have seen several questions regarding .NET logging frameworks, but haven't seen any
I have just discovered the joy of bitflags. I have several questions related to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.