lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PEAR: QuickForm
PEAR documentation

I have a form system that works pretty well. Once I refactored it, it would be quite cogent. But if I'm going to master PEAR, I figure I better learn QuickForm. And it should help me refactor my form system.

Setting up QuickForm is not too complicated -- it's rendering it that gets messy. More on that later.

Basic routine for using QuickForm

# set form field default values

# instantiate class (create form)

# set defaults

# add elements

# add submit button

# add rules

# add filters

# validate and freeze


Example

# set form field default values
$QFORM['defaults'] = array
(
'textfield' => 'default value',
'textarea' => 'default text',
);

# instantiate class
$OBJ['QFORM'] = new HTML_QuickForm('form_name', 'post');

# set defaults
$OBJ['QFORM']->setDefaults(QFORM['defaults']);

# add elements
$OBJ['QFORM']->addElement('text', 'textfield', 'Textfield Label');
$OBJ['QFORM']->addElement('textarea', 'textarea', 'Textarea Label');

# add submit button
$OBJ['QFORM']->addElement('submit', 'submit_post', 'post label');

# add rules
$OBJ['QFORM']->addRule('text', 'rule failed prompt', 'required');

# add filters
$OBJ['QFORM']->addFilter('text', 'trim');

# validate the form
if ($OBJ['QFORM']->validate())
{
$OBJ['QFORM']->freeze();
}


That's the gist. Now how to display?