After the interesting
Christian's and
Tobias' interesting talk at LinuxTag about the advantages and disadvantages of ASP.NET in comparison to PHP, Christian and I had a short chat about patForms and the parser we are using for rapid development.
IN ASP.NET, webcontrols allow you to add an additional attribute runat="server" to your HTML elements, which makes them available as variables in your script. This is extremely interesting when used with forms, as validation may be easily added and the elements automatically keep their values after submitting the form. Tobias and Christian thought, this was a huge advantage of ASP but then mentioned
HTML_QuickForm. But using Quickform forces you to build your form in an object-oriented way by creating instances, setting properties and then combining all elements to a form wich you may then serialize to HTML. This is a boring and tedious task.
I told Christian about
patForms_Parser, which is able to extract special tags from any HTML page and build a patForms object from it, which can be validated and then sent back to the user with about
7 lines of PHP code, independent of your form.
But still, you need to create a HTML page, that contains special tags and that your designer is not able to view in his favourite HTML editor, which definitely is a drawback when it comes to rapidly creating form based applications.
Last week I wrote a very simple subclass of the parser, which is able to read plain HTML forms, and give you a form object back that you may modify, validate and then send it back to the browser. All you need is the following code:
$parser = &patForms_Parser::createParser( 'Html' );
$parser->parseFile( 'templates/any_standard_html.html' );
$parser->writeFormTemplate( 'templates/example_html.fhtml' );
$form = &patForms_Parser::createFormFromTemplate( 'SimpleRenderer', 'templates/example_html.fhtml', null );
if( isset( $_GET["save"] ) ) {
$form->setSubmitted( true );
$form->validateForm();
}
echo $form->renderForm();
?>
You may use this code with any HTML page that contains a form and you will get an form element object for all elements that you added to the HTML page. The steps where a form template is written, can basically be left out, these just create the HTML pages with the special <patForms:Foo/> tags.
To include special validation rules you may add attributes like required="true", format="email", etc. to your standard HTML elements. Of course you may easily modify the form from PHP, as patForms provides a DOM-like API. To add a new custom validation rule to an element, you just had to get the element and modify it:
$name = &$form->getElementById( 'name' );
$enum = &patForms::createRule( 'Enum' );
$enum->setValues( array( 'the', 'only', 'allowed', 'values' ) );
$name->addRule( $enum, PATFORMS_RULE_AFTER_VALIDATION );
?>
You may also use all other features of patForms, like filtering user-input (to avoid XSS attacks), observers which allow to modify elements on certain events (e.g. changing the CSS class on error), javascript validation or storage container, which automically write the user input to a datasource or file.
You may also specify a cache file which enables you to speed up your form-based applications.
The
code and a
simple example is available in
CVS or can be downloaded as a
snapshot.