FormBuilder

Form­Builder is a PHP class that sim­pli­fies the process of cre­ation and val­i­da­tion of HTML forms. It can also out­put the results in a text form and email them.

Form­Builder in essence man­ages a col­lec­tion of field classes that imple­ment a stan­dard inter­face defined by the Form­Field class. Classes cur­rently imple­mented include text fields, radio but­tons, check­boxes, popup menus, date/time, etc.

The gen­er­ated form can be fully cus­tomized through CSS.

The cur­rent ver­sion of Form­Builder is already very usable, but there are some improve­ments in plan­ning: Bet­ter val­i­da­tion, more types of Fields and data­base integration.

Exam­ple

The best way to explain how Form­Builder works is to go through a sim­ple exam­ple — a guest book:

// The first step is to include the FormBuilder class

require_once('FormBuilder.php');

// Then FormBuilder needs to be initialized

$form = new FormBuilder();

// insert some fields into the form

$form->addField(new TextField('Name:', 'name', 
                              25, 2, 30));
$form->addField(new TextAreaField('Text:', 'textArea',
                                  1, 100, 5, 40));
$form->addField(new SubmitField('submit', 'Send'));

// Next, check if the form has already been submitted,
// or if this is the first time the form is displayed

if ($form->isSubmitted('submit'))
{
    // Form has been submitted, so validate it.
    $form->validate();

    if ($form->isValid())
    {
        // Form is valid, display a success message
        echo 'Thank you for submitting the form!';

        // now the form content can be sent by email,
        // displayed on screen, or stored in a database.
    }
    else
    {
        // Form is invalid, show an error message and 
        // display the form again

        echo 'The form is invalid, please correct the'.
             ' highlighted fields, and submit again';

        $form->display();
    }
}
else
{
    // First time the page is loaded, display the form
    $form->display();
}