PHP Form Builder Class

This project promotes rapid development of HTML forms through an object-oriented PHP framework.

Download PFBC 3.1

What's New in 3.x

Unlike the 2.x branch, version 3.x doesn't represent a complete rewrite from its previous version. In fact, most of the PHP code for creating and validating forms has remained unchanged in this new major version release. So, what's different?

The most significant enhancement is the integration with Bootstrap - a front-end framework from Twitter. Bootstrap incorporates responsive CSS, which means your forms not only look and behave great in the latest desktop browser, but in tablet and smartphone browsers as well. To see responsive CSS in action, resize your browser window and watch how the login form in the Getting Started section responds.

Another enhancement in version 3.x is the addition of 13 HTML5 elements, which you can check out in our HTML5 example. HTML5 form elements and attributes improve your form's usability - especially on tablets and smartphones where data is entered with virtual keyboards.

Here are a few more differences between PFBC 3.x and 2.x to be aware of if you're planning on upgrading.

  • In version 3.x, the Form class constructor no longer includes the width parameter.
  • PFBC 3.x contains new view classes - SideBySide, Vertical, Inline, and Search. The views used in version 2.x are no longer supported.
  • The description element property that existed in PFBC 2.x has been replaced by two properties in 3.x - shortDesc and longDesc. ShortDesc is displayed inline while longDesc is displayed block.
  • jQuery UI js/css files are no longer included by default. The js/css files will only be added when an elements that make use of jQuery UI (jQueryUIDate, Checksort, and Sort) is added to the form.
  • The Date element in PFBC 2.x has been renamed to jQueryUIDate. The new Date element in version 3.x is HTML5's input type="date".
  • The Email element now uses input type="email" as apposed to type="text".
  • The required Element property now sets the HTML5 required attribute triggering client-side validation when supported by the web browser.
  • Validation errors have been restyled using Bootstrap specific classes.

Getting Started

Before writing any code, you'll first need to download the latest version of PFBC, extract the contents of the zip file, and upload the PFBC directory within the document root of your web server. The other files/directories outside of the PFBC folder (like this one) that are included in the download are provided only for instruction and can be omitted from your production environment.

Important This project maintains two separate code bases - one for PHP 5 and another for PHP 5 >= 5.3.0. The primary difference is the use of namespaces in PFBC 3.1 (PHP 5 >= 5.3.0). Namespaces weren't introduced in PHP until version 5.3.0, so if you're web server is running an older version of PHP 5, then you will need to use PFBC 3.1 (PHP 5).

Once the PFBC directory is up on your web server, you're ready to create your first form. Below you'll find a sample login form that we'll talk through in detail.

<?php
session_start();

use PFBC\Form;
use PFBC\Element;

include("PFBC/Form.php");
$form = new Form("login");
$form->addElement(new Element\HTML('<legend>Login</legend>'));
$form->addElement(new Element\Hidden("form", "login"));
$form->addElement(new Element\Email("Email Address:", "Email", array(
    "required" => 1
)));
$form->addElement(new Element\Password("Password:", "Password", array(
    "required" => 1
)));
$form->addElement(new Element\Checkbox("", "Remember", array(
    "1" => "Remember me"
)));
$form->addElement(new Element\Button("Login"));
$form->addElement(new Element\Button("Cancel", "button", array(
    "onclick" => "history.go(-1);"
)));
$form->render();
<?php
session_start();

include("PFBC/Form.php");
$form = new Form("login");
$form->addElement(new Element_HTML('<legend>Login</legend>'));
$form->addElement(new Element_Hidden("form", "login"));
$form->addElement(new Element_Email("Email Address:", "Email", array(
    "required" => 1
)));
$form->addElement(new Element_Password("Password:", "Password", array(
    "required" => 1
)));
$form->addElement(new Element_Checkbox("", "Remember", array(
    "1" => "Remember me"
)));
$form->addElement(new Element_Button("Login"));
$form->addElement(new Element_Button("Cancel", "button", array(
    "onclick" => "history.go(-1);"
)));
$form->render();
Login

Line 2: PFBC uses PHP sessions in the validation process, so you'll need to ensure you have session_start(); in your webpage - before outputting anything to the browser. If you forget, you'll be reminded by an error message displayed above your form.

Lines 4-5: As previously mentioned, PFBC 3.1 (PHP 5 >= 5.3.0) makes use of namespaces. The use operator creates aliases for the various namespaces we're going to use within our form. This way, we don't have to include the PFBC\ prefix each time we reference one of the project's classes.

Line 7: In order to create an instance of the Form class, you'll need to first include PFBC/Form.php. Any other files (elements, views, etc) will be autoloaded on-demand.

Line 8: In the Form class' constructor, a unique identifier is supplied - login in this sample. You'll reference this same identifier in the validation process.

Lines 9-23: Fields are added to the form with the addElement method.

Line 24: The render method outputs the form's HTML, CSS, and javascript to the web browser.

Now that you've had a crash course in PFBC, go check out the example files that are included in the navigation above. They demonstrate how the validation process works, what form elements are supported, and much more.