PHP DOM to Implement a Form Class with Pure HTML Templates

So, I had this painful gout attack today and was mostly bedridden. It sucked. But I got some reading done, which was nice. My mind wandered and came up with this idea (and a headache). I was looking for a beginner’s framework for PHP – and really found none except P5, which I didn’t find relevant. So I figured I might as well mine my old piles of code to create one. I was digging around for some Forms classes for PHP, discovered I had none, and also found the ones online kind of annoying, because I just don’t like the Builder pattern to create code. I started out with Little Languages, and learned YACC and later Bison, and sometimes prefer languages to function calls. That’s probably why I prefer to use plain SQL with prepared statements, ala PHP’s PDO, over the different SQL builders in the numerous frameworks.

The Forms classes usually have an HTML builder class. Even worse, in the past, some libraries like Perl’s used functions, and they had to be nested, like this: html(body(h1(“foo”),p(“bar”))); is awesome, but I never got into that forms manager because of that Lisp-y HTML builder. I know that closing parens is easy, especially in emacs and vi, but I just never got the hang of using those HTML functions.

So I dug around and found the PHP DOM implementation. With DOM, you can write your form in HTML, and then manipulate it with PHP.

In the example below, I wrote my HTML form as a string, and then turned it into a DOMDocument object. Then, I manipulated it, adding an error message. If there was user data present, it would modify the form. Then it would save the form as text and display it.

This is a sloppy example, but it shows off the first steps toward making a form manager class that uses regular HTML forms as its templates.

$f = '
<form name="foorm" id="foorm">
  <label for="foo">foo:</label><input id="foo" type="text" name="foo" value="bar" /><span class="error" id="fooerror"></span><br />
  <label for="bar">bar</label><input id="bar" name="bar" type="text" value="bar" /><span class="error" id="barerror"></span><br />
  <input type="submit" />

$d = new DOMDocument();
$e = $d->getElementById('fooerror');
$e->appendChild(new DOMText('error message'));

if ($_GET['foo']) { 
  $e = $d->getElementById('foo');
if ($_GET['bar']) { 
  $e = $d->getElementById('bar');
echo $d->saveHTML();



My main justification for using HTML code instead of PHP code to build the form template is: HTML code is best suited for creating HTML code. It’s tautological 🙂 Also, with builder functions like those referenced below, it’s harder to mix form tags with other tags, like spacers, tables, CSS code, and other tweaks that make the forms more attractive. If you use HTML code, you can also add Javascript widgets to make data entry easier. The PHP part of the program will only manipulate the form tags, and won’t alter the rest of the code.

Speaking of Javascript, it’s also possible to use the new HTML5 data-* attribute to specify validation rules. These rules could be specified one time, in this form’s template, and it could be used in both the client and the server to validate the data.


CodeIgniter Form helper
CakePHP Form Helper
Zend Form
Symfony 1 forms
Symfony 2 forms
Fat Free Framework Forms

Totally related – someone else figured this out first: DOM Templating!

Somewhat related: SQL DOM, a way to edit SQL statements programmatically.