|
HTML::Formulate - module for producing/rendering HTML forms |
HTML::Formulate - module for producing/rendering HTML forms
# Simple employee create form
$f = HTML::Formulate->new({
fields => [ qw(firstname surname email position) ],
required => [ qw(firstname surname) ],
});
print $f->render;
outputs:
<form method="post"> <table cellpadding="2"> <tr><th style="color:blue"><span class="required">Firstname</span></th> <td><input name="firstname" type="text" /></td></tr> <tr><th style="color:blue"><span class="required">Surname</span></th> <td><input name="surname" type="text" /></td></tr> <tr><th>Email</th><td><input name="email" type="text" /></td></tr> <tr><th>Position</th><td><input name="position" type="text" /></td></tr> <tr><td align="center" colspan="2"> <input name="submit" id="submit" type="submit" value="Submit" /> </td></tr> </table> </form>
# Simple employee edit form
$f = HTML::Formulate->new({
fields => [ qw(emp_id firstname surname email position) ],
required => [ qw(firstname surname) ],
field_attr => {
emp_id => { type => 'hidden' },
},
});
print $f->render(\%data);
outputs the same form but with an additional 'hidden' emp_id input field, and data values from the %data hash in the relevant input field values.
HTML::Formulate is a module used to produce HTML forms. It uses a presentation definition hash to control the output format, which is great for flexible programmatic control, inheritance, and subclassing (e.g. defining site- or section-specific HTML::Formulate subclasses and then producing standardised forms very easily). On the other hand, it doesn't give you the very fine-grained control over presentation that you get using a template-based system.
HTML::Formulate handles only form presentation - it doesn't include any validation or processing functionality (although it does include functionality for displaying validation errors). If you're after the processing end of things, check out CGI::FormFactory, which uses HTML::Formulate and Data::FormValidator to manage the full HTML form lifecycle. CGI::FormBuilder is another good alternative.
HTML::Formulate also allows form definitions to be built in multiple stages, so that you can define a base form with common definitions (either on the fly or as a dedicated subclass) and then provide only the details that are particular to your new form.
HTML::Formulate is a subclass of HTML::Tabulate, and uses HTML tables to lay out its forms. It supports all the standard HTML::Tabulate presentation definition arguments - see HTML::Tabulate for details. Probably the following are the most important:
In addition, HTML::Formulate supports the following form-specific definition arguments:
type="submit" name="search" id="search" value="Search"
input elements. To change attributes, use a named field_attr section (see FIELD ATTRIBUTE ARGUMENTS below) or the special field_attr '-submit' section (which applies to all submit elements). Default:
submit => [ 'submit' ]
To omit submit elements altogether, use:
submit => [] # or submit => 0
Required fields are marked as such, usually on the field label. By default, required field labels are rendered as:
<th style="color:blue"><span class="required">Label</span></th>
This colours required labels blue, by default, but can be overridden by defining a CSS 'required' class. This default itself can be overridden by defining per-field attributes (typically 'th' and 'label_format') for the '-required' pseudo-field (see '-required' below).
In a similar way to 'required' fields, error field labels are by default rendered as:
<th style="color:red"><span class="error_field">Label</span></th>
This colours error labels red, but can be overridden by defining a CSS 'error_field' class. This default itself can be overridden by defining per-field attributes (typically 'th' and 'label_format') for the '-errors' pseudo-field (see '-errors' below).
Error messages, if defined, are displayed as a list before the form (errors_where => 'top') or in a third table column annotating each field (errors_where => 'column'). See 'errors_where' following.
errors_format => '<span class="error">%s</span><br />'
Default is a subref that renders messages like this:
<p style="color:red;font-weight:bold"> <span class="error">Error 1</span> <span class="error">Error 2</span> </p>
producing red bold error messages, which can be overridden by defining a CSS 'error' class.
Per-field attributes can be defined in a 'field_attr' hashref (see HTML::Tabulate for the details). In addition to the standard HTML::Tabulate attributes (and the '-defaults' pseudo-field), HTML::Formulate defines some extra attributes and a set of extra pseudo-fields, as follows.
text textarea password radio select checkbox hidden display static omit
Of these display, static, and omit do not have obvious HTML correlates - these mean:
emp_name => { type => 'display' }
is rendered as the following line:
<tr><th>Emp Name</th><td>Fred Flintstone</td></tr>
emp_name => { type => 'static' }
is rendered as the following line:
<tr><th>Emp Name</th> <td>Fred Flintstone <input name="emp_name" type="hidden" value="Fred Flintstone" /> </td></tr>
(some formatting newlines added). If you want to use a different label than the underlying data value, you can set a (scalar) vlabel, similar to selects. The vlabel is interpreted as a sprintf pattern passed the current data value i.e. $label = sprintf($vlabel,$value). For example:
emp_id => { type => 'static', value => '123', vlabel => 'E%05d' }
is rendered as:
<tr><th>Emp ID</th> <td>E00123<input name="emp_id" type="hidden" value="123" /> </td></tr>
(newlines added).
$f = HTML::Formulate->new({
fields => [ qw(username password) ],
required => 'ALL',
submit => [ qw(login) ],
field_attr => {
password => { type => 'password' },
},
});
$f = HTML::Formulate->new({
fields => [ qw(firstname surname email password password_confirm) ],
required => 'ALL',
submit => [ qw(register) ],
field_attr => {
qr/^password/ => { type => 'password' },
},
});
HTML::Tabulate, CGI::FormFactory, CGI::FormBuilder
Gavin Carr, <gavin@openfusion.com.au>
Copyright 2003-2004, Open Fusion Pty Ltd. All Rights Reserved.
This program is free software. You may copy or redistribute it under the same terms as perl itself.
|
HTML::Formulate - module for producing/rendering HTML forms |