[eluser]alexmiller[/eluser]
Hi
I've never really been comfortable working with forms on CI, so I wrote this extended helper model over the last few months to simplify the process.
I'd really welcome any comments, suggestions and feedback because I don't know any other coders and no-one has ever looked at my code before (I am Google-taught).
The usage is exactly the same as for the core form helper, except functions are called as $f->open() instead of form_open().
Features
- Automatically uses Code Igniter's set_value function within all form elements (including checkboxes, dropdowns and radios) to show submitted data
- Form can be pre-populated from a database by using $f->populate($data_array);
- Elements auto-echo by default eg.
Code:
// NO NEED TO DO THIS
echo form_input('name');
// JUST DO THIS
$f->input('name');
// CAN DISABLE AUTO-ECHOING EASILY
$f->auto_echo = FALSE;
- CSS Classes are added to all elements by default (unless overridden.) This makes it easy to style a form. Calling $f->input('name') will produce a field with its CSS class set as
textfield.
- All form elements have an ID set the same as their name by default (unless an ID is supplied)
- Opening a form includes a hidden field,
_submit_check, with its value set to the name of the form. This makes checking for submission easy.
Installation
Unzip downloaded file and put MY_Form_Helper.php into your application/helpers directory. Load form helper as normal ($this->load->helper('form');
Usage example
Code:
<?php
$f = new form(); // initialise the class
$f->set_defaults( array ('name'=>'Example name','gender'=>'F')); // set the forms default values
$f->open('test','form_name'); // create a form opening tag (uses form_open)
?>
<div>
<? $f->label('Name','name'); ?>
<? $f->input('name'); ?>
<br />
<? $f->label('Gender','gender'); ?>
<? $f->radio('gender','M'); ?> Male
<? $f->radio('gender','F'); ?> Female
<br />
<? $f->submit('submit','submit'); ?>
<? $f->close();
</div>
One of the most timesaving things for me is that I can reuse forms for editing and adding. For example:
add_person.php (view)
Code:
<?php
$f = new form();
$f->open('people/add','add_person');
$f->include_form('people/form'); // form.php is saved in the view/people directory
$f->submit('add','Add');
?>
edit_person.php (view)
Code:
<?php
/* $person_data and $person_id are set in the controller */
$f = new form();
$f->open('people/edit','edit_person');
$f->populate($person_data);
$f->hidden('person_id',$person_id);
$f->include_form('people/form'); // form.php is saved in the view/people directory
$f->submit('edit','Edit');
?>
form.php (view)
Code:
<fieldset>
<legend>Personal details</legend>
<table>
<tr>
<th><? $f->label('Title','title'); ?></th>
<td><? $f->input(array('name' => 'title','size'=>5)); ?></td>
</tr>
<tr>
<th><? $f->label('First name','first_name'); ?></th>
<td><? $f->input('first_name'); ?></td>
</tr>
<tr>
<th><? $f->label('Surname','surname'); ?></th>
<td><? $f->input('surname'); ?></td>
</tr>
<tr>
<th><? $f->label('Suffix','suffix'); ?></th>
<td><? $f->input('suffix'); ?></td>
</tr>
</table>
Let's say you add a 'town' field to the database - you just need to add an input field for 'town' to the form.php view and it will all just work.
I didn't want to write up too much documentation in case this is useless to everyone but me. If people think it worthwhile I will add more documentation.