Simple Form Generation in CodeIgniter

If you are using CodeIgniter, as well as any other PHP framework, you may notice that building CRUD forms is one of the most bother and routine tasks. Probably 80% of general web applications uses CRUD (create/read/update/delete).

In CodeIgniter, you can use any of the form generation libraries.

One of my preferred libraries is Form Generation Library by Frank Michel (see @macigniter’s thread here). It allows you to create clean XHTML forms with CodeIgniter. See demo here.

Using Form Generation Library

So, in order to use this library, you need to download it from here (library files only is enough if you don’t want the entire site structure).

Make sure to follow the installation steps, as shown here. That means unzipping the package, configuring config/form.php and moving config/form.php and libraries/form.php to the right directories.

Let’s assume we want to create a simple form for my Time tracking utility.

timetracker

This simple form has:

  • Project name text input
  • Description text input
  • Notes text area
  • Submit button

So, I created a Project controller class with an edit() function, and added the following method:


	function edit()
	{
		$this->load->library('form');

		$this->form->open('project/edit', 'project_edit_form')
					->fieldset('Project')
					->text('name', 'Project Name', 'max_length[40]')
					->text('description', 'Description', 'max_length[40]')
					->textarea('notes', 'Notes', 'trim', "Write your project notes here")
					->indent(150)
					->submit('Submit', 'sub')
					->onsuccess('redirect', 'project/index')
					->nobr();

		$this->data['form'] = $this->form->get();
		$this->data['errors'] = $this->form->errors;

		$this->load->view('project/edit', $this->data );
	}

Then, we need to create the view. For that purpose, I created the edit.php file under application/view/project folder.

<?php
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');

$this->load->view('header', $data);
?>

<h2>Project</h2>

<?php echo $form ?>

<?php
$this->load->view('footer', $data);
?>

The important code line there is only <?php echo $form ?> since it displays the form we previously generated. However, the other pieces of code display a generic header and footer part, you can discard that for your tests.

just need to open your browser, for example at: http://localhost/project/edit to see the results.

By using this Form Generation library you can create advanced forms, too. For example you can create forms that use checkboxes, radio buttons, multiple lists, etc. I’d recommend to download the library including CI and user guide, from here and look for views/welcome.php example to see an advanced version.

Related searches:

  • codeigniter crud generator
  • codeigniter form library
  • codeigniter form
  • codeigniter 2 crud
  • crud codeigniter 2 0
  • codeigniter form generation library
  • CRUD codeigniter 2
  • codeigniter form generation
  • crud codeigniter download
  • CODEIGNITER CRUD
  1. Kyle Farris says:

    Very nice! Clean and simple… I like.

  2. Frank says:

    Cool. A tutorial about my library :) By the way… the ->method(‘post’) isn’t really needed, since that is set as the default method.

    Oh and by the way, the download link has changed to:
    http://www.frankmichel.com/download/get/2

  3. admin says:

    Hi Frank,
    Thanks for the update. I have replaced the download link to the new one, and removed the method(‘post’) as you suggested.

  4. Benoa says:

    Very nice library! Thank you Frank! CI rocks.

  5. Benoa says:

    Hey, by the way, Frank, when I go to your website, it gives me a 404 when I try to reach the blog. It probably is because it detects my language from IP adress (France), so beware ;)

  6. Frank says:

    Hey Benoa, you just found a bug in my i18n library. Congratulations!! ;-) Thanks for pointing that out. I am glad you enjoy the form generation library. I just published a quick reference guide. Make sure you grab one at http://www.frankmichel.com/download/get/4

  7. EricZ says:

    I get the following error when using radios and checkboxes with this library:

    Message: in_array() [function.in-array]: Wrong datatype for second argument

    Filename: libraries/Form.php

    Line Number: 1737

    Strange thing: The only radio or check value that will not error out is a zero. The demo that came with the download does the same. Anyone else encounter something like this?

  8. tarun says:

    Nice one

  9. dhanesh mane says:

    cool and simple way of coding the forms for CI.
    how to integrate validations in this?

  10. Thor says:

    Such a great library. Really, an integrated form generator-validator should be built into the CodeIgniter core. It’s much more common than many of the other often useless libraries built into the core.

  1. [...] Simple Form Generation in CodeIgniter [...]

  2. [...] Simple form CodeIgniter is an article that we posted time ago. In CodeIgniter 2.0 there are some slightly changes. For references, there are many open source applications in CodeIgniter and blog’s posts with examples how to use the forms and validations in CodeIgniter 2.0. [...]

Leave a Reply