HTML Driven Forms - phForm

First things first phForm is NOT a form component/widget library!  There are no form components like TextInput, Button, etc, the content and look of a form is all HTML driven.  It's easier to explain with a simple example:

Firstly you write your view:

/myproject/view/loginFormView.php

<dl>
<dt><label for="<?=$this->id('username')?>">Username:</label></dt>
<dd>
<input id="<?=$this->id('username')?>" name="<?=$this->name('username')?> type="text" value="" />
</dd> 
<dt><label for="<?=$this->id('password')?>">Password:</label></dt>
<dd>
<input id="<?=$this->id('password')?>" name="<?=$this->name('password')?> type="password" value="" />
</dd> 
</dl> 

As you can see this is standard HTML with a couple of helper calls: 

  • The 'id' helper is keeps id's unique and makes the element accessible by PHP code.
  • The 'name' helper keeps posted form data unique

Then using the phForm class you can access elements in this form like so

<?php
$form = new phForm ('login', '/myproject/view/loginFormView.php');
$form->username->setValue('admin'); // set an initial value
$form->username->setValidator(new phRequiredValidator('Username must be filled in')); // a form validator that says the username is a required element
echo $form; // echo's out the html with values set 

So basically that is it, this library provides API access to forms that every web developer should be familiar with.  Doing it this way also means that front end specialists/web designers can make alterations without needing more in depth PHP knowledge.

The code is not quite ready for release yet but you can take a look at the latest code here on Github