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

  • SEARCH
  • Home
  • 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 6751589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:56:43+00:00 2026-05-26T12:56:43+00:00

It is incredibly common to create a New or Edit form in CodeIgniter (or

  • 0

It is incredibly common to create a “New” or “Edit” form in CodeIgniter (or any framework for that matter).

Without resorting to automated scaffolding, what is the quickest / easiest way to create these forms in CodeIgniter with the least amount of typing/fuss/etc.

The ideal solution should handle many elements, validate itself before submitting, should not clear the form if a mistake is made, and still be readable by the developer. It would not rely on a “generator” script.

  • 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-26T12:56:43+00:00Added an answer on May 26, 2026 at 12:56 pm

    I wrote a set of code which currently lives in MY_Controller which uses the $config that you write for form validation to generate a form.

    Along the way it creates the form, adds * to the labels etc

    I’ll dig it out and post it on Monday.

    I’d prefer it to be a library but my knowledge of CI is still growing…

    ADDED MY MY_controller.php details:

    I have the following in my MY_Controller.php file

    function generate_form($config, $legend = 'Details', $wraptag = 'div', $buttontext = 'update')
    {
    
        $fields = array();
        $files  = 0;
    
        if($config)
        {
    
    
    
            foreach($config as $c)
            {
    
                if(strpos($c['rules'],'required') === FALSE) {
                    $label = $c['label'];               
                }
                else
                {
                    $label = $c['label'].' <span class="required">*</span>';
                }               
    
                $data = array(
                    'name'        => $c['field'],
                    'id'          => $c['field'],
                    'value'       => set_value($c['field'], $c['value']),
                    'class'       => $c['field'],
    
                );
    
                $fields[] = '<'.$wraptag.'>'."\r\n";
    
                $func = 'form_'.$c['input_type'];
    
                switch ($c['input_type'])
                {
    
                    case 'displayonly':
    
                        $fields[] = form_label($label, $c['field'])."\r\n";
                        $fields[] = '<input type="text" value="'.$c['value'].'" disabled="disabled"/>';
    
                        break;
    
    
                    case 'dropdown':
                    case 'multiselect':
    
                        $fields[] = form_label($label, $c['field'])."\r\n";
                        $fields[] = $func($c['field'], $c['options'], $c['value']);
    
                        break;
    
                    case 'datepicker':
    
                        $fields[] = form_label($label, $c['field'])."\r\n";
                        $fields[] = $func($c['field'])."\r\n";
    
                        break;
                    case 'timezone':
    
                        $fields[] = form_label($label, $c['field'])."\r\n";
                        $fields[] = timezone_menu($c['value'],$c['field'], $c['field']);
                        break;
    
                    case 'upload':
    
                        $fields[] = form_label($label, $c['field'])."\r\n";
                        $fields[] = $func($data)."\r\n";
                        $files = 1;
                        break;
    
                    default:
    
                        $fields[] = form_label($label, $c['field'])."\r\n";
                        $fields[] = $func($data)."\r\n";
    
                        break;
    
    
                }
    
                $fields[] = '</'.$wraptag.'>'."\r\n";
    
    
            }
    
            $fields[] = '<'.$wraptag.'>'."\r\n";
            $fields[] = form_submit('btnSubmit', 'Update');
            $fields[] = '</'.$wraptag.'>'."\r\n";
    
    
            $form_start[]   = validation_errors('<div class="error">', '</div><!--class="error"-->');
            if($files)
            {   
                $form_start[]   = form_open_multipart(uri_string());
            }
            else
            {
                $form_start[]   = form_open(uri_string());
            }
    
            $form_start[]   = '<fieldset>';
            $form_start[]   = '<legend>'.$legend.'</legend>';
    
    
            $form_end[] = '</fieldset>'."\r\n";
            $form_end[] = form_close();     
    
        }
    
        if($wraptag == 'li')
        {
            $fields_start   = '<ul>';
            $fields_end     = '</ul>';
        }
        else
        {
            $fields_start   = '';
            $fields_end     = '';
        }       
        return (implode('',$form_start).$fields_start.implode('',$fields).$fields_end.implode('',$form_end)); 
    
    }
    
    function create_validation_from_config($config)
    {
    
        foreach($config as $c)
        {
    
            if($c['rules'] != '')
            {
    
                if($c['input_type'] == 'datepicker')
                {
                    $validation[] = array(
                        'field' => $c['field'].'_day',
                        'label' => $c['label'],
                        'rules' => $c['rules'],         
                    );
                    $validation[] = array(
                        'field' => $c['field'].'_month',
                        'label' => $c['label'],
                        'rules' => $c['rules'],         
                    );
                    $validation[] = array(
                        'field' => $c['field'].'_year',
                        'label' => $c['label'],
                        'rules' => $c['rules'],         
                    );
                }
                else
                {
                    $validation[] = array(
                        'field' => $c['field'],
                        'label' => $c['label'],
                        'rules' => $c['rules'],         
                    );
                }
            }
    
        }
    
        return $validation;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that this problem is incredibly common and I have read through quite
Are there any languages that support easily extending the compiler to support new literals
Well this is incredibly frustrating. After being nagged by Rails that I need to
This is an incredibly simple question (I'm new to Python). I basically want a
We've got an incredibly frustrating situation with a CF Web Services-based API that we
I must admit that I am incredibly jealous of those developers who happen to
I have an incredibly simple query (table type InnoDb) and EXPLAIN says that MySQL
I'm incredibly new to JQuery and Javascript in general. I'm using the Infinite Scroll
I have a form that redirects to the same page after a user enters
I'm incredibly new to Rails and programming in general. Built my first, fairly static,

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.