tag.
---
elements:
- type: Text
name: user
- type: Block
id: foo
- type: Text
name: email
=head1 DISPLAY
=head2 Custom error messages
If you want to display an error message due to an error in your own code,
such as a database check; something which isn't implemented as a
L
or
L; you can use a
L.
If you don't provide your own callback routine, the default callback will
always pass, regardless of user input.
You can take advantage of this by setting
L, to display its error message
when needed.
Example config:
---
elements:
- type: Text
- name: email
- constraints:
type: Callback
message: 'Email address already in use'
Example usage:
if ( $@ =~ m/duplicate entry for key 'email'/i ) {
$form->get_field('email')
->get_constraint({ type => 'Callback' })
->force_errors(1);
$form->process;
# then redisplay the form as normal
}
=head2 Highlight required fields (or fields with certain types of constraint)
This can be achieved using the form's C method:
$form->auto_constraint_class( 'constraint_%t' );
The container divs around any form field with a constraint will then have extra
CSS classes added, which indicate the type of constraint and allow you to apply
appropriate styling with CSS:
/* change background of labels for fields with a Required constraint */
fieldset .constraint_required label {
background: #f00;
}
This technique can also be used to add content before or after the fields in
question (note this will not work in older browsers with more limited CSS
support such as IE6):
/* add an asterisk at the end of the label for required fields */
fieldset .constraint_required label:after {
content: '*'
}
=head2 Add a popup hint to a field
Some visual browsers (including IE6/7, Firefox, Opera 9) display a tooltip when
a user hovers their mouse pointer over an HTML element with a "title" tag. Aural
browsers may try to turn the content into speech. You can take advantage of this
behaviour to provide a hint to the user about how to complete a form field.
elements:
- type: Text
name: country_name
label: Country Name
attributes:
title: Name of country
The above will provide a hint when the "country_name" field receives focus.
Or you could provide the hint for the container tag around both field and label:
elements:
- type: Text
name: country_name
label: Country Name
container_attributes:
title: Name of country
=head2 Display filtered values
If you have a Filter on a field, such as L
to strip leading / trailing whitespace, then if you redisplay the form the
field is normally populated with the value the user originally entered.
If you would like the field to contain the filtered value, use
L.
=head2 Multiple forms using Catalyst::Controller::HTML::FormFu
Sometimes you need to display multiple forms on a single page. If you
try to use FormConfig on several actions in a chain, or similar, they
all use C<< $c->stash->{form} >> to store the form, hence you only get the last
form.
One way to work around such problems is to do a little of the work yourself:
In this example we have a login_form that we want on every page
# root/forms/login.yml:
---
indicator: username
elements:
-
type: Text
name: username
constraints:
- Required
...
We also have an edit-form
# root/forms/foo/edit.yml
---
indicator: foo
elements:
-
type: Text
name: foo
constraints:
- Required
...
In this example, we want the login form to appear on every page, so
we load this in the top-most auto action:
package MyApp::Controller::Root;
BEGIN { extends 'Catalyst::Controller::HTML::FormFu'; }
sub auto : Private {
my ($self, $c) = @_;
# We want to utilize alot of the magic that the controller
# gives us, so therefore we call $self->form like this
my $login_form = $self->form;
$login_form->load_config_file('login.yml');
# Notice how we put it into another stash var, not 'form'
$c->stash->{login_form} = $login_form;
unless ($c->user_exists) {
$login_form->process();
if ($login_form->submitted_and_valid) {
# Since we set indicator, we should only end up here if we
# have a username in the form
$c->authenticate({
username => $login_form->param_value('username'),
password => $login_form->param_value('password'),
});
}
}
}
Any other page that wants to load another form, can now do so freely:
package MyApp::Controller::Foo;
sub edit : Local FormConfig {
my ( $self, $c ) = @_;
my $form = $c->stash->{form};
if ($form->submitted_and_valid) {
# Do whatever you want with it :p
}
}
In the view we now have two stash-variables:
In F:
[% login_form %]
edit
[% form %]
=head1 ADVANCED CUSTOMISATION
=head2 Installing the TT templates
It only makes sense to use the template files if you plan on customising
them, as the default C render-method is faster.
If you're using the L web framework, install
L and run the following command:
$ script/myapp_create.pl HTML::FormFu
This will create a directory, C, containing the HTML::FormFu
template files.
If you use L as a base class and you
don't set HTML::FormFu's INCLUDE_PATH yourself, it will automatically be set
to C if that directory exists.
If you're not using L, you can create the template files by
running the following command:
$ html_formfu_deploy.pl
Take note that if you choose to customise your own copy of HTML::FormFu's
template files, you'll need to keep track of the C file, when
updating HTML::FormFu, so that you can update your own templates if the
core templates are updated.
=head1 PERFORMANCE
=head2 Catalyst::Plugin::StackTrace
If you're using L, make sure you're using at
least version C<0.09> - earlier versions had performance problems with
C.
=head2 Template::Alloy
You can also use L instead of
L, it's mostly compatible, and in many cases
provides a reasonable speed increase. You can do this either by setting the
C environment variable to a true value, or by
passing C to L:
tt_args:
TEMPLATE_ALLOY: 1
COMPILE_DIR: /tmp
COMPILE_PERL: 1
Template::Alloy's caching is off by default. Switch it on by setting either
C or C. If you're running under a persistent
environment such as modperl or fastcgi, you should also set C
to compile the cached templates down to perl code.
Of cource, if you wish you can still use L to
process your own application templates, letting L process
just the HTML::FormFu templates.
=head2 HTML:FormFu::Preload
To reduce the runtime for each form that uses a previously unused
element or processor - at the expense of greater memory usage - you
can preload all FormFu modules - this is only recommended for persistent
environments such as modperl or fastcgi:
use HTML::FormFu::Preload;
=head1 FAQs
=head2 Force an element to always have a certain value
See the following:
L,
L
=head1 AUTHORS
Will Hawes C
Carl Franks C
=head1 COPYRIGHT
This document is free, you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut