TracNav menu
patForms Parser
The patForms_Parser allows you to embedd XML-tags that represent a form and its elements inside HTML files. The parser will then extract the form definition from this file and create a new renderer which contains the plain HTML document and placeholders for the element. This way a designer is able to create a form, add some simple validation rules and has full control over the layout of the resulting page and the styles of the elements.
The developer only needs to parse the file, instead of using the factory methods and then can continue working with the form as if he had created it using PHP.
Basic form template
A form template can be any HTML or other text-based file that includes form elements in the form namespace patForms:
<patForms:Form name="myform">
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td>Username*</td>
<td>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><patForms:String name="username" required="yes" description="Please enter your name here" size="8" maxlength="8" accesskey="U" /></td>
<td> Password* </td>
<td><patForms:String name="password" type="password" required="yes" size="6" maxlength="6" accesskey="P" /></td>
<td> Age* </td>
<td><patForms:Number name="age" required="yes" size="2" max="22" min="18" default="" accesskey="A" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Comments</td>
<td colspan="7"><patForms:Text name="comments" required="no" cols="68" rows="3" accesskey="C" /></td>
</tr>
</table>
</patForms:Form>
Parsing the template
The patForms parser is now able to extract all form elements from this template and build the complete form object for you. Furthermore, it will create a new template that can be used to insert the rendered HTML tags in the locations where you placed the patForms tags. The easiest way to do this is using the static methods provided by the patForms_Parser class:
patForms_Parser::setNamespace( "patForms" );
patForms_Parser::setCacheDir( "cache" );
// this method creates a complete patForms object in just one call,
// by setting the renderer to use, the source form template file,
// and the optional target template file.
$form =& patForms_Parser::createFormFromTemplate(
'SimpleRenderer',
'templates/example_parser_simple.fhtml',
'templates/example_parser_simple.html'
);
These three lines will not only create the patForms object with all elements, but also set up a renderer? for you. In this case, a simple renderer implementation based on PHP's string functions will be used.
The form object now contains four elements:
- A String element to enter the username
- A String element to enter the password
- A Number element to enter your age
- A Text element to enter any comment
The returned form may be used like any form object, you created manually. That means, you can use the getElementByName() method to access the individual elements and attach observers?, rules, filters or datasources. You can also call any other method, that patForms provides on this object.
If you want to display it to the user, you only need to call the render() method and echo the return value of this method:
$content = $form->renderForm(); echo $content;
Using the SimpleRenderer you will need to handle the display of error messages all by yourself. However, there is also a renderer based on patTemplate available. The distribution includes an example that shows how to use the patForms_Parser with the SimpleRenderer.
