cgForm class explained

Long, long ago I became frustrated with the clunkiness of the average online form. Historically most form processors handled sanity checking by showing a status page with errors and forcing the user to hit "back" in their browser if they wanted to make any changes (often losing their data in the process). More user friendly forms often relied on inline PHP to do their magic creating a difficult to maintain and non-reusable set of legacy code.

I wanted something different, a form class I could re-use that would enforce data consistency while providing a user friendly experience. Thus the cgForm class was born!

The main features of this form class are tableless layout, built in sanity checking, user friendly error reporting and re-usability.

Example Form

This form class uses an array to represent each element in the form. Here is the instantiated form object used to create this page you are reading now:

// this code is normally buried in a class method // so that you can do things like this: // $document->install_document_form($tpl,'document_form'): $f=new cgForm; $f->submit=($this->id>0 ? array('edit') : array('add')); array_push($f->submit,'cancel'); $f->hidden['go']=1; $f->hidden['action']='document'; $f->hidden['sub']='edit'; $f->hidden['cgDocument_id']=$this->id; // set up base form variables based on default class attributes $f->vars=parent::gen_base_form_vars(array('role'=>1,'title'=>1,'ident'=>1,'data'=>1)); // customize form variables as needed $f->vars['cgDocument_data']['format_opts']=array('cols'=>60,'rows'=>16); $f->vars['cgDocument_status']=array( 'type'=>'input_radio', 'type_opts'=>array(0,1), 'type_opts_title'=>array('disabled','active'), 'default'=>(string)$this->status, 'title'=>'status', 'required'=>1, 'type_out'=>'select_list' ); $f->vars['cgDocument_role']=array( 'type'=>'select_list', 'type_opts'=>array('inline','file'), 'default'=>$this->role, 'title'=>'role', 'required'=>1, 'type_out'=>'select_list' );

The above form the is rendered via a method call like this:

$f->render_form();

The resulting rendered form looks like this:


example of rendered cgForm object

CODEgrunt consulting

Experienced PHP web developer.

$35 an hour, no job too small.

There is no replacement for integrity and experience.

Contact me for more information.

calendar

February 2012

Sun
Mon
Tue
Wed
Thu
Fri
Sat
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3