PHP XML Form Builder – Validation

Update: The XML form builder is now live on the site, it can been seen here.

The first version of my XML form builder consisted of two classes, one for building the form and another for handling the validation rules, I have been thinking recently about combining them into one class, I’m still undecided, for now I will continue with two seperate classes.

The idea is that the builder builds the form and the process class runs the validation rules, it logs the errors and then hands them back to the form builder, the form builder can then display the rules, either inline or at the top of the form and then if required the styling can be altered for the inputs with errors.

Below is an example of how I envisage the validation system working, lines 12 through to 18 are currently fictional and only how I intend it to work, hopefully, it will be a reality some time tomorrow.

// Create new form
$formBuilder = new lib_form_builder_xml('test', $this->domain . $this->controller, $this->action);

// Build up form, as a method, not in constructor so set_* functions can be run

// Instantiate validator
$validator = new lib_form_process_xml('test');

if($formBuilder->is_posted() == TRUE) {

    if($validator->validate() == TRUE) {
        // Process submission
        ## Insertion code here or additional validation
    }  else {


// Return the completed form
$html = $formBuilder->get_form();

PHP XML Form Builder – Params

Update: The XML form builder is now live on the site, it can been seen here.

The decorator for the text form object has now been completed, the forms are drawn using divs, below is an outline of how a single form element is structured, as you can see there are three divs, each of which can have an independent class, style and id.

Form Element Structure

The updated XML structure for the text type is in the image below.

XML Structure
XML Structure

Although there are several elements for the text field, not all are mandatory, the builder informs you if any required information is missing.

In addition to being able to control every property in the XML file, you can also override the settings at runtime using any of the many public setting methods on the form builder. The following can all be overridden at runtime if required, element value, id, disabled setting, readonly setting, class and style, for each container div you can override the id, class and style.

The override methods are generally used to preset values (edit pages) and highlight elements after failed validation, I figured though that why limit it now, there was no harm in adding overrides for everything.

The original form builder required two XML files per form, one for form structure and another for the validation, the two will be combined in this version of the builder, once I have added all the types to the form builder I will start the validation system, as per params, the validation rules will be able to be set in the XML file and at runtime.

PHP XML Form Builder – XML Structure

Update: The XML form builder is now live on the site, it can been seen here.

Over the last couple of days I’ve been working on the decorator methods in the XML form builder, each element of a form is build up using three divs, the container and a separate div for both the label and element, over the last year this method has been able to handle any form style I’ve come across.

Below is the XML structure for a text field.

XML Structure
XML Structure

As you can see the structure is very simple, the params are quite simply the attributes of the text input and the class and style elements let you control both the class and style attributes assigned to each div.

The XML builder class validates the XML file being read, if an element is missing an exception will be thrown telling you which element you forgot to add.

Framework Release and License

I’ve been thinking recently about what exactly I am going to release, I have to release enough to be worthwhile to other developers but also I don’t want to hurt my future developments, of which one is quite major.

I have decided that I will release the full library and the base controllers and models, this will take the form of a very simple template site with user authentication, user management, page management and a few other default actions.

I mentioned in the previous post that I am creating three form builders, array, XML and MySQL. The array and XML form builder are part of the core library so they will be released with the framework, the MySQL form builder (controllers and models) will however not be released, at least initially. Obviously, because it will be built using the XML and array form builders from the library it should be relatively trivial for a competent developer to recreate.

With regards the license I will go with the least prohibitive license I can find.

PHP XML Form Builder

Whilst at Web on High I created an XML form builder to allow very quick creation of forms. The new framework will feature a brand new, much more feature complete XML form builder as well as an array form builder and a MySQL builder, the MySQL builder will include a full interface.

I’m creating the XML form builder first because I will use that to create the MySQL builder, the goal is to make creating forms incredibly simple, below is an example of the minimum code required to create a form with full validation, error highlighting and support for any form element, I’ve included the phpDoc comment from the constructor.

At the moment the builder only handles the text and textarea input, I’ll be adding support for the rest over the coming weeks.

* Constructor
* Set up form properties
* @param string $xml XML file that contains form structure
* @param string $controller Controller for form
* @param string $action Action for form
* @param string $params Additional $_POST params
* @param boolean $post Submitted using $_POST
* @param boolean $multipart Multi-part form
$formBuilder = new lib_form_builder_xml('test', 'formbuilder', 'addform');
return $formBuilder->get_html();