jigniter™

jQuery & CodeIgniter – Perfect combination for web application

Simple Form Generation in CodeIgniter

7 comments

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.

No related posts.

Posted by admin

2009-08-02 at 6:30 pm

7 Responses to 'Simple Form Generation in CodeIgniter'

Subscribe to comments with RSS or TrackBack to 'Simple Form Generation in CodeIgniter'.

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

  2. Very nice! Clean and simple… I like.

    Kyle Farris

    5 Aug 09 at 7:37 am

  3. 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

    Frank

    14 Aug 09 at 4:32 am

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

    admin

    14 Aug 09 at 7:28 am

  5. Very nice library! Thank you Frank! CI rocks.

    Benoa

    5 Sep 09 at 4:21 am

  6. 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 ;)

    Benoa

    5 Sep 09 at 4:22 am

  7. 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

    Frank

    12 Sep 09 at 3:15 am

Leave a Reply