Form module

During my recent contract I have spent quite a lot of time working with my XML Form Builder, I’ve made a few tiny modifications to the code but nothing too drastic, most of the changes I have made has been to the processor.

There have always been a few checks that I have done outside the XML file validation, for example, checking to see that a new field will be unique in a database table, (adding a new user, email address), checking to see if a value exists in a table and file validation rules to name a few.

Over the last six weeks I’ve added support for these to the processor and have noticed that very few forms need custom validation that the processor can’t handle. I started wondering if it was worth grouping creation and validation of a form together to reduce code.

Currently, the code for a reasonably complex form created with the form builder might  look like the below. This example form contains a text field, text area, password field, select menu, date input, checkboxes, a hidden field and radio buttons. The text field and the date input also have validation but so could any other field. (I’m planning on adding a page explaining all the validation rule and how to create additional rules shortly)

// Form passed validation
$bComplete = FALSE;

// Instantiate xml form builder
$formBuilder = new lib_form_builder_xml('test', $this->controller, $this->action, 'add');

// Add element data
$formBuilder->add_select_data_array('select_1', array(0 => 'No', 1 => 'Yes'));
$formBuilder->add_select_data_array('radio_buttons', array('one_1'=>'Check option 1', 'two'=>'Check option 2'));
$formBuilder->add_select_data_array('check_boxes', array('cycling'=>'Cycling', 'rowing'=>'Rowing', 'climbing'=>'Climbing'));

// Validation
if($formBuilder->is_posted('save') == TRUE) {

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

    if($validator->validate() == TRUE) {

        // Get data and pass to model
        $dataArray = $formBuilder->get_post_array();

        // Set complete
        $bComplete = TRUE;

        // Remove submit button
    } else {
        // Add validation errors to form

// Build form

// Confirm message
if($bComplete == TRUE) {
    $html .= "Form passed validation";

// Add form to content
$html .= $formBuilder->get_form();

// Add content and H1 to template

Well, I’ve created a form module class that does exactly this, it groups everything together to make it even simpler to create an entire form.

The new code using the form module now looks like the below.

$html = '';

// Instantiate form module
$formModule = new lib_form_module_simple('test', $this->controller, $this->action, 'add');

// Add element data
$formModule->add_data_array('select_1', array(0 => 'No', 1 => 'Yes'));
$formModule->add_data_array('radio_buttons', array('one'=>'Check option 1', 'two'=>'Check option 2'));
$formModule->add_data_array('check_boxes', array('cycling'=>'Cycling', 'rowing'=>'Rowing', 'climbing'=>'Climbing'));

// Remove submit button after validation complete

// Form
$formModule->build_form('save', $this->settings['Forms']['Error_Class']);

// Form validated
if($formModule->validated() == TRUE) {
    // Add data processing here (add to database etc)
    // Add confirm messages here
    $html .= "Thank you for posting";

// Add to html
$html .= $formModule->get_html();


I’ve plenty of testing left to do before I even think about releasing the module class, obviously my builder has been released so it should be very simple to duplicate what I have done.

Busy month

You may have noticed that I haven’t released any code over the last month, this has mainly been because of my current contract, it is keeping me very busy, which I like.

Another reason for the quietness is that I have been working on the core of the framework. I wasn’t completely happy with either my widgets system or the general split between core and custom code so decided to do some structuring, took a little longer than expected.

The original plan was to release the template site in early May, I’m going to have to move the release to nearer the end of May, I just don’t have enough time to get everything ready.

I should however have updates to the XML Form Builder (file uploading) and additional tutorials on the site some time over the next two weeks.

I’ll keep you posted if anything changes.