Customizing HTML_QuickForm

Here’s a small tip for code hackers and PEAR lovers!


Using PEAR’s HTML_QuickForm classes you can do so much, especially within a short period of time. When I first started on this, even the term rapid development which people frequently use when they talk about PHP development, seemed to slow for this.

So for example, today I had to “script” a couple interfaces for a database for some marketing people – because I was actually getting tired of their change requests all the time. And since you can’t tell them to use MySQL Front, well this is where we met. :)

Generating forms was not the issue, my main obstacle were the looks of the forms generated by the class. Point taken, they offer hookups to all sorts of template classes, but what if you would like to customize the forms just a little? For example, by adding a reference to your CSS file. Personally I don’t see the need to throw Smarty at it.

So here is how I did. This is the class I wrapped around HTML_QuickForm.



*/
class HTML_QuickForm_LIS extends HTML_QuickForm {

function HTML_QuickForm_LIS($formName)
{
if(empty($formName))
{
$formName = ‘foobar’;
}
$this->HTML_QuickForm($formName);
}

/**
* lis_setTemplate
*
* @access public
* @param string $header Template for element header
* @param string $element Template for standard element
*/
function lis_setTemplate($header=”, $element=”)
{
$renderer = $this->defaultrenderer();
if(!empty($header))
{
$renderer->_headerTemplate = $header;
}
if(!empty($element))
{
$renderer->_elementTemplate = $element;
}
$GLOBALS[‘_HTML_QuickForm_default_renderer’] = $renderer;
}
}

And here are some example templates and a small HOWTO:


$header_tpl = (string) ”;
$header_tpl.= ‘

{header}

‘.”\n”;

$element_tpl = (string) ”;
$element_tpl.= ‘

‘.”\n”;
$element_tpl.= ‘

‘;
$element_tpl.= ‘*{label}

‘.”\n”;
$element_tpl.= ‘

{error}
‘;
$element_tpl.= ‘ {element}

‘.”\n”;
$element_tpl.= ‘

‘.”\n”;

$qf = new HTML_QuickForm_LIS(‘myform’);
$qf->lis_setTemplate($header_tpl, $element_tpl);

I know the code is not too pretty and hackish. Still after all the copy and pasting I hope it works and in case you got hooked on the class you need to check the official documentation of HTML_Quickform and the mighty rest.

Tags: ,

2 Responses

  • Johan

    Thanks for this post. I just started fooling around with this class and I was looking for something similar.

    How would you add a “summary” attribute to the “table” – tag QuickForm generates?

  • Not sure I ever heard of a “summary” attribute. What is it used for?