|
::Features
::Download
::Tutorial
::Examples
::Documentation ::FormBuilder Google Group ::Old List Archives ::Contribute |
|
Advanced Tutorial - Step 10: Custom Messages The last topic we'll cover is custom messaging. Most of the time, the built-in FormBuilder messages are sufficient. However, you may be writing a multi-lingual application, or something else that requires precise control over messages.
FormBuilder's messaging system is very easy to understand. Each message
is given a unique tag by FormBuilder. For example, the message that is
used in a JavaScript alert about an invalid select list is called
Each message you want to customize is specified to the
#!/usr/bin/perl
use CGI::FormBuilder;
$form = CGI::FormBuilder->new(
fields => [qw/name email phone/],
messages => {
js_invalid_start => 'There were %s errors found:',
js_invalid_text => '- The "%s" field was invalid',
js_invalid_end => 'For more help, click the "Help" button',
form_invalid_text => 'This field is missing or invalid',
}
);
print $form->render(header => 1);
Alternatively, you can put these specifications in a separate file,
one per line, separated by a space between them:
# # messages.en - messages for English # js_invalid_start There were %s errors found: js_invalid_text - The "%s" field was invalid js_invalid_end For more help, click the "Help" button form_invalid_text This field is missing or invalid(Note: You do not use quotes or commas in this file.) Then, you simply specify the filename to your form:
#!/usr/bin/perl
use CGI::FormBuilder;
$lang = $ENV{HTTP_ACCEPT_LANGUAGE} || 'en'; # language
$form = CGI::FormBuilder->new(
fields => [qw/name email phone/],
messages => "messages.$lang", # messages file
);
print $form->render(header => 1);
FormBuilder will issue a warning if it can't open the specified
filename, and then will continue using the default messages. Any
messages not explicitly specified will remain the same as the defaults.
If you're interested in custom messages, please view the complete list of custom message tags. |
| FormBuilder is © Nate Wiger, with contributions from many people. |