Welcome Guest, Not a member yet? Register   Sign In
Extended Form Helper - Automatically sets values, classes, and IDs
#1

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

&lt;? $f->label('Name','name'); ?&gt;
&lt;? $f->input('name'); ?&gt;
<br />

&lt;? $f->label('Gender','gender'); ?&gt;
&lt;? $f->radio('gender','M'); ?&gt; Male
&lt;? $f->radio('gender','F'); ?&gt; Female
<br />

&lt;? $f->submit('submit','submit'); ?&gt;

&lt;? $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:
&lt;?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');
?&gt;

edit_person.php (view)
Code:
&lt;?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');
?&gt;

form.php (view)
Code:
<fieldset>
<legend>Personal details</legend>
<table>
<tr>
    <th>&lt;? $f->label('Title','title'); ?&gt;</th>
    <td>&lt;? $f->input(array('name' => 'title','size'=>5)); ?&gt;</td>
</tr>
<tr>
    <th>&lt;? $f->label('First name','first_name'); ?&gt;</th>
    <td>&lt;? $f->input('first_name'); ?&gt;</td>
</tr>
<tr>
    <th>&lt;? $f->label('Surname','surname'); ?&gt;</th>
    <td>&lt;? $f->input('surname'); ?&gt;</td>
</tr>
<tr>
    <th>&lt;? $f->label('Suffix','suffix'); ?&gt;</th>
    <td>&lt;? $f->input('suffix'); ?&gt;</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.
#2

[eluser]deanf7[/eluser]
Does it override CI's default form helper or could they be used side by side?
#3

[eluser]alexmiller[/eluser]
Absolutely, both can be used side-by-side. The core functions are unchanged and fully accessible.
#4

[eluser]goliatone[/eluser]
Hi there,
it looks interesting, i will check it out!
tnxs for sharing!
#5

[eluser]Dam1an[/eluser]
Shoul this not really be a library? helper are isolated functions, here, you're creating an object and callings its methods




Theme © iAndrew 2016 - Forum software by © MyBB