\& User Information
\& Please fill out the following information:
\&
\& Your full name:
\& Your email address:
\& Choose a password:
\& Please confirm it:
\& Your home zipcode:
\&
\&
\&
.Ve
.PP
Then, all we need to do add the \f(CW\*(C`template\*(C'\fR option, and the rest of
the code stays the same:
.PP
.Vb 9
\& my $form = CGI::FormBuilder\->new(
\& fields => \e@fields,
\& header => 1,
\& validate => {
\& name => \*(AqNAME\*(Aq,
\& email => \*(AqEMAIL\*(Aq
\& },
\& template => \*(Aquserinfo.tmpl\*(Aq
\& );
.Ve
.PP
So, our complete code thus far looks like this:
.PP
.Vb 1
\& use CGI::FormBuilder;
\&
\& my @fields = qw(name email password confirm_password zipcode);
\&
\& my $form = CGI::FormBuilder\->new(
\& fields => \e@fields,
\& header => 1,
\& validate => {
\& name => \*(AqNAME\*(Aq,
\& email => \*(AqEMAIL\*(Aq
\& },
\& template => \*(Aquserinfo.tmpl\*(Aq,
\& );
\&
\& if ($form\->submitted && $form\->validate) {
\& # form was good, let\*(Aqs update database
\& my $fields = $form\->field;
\&
\& # update database (you write this part)
\& do_data_update($fields);
\&
\& # print confirmation screen
\& print $form\->confirm;
\&
\& } else {
\& # print the form for them to fill out
\& print $form\->render;
\& }
.Ve
.PP
You may be surprised to learn that for many applications, the
above is probably all you'll need. Just fill in the parts that
affect what you want to do (like the database code), and you're
on your way.
.PP
\&\fBNote:\fR If you are confused at all by the backslashes you see
in front of some data pieces above, such as \f(CW\*(C`\e@fields\*(C'\fR, skip down
to the brief section entitled "REFERENCES" at the bottom of this
document (it's short).
.SH METHODS
.IX Header "METHODS"
This documentation is very extensive, but can be a bit dizzying due
to the enormous number of options that let you tweak just about anything.
As such, I recommend that you stop and visit:
.PP
.Vb 1
\& www.formbuilder.org
.Ve
.PP
And click on "Tutorials" and "Examples". Then, use the following section
as a reference later on.
.SS \fBnew()\fP
.IX Subsection "new()"
This method creates a new \f(CW$form\fR object, which you then use to generate
and process your form. In the very shortest version, you can just specify
a list of fields for your form:
.PP
.Vb 3
\& my $form = CGI::FormBuilder\->new(
\& fields => [qw(first_name birthday favorite_car)]
\& );
.Ve
.PP
As of 3.02:
.PP
.Vb 3
\& my $form = CGI::FormBuilder\->new(
\& source => \*(Aqmyform.conf\*(Aq # form and field options
\& );
.Ve
.PP
For details on the external file format, see CGI::FormBuilder::Source::File.
.PP
Any of the options below, in addition to being specified to \f(CWnew()\fR, can
also be manipulated directly with a method of the same name. For example,
to change the \f(CW\*(C`header\*(C'\fR and \f(CW\*(C`stylesheet\*(C'\fR options, either of these works:
.PP
.Vb 6
\& # Way 1
\& my $form = CGI::FormBuilder\->new(
\& fields => \e@fields,
\& header => 1,
\& stylesheet => \*(Aq/path/to/style.css\*(Aq,
\& );
\&
\& # Way 2
\& my $form = CGI::FormBuilder\->new(
\& fields => \e@fields
\& );
\& $form\->header(1);
\& $form\->stylesheet(\*(Aq/path/to/style.css\*(Aq);
.Ve
.PP
The second form is useful if you want to wrap certain options in
conditionals:
.PP
.Vb 7
\& if ($have_template) {
\& $form\->header(0);
\& $form\->template(\*(Aqtemplate.tmpl\*(Aq);
\& } else {
\& $form\->header(1);
\& $form\->stylesheet(\*(Aq/path/to/style.css\*(Aq);
\& }
.Ve
.PP
The following is a description of each option, in alphabetical order:
.ie n .IP "action => $script" 4
.el .IP "action => \f(CW$script\fR" 4
.IX Item "action => $script"
What script to point the form to. Defaults to itself, which is
the recommended setting.
.IP "body => \e%attr" 4
.IX Item "body => %attr"
This takes a hashref of attributes that will be stuck in the
\&\f(CW\*(C`\*(C'\fR tag verbatim (for example, bgcolor, alink, etc).
See the \f(CW\*(C`fieldattr\*(C'\fR tag for more details, and also the
\&\f(CW\*(C`template\*(C'\fR option.
.IP charset 4
.IX Item "charset"
This forcibly overrides the charset. Better handled by loading
an appropriate \f(CW\*(C`messages\*(C'\fR module, which will set this for you.
See CGI::FormBuilder::Messages for more details.
.IP "debug => 0 | 1 | 2 | 3" 4
.IX Item "debug => 0 | 1 | 2 | 3"
If set to 1, the module spits copious debugging info to STDERR.
If set to 2, it spits out even more gunk. 3 is too much. Defaults to 0.
.IP "fields => \e@array | \e%hash" 4
.IX Item "fields => @array | %hash"
As shown above, the \f(CW\*(C`fields\*(C'\fR option takes an arrayref of fields to use
in the form. The fields will be printed out in the same order they are
specified. This option is needed if you expect your form to have any fields,
and is \fIthe\fR central option to FormBuilder.
.Sp
You can also specify a hashref of key/value pairs. The advantage is
you can then bypass the \f(CW\*(C`values\*(C'\fR option. However, the big disadvantage
is you cannot control the order of the fields. This is ok if you're
using a template, but in real-life it turns out that passing a hashref
to \f(CW\*(C`fields\*(C'\fR is not very useful.
.IP "fieldtype => 'type'" 4
.IX Item "fieldtype => 'type'"
This can be used to set the default type for all fields in the form.
You can then override it on a per-field basis using the \f(CWfield()\fR method.
.IP "fieldattr => \e%attr" 4
.IX Item "fieldattr => %attr"
This option allows you to specify \fIany\fR HTML attribute and have it be
the default for all fields. This used to be good for stylesheets, but
now that there is a \f(CW\*(C`stylesheet\*(C'\fR option, this is fairly useless.
.IP "fieldsets => \e@attr" 4
.IX Item "fieldsets => @attr"
This allows you to define fieldsets for your form. Fieldsets are used
to group fields together. Fields are rendered in order, inside the
fieldset they belong to. If a field does not have a fieldset, it
is appended to the end of the form.
.Sp
To use fieldsets, specify an arrayref of \f(CW\*(C`\*(C'\fR names:
.Sp
.Vb 1
\& fieldsets => [qw(account preferences contacts)]
.Ve
.Sp
You can get a different \f(CW\*(C`\*(C'\fR tag if you specify a nested arrayref:
.Sp
.Vb 5
\& fieldsets => [
\& [ account => \*(AqAccount Information\*(Aq ],
\& [ preferences => \*(AqWebsite Preferences\*(Aq ],
\& [ contacts => \*(AqEmail and Phone Numbers\*(Aq ],
\& ]
.Ve
.Sp
If you're using the source file, that looks like this:
.Sp
.Vb 1
\& fieldsets: account=Account Information,preferences=...
.Ve
.Sp
Then, for each field, specify which fieldset it belongs to:
.Sp
.Vb 5
\& $form\->field(name => \*(Aqfirst_name\*(Aq, fieldset => \*(Aqaccount\*(Aq);
\& $form\->field(name => \*(Aqlast_name\*(Aq, fieldset => \*(Aqaccount\*(Aq);
\& $form\->field(name => \*(Aqemail_me\*(Aq, fieldset => \*(Aqpreferences\*(Aq);
\& $form\->field(name => \*(Aqhome_phone\*(Aq, fieldset => \*(Aqcontacts\*(Aq);
\& $form\->field(name => \*(Aqwork_phone\*(Aq, fieldset => \*(Aqcontacts\*(Aq);
.Ve
.Sp
You can also automatically create a new \f(CW\*(C`fieldset\*(C'\fR on the fly by
specifying a new one:
.Sp
.Vb 1
\& $form\->field(name => \*(Aqremember_me\*(Aq, fieldset => \*(Aqadvanced\*(Aq);
.Ve
.Sp
To set the \f(CW\*(C`\*(C'\fR in this case, you have two options.
First, you can just choose a more readable \f(CW\*(C`fieldset\*(C'\fR name:
.Sp
.Vb 2
\& $form\->field(name => \*(Aqremember_me\*(Aq,
\& fieldset => \*(AqAdvanced\*(Aq);
.Ve
.Sp
Or, you can change the name using the \f(CW\*(C`fieldset\*(C'\fR accessor:
.Sp
.Vb 1
\& $form\->fieldset(advanced => \*(AqAdvanced Options\*(Aq);
.Ve
.Sp
Note that fieldsets without fields are silently ignored, so you can
also just specify a huge list of possible fieldsets to \f(CWnew()\fR, and
then only add fields as you need them.
.IP "fieldsubs => 0 | 1" 4
.IX Item "fieldsubs => 0 | 1"
This allows autoloading of field names so you can directly access
them as:
.Sp
.Vb 1
\& $form\->$fieldname(opt => \*(Aqval\*(Aq);
.Ve
.Sp
Instead of:
.Sp
.Vb 1
\& $form\->field(name => $fieldname, opt => \*(Aqval\*(Aq);
.Ve
.Sp
Warning: If present, it will hide any attributes of the same name.
For example, if you define "name" field, you won't be able to
change your form's name dynamically. Also, you cannot use this
format to create new fields. Use with caution.
.ie n .IP "font => $font | \e%attr" 4
.el .IP "font => \f(CW$font\fR | \e%attr" 4
.IX Item "font => $font | %attr"
The font face to use for the form. This is output as a series of
\&\f(CW\*(C`\*(C'\fR tags for old browser compatibility, and will
properly nest them in all of the table elements. If you specify
a hashref instead of just a font name, then each key/value pair
will be taken as part of the \f(CW\*(C`\*(C'\fR tag:
.Sp
.Vb 1
\& font => {face => \*(Aqverdana\*(Aq, size => \*(Aq\-1\*(Aq, color => \*(Aqgray\*(Aq}
.Ve
.Sp
The above becomes:
.Sp
.Vb 1
\&
.Ve
.Sp
I used to use this all the time, but the \f(CW\*(C`stylesheet\*(C'\fR option
is \fBSO MUCH BETTER\fR. Trust me, take a day and learn the basics
of CSS, it's totally worth it.
.IP "header => 0 | 1" 4
.IX Item "header => 0 | 1"
If set to 1, a valid \f(CW\*(C`Content\-type\*(C'\fR header will be printed out,
along with a whole bunch of HTML \f(CW\*(C`\*(C'\fR code, a \f(CW\*(C`\*(C'\fR
tag, and so on. This defaults to 0, since often people end up using
templates or embedding forms in other HTML.
.IP "javascript => 0 | 1" 4
.IX Item "javascript => 0 | 1"
If set to 1, JavaScript is generated in addition to HTML, the
default setting.
.IP "jserror => 'function_name'" 4
.IX Item "jserror => 'function_name'"
If specified, this will get called instead of the standard JS
\&\f(CWalert()\fR function on error. The function signature is:
.Sp
.Vb 1
\& function_name(form, invalid, alertstr, invalid_fields)
.Ve
.Sp
The function can be named anything you like. A simple one might
look like this:
.Sp
.Vb 10
\& my $form = CGI::FormBuilder\->new(
\& jserror => \*(Aqfield_errors\*(Aq,
\& jshead => <<\*(AqEOJS\*(Aq,
\&function field_errors(form, invalid, alertstr, invalid_fields) {
\& // first reset all fields
\& for (var i=0; i < form.elements.length; i++) {
\& form.elements[i].className = \*(Aqnormal_field\*(Aq;
\& }
\& // now attach a special style class to highlight the field
\& for (var i=0; i < invalid_fields.length; i++) {
\& form.elements[invalid_fields[i]].className = \*(Aqinvalid_field\*(Aq;
\& }
\& alert(alertstr);
\& return false;
\&}
\&EOJS
\& );
.Ve
.Sp
Note that it should return false to prevent form submission.
.Sp
This can be used in conjunction with \f(CW\*(C`jsfunc\*(C'\fR, which can add
additional manual validations before \f(CW\*(C`jserror\*(C'\fR is called.
.ie n .IP "jsfunc => $jscode" 4
.el .IP "jsfunc => \f(CW$jscode\fR" 4
.IX Item "jsfunc => $jscode"
This is verbatim JavaScript that will go into the \f(CW\*(C`validate\*(C'\fR
JavaScript function. It is useful for adding your own validation
code, while still getting all the automatic hooks. If something fails,
you should do two things:
.Sp
.Vb 2
\& 1. append to the JavaScript string "alertstr"
\& 2. increment the JavaScript number "invalid"
.Ve
.Sp
For example:
.Sp
.Vb 6
\& my $jsfunc = <<\*(AqEOJS\*(Aq; # note single quote (see Hint)
\& if (form.password.value == \*(Aqpassword\*(Aq) {
\& alertstr += "Moron, you can\*(Aqt use \*(Aqpassword\*(Aq for your password!\e\en";
\& invalid++;
\& }
\& EOJS
\&
\& my $form = CGI::FormBuilder\->new(... jsfunc => $jsfunc);
.Ve
.Sp
Then, this code will be automatically called when form validation
is invoked. I find this option can be incredibly useful. Most often,
I use it to bypass validation on certain submit modes. The submit
button that was clicked is \f(CW\*(C`form._submit.value\*(C'\fR:
.Sp
.Vb 9
\& my $jsfunc = <<\*(AqEOJS\*(Aq; # note single quotes (see Hint)
\& if (form._submit.value == \*(AqDelete\*(Aq) {
\& if (confirm("Really DELETE this entry?")) return true;
\& return false;
\& } else if (form._submit.value == \*(AqCancel\*(Aq) {
\& // skip validation since we\*(Aqre cancelling
\& return true;
\& }
\& EOJS
.Ve
.Sp
Hint: To prevent accidental expansion of embedding strings and escapes,
you should put your \f(CW\*(C`HERE\*(C'\fR string in single quotes, as shown above.
.ie n .IP "jshead => $jscode" 4
.el .IP "jshead => \f(CW$jscode\fR" 4
.IX Item "jshead => $jscode"
If using JavaScript, you can also specify some JavaScript code
that will be included verbatim in the section of the
document. I'm not very fond of this one, what you probably
want is the previous option.
.IP "keepextras => 0 | 1 | \e@array" 4
.IX Item "keepextras => 0 | 1 | @array"
If set to 1, then extra parameters not set in your fields declaration
will be kept as hidden fields in the form. However, you will need
to use \f(CWcgi_param()\fR, \fBNOT\fR \f(CWfield()\fR, to access the values.
.Sp
This is useful if you want to keep some extra parameters like mode or
company available but not have them be valid form fields:
.Sp
.Vb 1
\& keepextras => 1
.Ve
.Sp
That will preserve any extra params. You can also specify an arrayref,
in which case only params in that list will be preserved. For example:
.Sp
.Vb 1
\& keepextras => [qw(mode company)]
.Ve
.Sp
Will only preserve the params \f(CW\*(C`mode\*(C'\fR and \f(CW\*(C`company\*(C'\fR. Again, to access them:
.Sp
.Vb 2
\& my $mode = $form\->cgi_param(\*(Aqmode\*(Aq);
\& $form\->cgi_param(name => \*(Aqmode\*(Aq, value => \*(Aqrelogin\*(Aq);
.Ve
.Sp
See \f(CW\*(C`CGI.pm\*(C'\fR for details on \f(CWparam()\fR usage.
.IP "labels => \e%hash" 4
.IX Item "labels => %hash"
Like \f(CW\*(C`values\*(C'\fR, this is a list of key/value pairs where the keys
are the names of \f(CW\*(C`fields\*(C'\fR specified above. By default, \fBFormBuilder\fR
does some snazzy case and character conversion to create pretty labels
for you. However, if you want to explicitly name your fields, use this
option.
.Sp
For example:
.Sp
.Vb 7
\& my $form = CGI::FormBuilder\->new(
\& fields => [qw(name email)],
\& labels => {
\& name => \*(AqYour Full Name\*(Aq,
\& email => \*(AqPrimary Email Address\*(Aq
\& }
\& );
.Ve
.Sp
Usually you'll find that if you're contemplating this option what
you really want is a template.
.IP "lalign => 'left' | 'right' | 'center'" 4
.IX Item "lalign => 'left' | 'right' | 'center'"
A legacy shortcut for:
.Sp
.Vb 1
\& th => { align => \*(Aqleft\*(Aq }
.Ve
.Sp
Even better, use the \f(CW\*(C`stylesheet\*(C'\fR option and tweak the \f(CW\*(C`.fb_label\*(C'\fR
class. Either way, don't use this.
.IP lang 4
.IX Item "lang"
This forcibly overrides the lang. Better handled by loading
an appropriate \f(CW\*(C`messages\*(C'\fR module, which will set this for you.
See CGI::FormBuilder::Messages for more details.
.IP "method => 'post' | 'get'" 4
.IX Item "method => 'post' | 'get'"
The type of CGI method to use, either \f(CW\*(C`post\*(C'\fR or \f(CW\*(C`get\*(C'\fR. Defaults
to \f(CW\*(C`get\*(C'\fR if nothing is specified. Note that for forms that cause
changes on the server, such as database inserts, you should use
the \f(CW\*(C`post\*(C'\fR method.
.ie n .IP "messages => 'auto' | $file | \e%hash | $locale" 4
.el .IP "messages => 'auto' | \f(CW$file\fR | \e%hash | \f(CW$locale\fR" 4
.IX Item "messages => 'auto' | $file | %hash | $locale"
This option overrides the default \fBFormBuilder\fR messages in order to
provide multilingual locale support (or just different text for the picky ones).
For details on this option, please refer to CGI::FormBuilder::Messages.
.ie n .IP "name => $string" 4
.el .IP "name => \f(CW$string\fR" 4
.IX Item "name => $string"
This names the form. It is optional, but when used, it renames several
key variables and functions according to the name of the form. In addition,
it also adds the following \f(CW\*(C`\*(C'\fR tags to each row of the table:
.Sp
.Vb 5
\&
\& Label
\&
\& Error
\&
.Ve
.Sp
These changes allow you to (a) use multiple forms in a sequential application
and/or (b) display multiple forms inline in one document. If you're trying
to build a complex multi-form app and are having problems, try naming
your forms.
.IP "options => \e%hash" 4
.IX Item "options => %hash"
This is one of several \fImeta-options\fR that allows you to specify
stuff for multiple fields at once:
.Sp
.Vb 7
\& my $form = CGI::FormBuilder\->new(
\& fields => [qw(part_number department in_stock)],
\& options => {
\& department => [qw(hardware software)],
\& in_stock => [qw(yes no)],
\& }
\& );
.Ve
.Sp
This has the same effect as using \f(CWfield()\fR for the \f(CW\*(C`department\*(C'\fR
and \f(CW\*(C`in_stock\*(C'\fR fields to set options individually.
.ie n .IP "params => $object" 4
.el .IP "params => \f(CW$object\fR" 4
.IX Item "params => $object"
This specifies an object from which the parameters should be derived.
The object must have a \f(CWparam()\fR method which will return values
for each parameter by name. By default a CGI object will be
automatically created and used.
.Sp
However, you will want to specify this if you're using \f(CW\*(C`mod_perl\*(C'\fR:
.Sp
.Vb 2
\& use Apache::Request;
\& use CGI::FormBuilder;
\&
\& sub handler {
\& my $r = Apache::Request\->new(shift);
\& my $form = CGI::FormBuilder\->new(... params => $r);
\& print $form\->render;
\& }
.Ve
.Sp
Or, if you need to initialize a \f(CW\*(C`CGI.pm\*(C'\fR object separately and
are using a \f(CW\*(C`post\*(C'\fR form method:
.Sp
.Vb 2
\& use CGI;
\& use CGI::FormBuilder;
\&
\& my $q = new CGI;
\& my $form = CGI::FormBuilder\->new(... params => $q);
.Ve
.Sp
Usually you don't need to do this, unless you need to access other
parameters outside of \fBFormBuilder\fR's control.
.IP "required => \e@array | 'ALL' | 'NONE'" 4
.IX Item "required => @array | 'ALL' | 'NONE'"
This is a list of those values that are required to be filled in.
Those fields named must be included by the user. If the \f(CW\*(C`required\*(C'\fR
option is not specified, by default any fields named in \f(CW\*(C`validate\*(C'\fR
will be required.
.Sp
In addition, the \f(CW\*(C`required\*(C'\fR option also takes two other settings,
the strings \f(CW\*(C`ALL\*(C'\fR and \f(CW\*(C`NONE\*(C'\fR. If you specify \f(CW\*(C`ALL\*(C'\fR, then all
fields are required. If you specify \f(CW\*(C`NONE\*(C'\fR, then none of them are
\&\fIin spite of what may be set via the "validate" option\fR.
.Sp
This is useful if you have fields that are optional, but that you
want to be validated if filled in:
.Sp
.Vb 5
\& my $form = CGI::FormBuilder\->new(
\& fields => qw[/name email/],
\& validate => { email => \*(AqEMAIL\*(Aq },
\& required => \*(AqNONE\*(Aq
\& );
.Ve
.Sp
This would make the \f(CW\*(C`email\*(C'\fR field optional, but if filled in then
it would have to match the \f(CW\*(C`EMAIL\*(C'\fR pattern.
.Sp
In addition, it is \fIvery\fR important to note that if the \f(CW\*(C`required\*(C'\fR
\&\fIand\fR \f(CW\*(C`validate\*(C'\fR options are specified, then they are taken as an
intersection. That is, only those fields specified as \f(CW\*(C`required\*(C'\fR
must be filled in, and the rest are optional. For example:
.Sp
.Vb 5
\& my $form = CGI::FormBuilder\->new(
\& fields => qw[/name email/],
\& validate => { email => \*(AqEMAIL\*(Aq },
\& required => [qw(name)]
\& );
.Ve
.Sp
This would make the \f(CW\*(C`name\*(C'\fR field mandatory, but the \f(CW\*(C`email\*(C'\fR field
optional. However, if \f(CW\*(C`email\*(C'\fR is filled in, then it must match the
builtin \f(CW\*(C`EMAIL\*(C'\fR pattern.
.ie n .IP "reset => 0 | 1 | $string" 4
.el .IP "reset => 0 | 1 | \f(CW$string\fR" 4
.IX Item "reset => 0 | 1 | $string"
If set to 0, then the "Reset" button is not printed. If set to
text, then that will be printed out as the reset button. Defaults
to printing out a button that says "Reset".
.ie n .IP "selectnum => $threshold" 4
.el .IP "selectnum => \f(CW$threshold\fR" 4
.IX Item "selectnum => $threshold"
This detects how \fBFormBuilder\fR's auto-type generation works. If a
given field has options, then it will be a radio group by default.
However, if more than \f(CW\*(C`selectnum\*(C'\fR options are present, then it will
become a select list. The default is 5 or more options. For example:
.Sp
.Vb 3
\& # This will be a radio group
\& my @opt = qw(Yes No);
\& $form\->field(name => \*(Aqanswer\*(Aq, options => \e@opt);
\&
\& # However, this will be a select list
\& my @states = qw(AK CA FL NY TX);
\& $form\->field(name => \*(Aqstate\*(Aq, options => \e@states);
\&
\& # Single items are checkboxes (allows unselect)
\& $form\->field(name => \*(Aqanswer\*(Aq, options => [\*(AqYes\*(Aq]);
.Ve
.Sp
There is no threshold for checkboxes since, if you think about it,
they are really a multi-radio select group. As such, a radio group
becomes a checkbox group if the \f(CW\*(C`multiple\*(C'\fR option is specified and
the field has \fIless\fR than \f(CW\*(C`selectnum\*(C'\fR options. Got it?
.IP "smartness => 0 | 1 | 2" 4
.IX Item "smartness => 0 | 1 | 2"
By default CGI::FormBuilder tries to be pretty smart for you, like
figuring out the types of fields based on their names and number
of options. If you don't want this behavior at all, set \f(CW\*(C`smartness\*(C'\fR
to \f(CW0\fR. If you want it to be \fBreally\fR smart, like figuring
out what type of validation routines to use for you, set it to
\&\f(CW2\fR. It defaults to \f(CW1\fR.
.IP "sortopts => BUILTIN | 1 | \e&sub" 4
.IX Item "sortopts => BUILTIN | 1 | &sub"
If specified to \f(CWnew()\fR, this has the same effect as the same-named
option to \f(CWfield()\fR, only it applies to all fields.
.ie n .IP "source => $filename" 4
.el .IP "source => \f(CW$filename\fR" 4
.IX Item "source => $filename"
You can use this option to initialize \fBFormBuilder\fR from an external
configuration file. This allows you to separate your field code from
your form layout, which is pretty cool. See CGI::FormBuilder::Source::File
for details on the format of the external file.
.IP "static => 0 | 1 | 2" 4
.IX Item "static => 0 | 1 | 2"
If set to 1, then the form will be output with static hidden fields.
If set to 2, then in addition fields without values will be omitted.
Defaults to 0.
.IP "sticky => 0 | 1" 4
.IX Item "sticky => 0 | 1"
Determines whether or not form values should be sticky across
submissions. This defaults to 1, meaning values are sticky. However,
you may want to set it to 0 if you have a form which does something
like adding parts to a database. See the "EXAMPLES" section for
a good example.
.ie n .IP "submit => 0 | 1 | $string | \e@array" 4
.el .IP "submit => 0 | 1 | \f(CW$string\fR | \e@array" 4
.IX Item "submit => 0 | 1 | $string | @array"
If set to 0, then the "Submit" button is not printed. It defaults
to creating a button that says "Submit" verbatim. If given an
argument, then that argument becomes the text to show. For example:
.Sp
.Vb 1
\& print $form\->render(submit => \*(AqDo Lookup\*(Aq);
.Ve
.Sp
Would make it so the submit button says "Do Lookup" on it.
.Sp
If you pass an arrayref of multiple values, you get a key benefit.
This will create multiple submit buttons, each with a different value.
In addition, though, when submitted only the one that was clicked
will be sent across CGI via some JavaScript tricks. So this:
.Sp
.Vb 1
\& print $form\->render(submit => [\*(AqAdd A Gift\*(Aq, \*(AqNo Thank You\*(Aq]);
.Ve
.Sp
Would create two submit buttons. Clicking on either would submit the
form, but you would be able to see which one was submitted via the
\&\f(CWsubmitted()\fR function:
.Sp
.Vb 1
\& my $clicked = $form\->submitted;
.Ve
.Sp
So if the user clicked "Add A Gift" then that is what would end up
in the variable \f(CW$clicked\fR above. This allows nice conditionality:
.Sp
.Vb 5
\& if ($form\->submitted eq \*(AqAdd A Gift\*(Aq) {
\& # show the gift selection screen
\& } elsif ($form\->submitted eq \*(AqNo Thank You\*(Aq)
\& # just process the form
\& }
.Ve
.Sp
See the "EXAMPLES" section for more details.
.ie n .IP "styleclass => $string" 4
.el .IP "styleclass => \f(CW$string\fR" 4
.IX Item "styleclass => $string"
The string to use as the \f(CW\*(C`style\*(C'\fR name, if the following option
is enabled.
.ie n .IP "stylesheet => 0 | 1 | $path" 4
.el .IP "stylesheet => 0 | 1 | \f(CW$path\fR" 4
.IX Item "stylesheet => 0 | 1 | $path"
This option turns on stylesheets in the HTML output by \fBFormBuilder\fR.
Each element is printed with the \f(CW\*(C`class\*(C'\fR of \f(CW\*(C`styleclass\*(C'\fR ("fb"
by default). It is up to you to provide the actual style definitions.
If you provide a \f(CW$path\fR rather than just a 1/0 toggle, then that
\&\f(CW$path\fR will be included in a \f(CW\*(C`
\*(C'\fR tag as well.
.Sp
The following tags are created by this option:
.Sp
.Vb 3
\& ${styleclass} top\-level table/form class
\& ${styleclass}_required labels for fields that are required
\& ${styleclass}_invalid any fields that failed validate()
.Ve
.Sp
If you're contemplating stylesheets, the best thing is to just turn
this option on, then see what's spit out.
.Sp
See the section on "STYLESHEETS" for more details on FormBuilder
style sheets.
.IP "table => 0 | 1 | \e%tabletags" 4
.IX Item "table => 0 | 1 | %tabletags"
By default \fBFormBuilder\fR decides how to layout the form based on
the number of fields, values, etc. You can force it into a table
by specifying \f(CW1\fR, or force it out of one with \f(CW0\fR.
.Sp
If you specify a hashref instead, then these will be used to
create the \f(CW\*(C`
\*(C'\fR tag. For example, to create a table
with no cellpadding or cellspacing, use:
.Sp
.Vb 1
\& table => {cellpadding => 0, cellspacing => 0}
.Ve
.Sp
Also, you can specify options to the \f(CW\*(C`\*(C'\fR and \f(CW\*(C` \*(C'\fR
elements as well in the same fashion.
.ie n .IP "template => $filename | \e%hash | \e&sub | $object" 4
.el .IP "template => \f(CW$filename\fR | \e%hash | \e&sub | \f(CW$object\fR" 4
.IX Item "template => $filename | %hash | &sub | $object"
This points to a filename that contains an \f(CW\*(C`HTML::Template\*(C'\fR
compatible template to use to layout the HTML. You can also specify
the \f(CW\*(C`template\*(C'\fR option as a reference to a hash, allowing you to
further customize the template processing options, or use other
template engines.
.Sp
If \f(CW\*(C`template\*(C'\fR points to a sub reference, that routine is called
and its return value directly returned. If it is an object, then
that object's \f(CWrender()\fR routine is called and its value returned.
.Sp
For lots more information, please see CGI::FormBuilder::Template.
.ie n .IP "text => $text" 4
.el .IP "text => \f(CW$text\fR" 4
.IX Item "text => $text"
This is text that is included below the title but above the
actual form. Useful if you want to say something simple like
"Contact \f(CW$adm\fR for more help", but if you want lots of text
check out the \f(CW\*(C`template\*(C'\fR option above.
.ie n .IP "title => $title" 4
.el .IP "title => \f(CW$title\fR" 4
.IX Item "title => $title"
This takes a string to use as the title of the form.
.IP "values => \e%hash | \e@array" 4
.IX Item "values => %hash | @array"
The \f(CW\*(C`values\*(C'\fR option takes a hashref of key/value pairs specifying
the default values for the fields. These values will be overridden
by the values entered by the user across the CGI. The values are
used case-insensitively, making it easier to use DBI hashref records
(which are in upper or lower case depending on your database).
.Sp
This option is useful for selecting a record from a database or
hardwiring some sensible defaults, and then including them in the
form so that the user can change them if they wish. For example:
.Sp
.Vb 3
\& my $rec = $sth\->fetchrow_hashref;
\& my $form = CGI::FormBuilder\->new(fields => \e@fields,
\& values => $rec);
.Ve
.Sp
You can also pass an arrayref, in which case each value is used
sequentially for each field as specified to the \f(CW\*(C`fields\*(C'\fR option.
.ie n .IP "validate => \e%hash | $object" 4
.el .IP "validate => \e%hash | \f(CW$object\fR" 4
.IX Item "validate => %hash | $object"
This option takes either a hashref of key/value pairs or a
Data::FormValidator object.
.Sp
In the case of the hashref, each key is the
name of a field from the \f(CW\*(C`fields\*(C'\fR option, or the string \f(CW\*(C`ALL\*(C'\fR
in which case it applies to all fields. Each value is one of
the following:
.Sp
.Vb 6
\& \- a regular expression in \*(Aqquotes\*(Aq to match against
\& \- an arrayref of values, of which the field must be one
\& \- a string that corresponds to one of the builtin patterns
\& \- a string containing a literal code comparison to do
\& \- a reference to a sub to be used to validate the field
\& (the sub will receive the value to check as the first arg)
.Ve
.Sp
In addition, each of these can also be grouped together as:
.Sp
.Vb 2
\& \- a hashref containing pairings of comparisons to do for
\& the two different languages, "javascript" and "perl"
.Ve
.Sp
By default, the \f(CW\*(C`validate\*(C'\fR option also toggles each field to make
it required. However, you can use the \f(CW\*(C`required\*(C'\fR option to change
this, see it for more details.
.Sp
Let's look at a concrete example. Note that the javascript
validation is a negative match, while the perl validation
is a positive match.
.Sp
.Vb 10
\& my $form = CGI::FormBuilder\->new(
\& fields => [qw(
\& username password confirm_password
\& first_name last_name email
\& )],
\& validate => {
\& username => [qw(nate jim bob)],
\& first_name => \*(Aq/^\ew+$/\*(Aq, # note the
\& last_name => \*(Aq/^\ew+$/\*(Aq, # single quotes!
\& email => \*(AqEMAIL\*(Aq,
\& password => \e&check_password,
\& confirm_password => {
\& javascript => \*(Aq!= form.password.value\*(Aq, # neg
\& perl => \*(Aqeq $form\->field("password")\*(Aq, # pos
\& },
\& },
\& );
\&
\& # simple sub example to check the password
\& sub check_password ($) {
\& my $v = shift; # first arg is value
\& return unless $v =~ /^.{6,8}/; # 6\-8 chars
\& return if $v eq "password"; # dummy check
\& return unless passes_crack($v); # you write "passes_crack()"
\& return 1; # success
\& }
.Ve
.Sp
This would create both JavaScript and Perl routines on the fly
that would ensure:
.Sp
.Vb 6
\& \- "username" was either "nate", "jim", or "bob"
\& \- "first_name" and "last_name" both match the regex\*(Aqs specified
\& \- "email" is a valid EMAIL format
\& \- "password" passes the checks done by check_password(), meaning
\& that the sub returns true
\& \- "confirm_password" is equal to the "password" field
.Ve
.Sp
\&\fBAny regular expressions you specify must be enclosed in single quotes
because they need to be used in both JavaScript and Perl code.\fR As
such, specifying a \f(CW\*(C`qr//\*(C'\fR will NOT work.
.Sp
Note that for both the \f(CW\*(C`javascript\*(C'\fR and \f(CW\*(C`perl\*(C'\fR hashref code options,
the form will be present as the variable named \f(CW\*(C`form\*(C'\fR. For the Perl
code, you actually get a complete \f(CW$form\fR object meaning that you
have full access to all its methods (although the \f(CWfield()\fR method
is probably the only one you'll need for validation).
.Sp
In addition to taking any regular expression you'd like, the
\&\f(CW\*(C`validate\*(C'\fR option also has many builtin defaults that can
prove helpful:
.Sp
.Vb 10
\& VALUE \- is any type of non\-null value
\& WORD \- is a word (\ew+)
\& NAME \- matches [a\-zA\-Z] only
\& FNAME \- person\*(Aqs first name, like "Jim" or "Joe\-Bob"
\& LNAME \- person\*(Aqs last name, like "Smith" or "King, Jr."
\& NUM \- number, decimal or integer
\& INT \- integer
\& FLOAT \- floating\-point number
\& PHONE \- phone number in form "123\-456\-7890" or "(123) 456\-7890"
\& INTPHONE\- international phone number in form "+prefix local\-number"
\& EMAIL \- email addr in form "name@host.domain"
\& CARD \- credit card, including Amex, with or without \-\*(Aqs
\& DATE \- date in format MM/DD/YYYY
\& EUDATE \- date in format DD/MM/YYYY
\& MMYY \- date in format MM/YY or MMYY
\& MMYYYY \- date in format MM/YYYY or MMYYYY
\& CCMM \- strict checking for valid credit card 2\-digit month ([0\-9]|1[012])
\& CCYY \- valid credit card 2\-digit year
\& ZIPCODE \- US postal code in format 12345 or 12345\-6789
\& STATE \- valid two\-letter state in all uppercase
\& IPV4 \- valid IPv4 address
\& NETMASK \- valid IPv4 netmask
\& FILE \- UNIX format filename (/usr/bin)
\& WINFILE \- Windows format filename (C:\ewindows\esystem)
\& MACFILE \- MacOS format filename (folder:subfolder:subfolder)
\& HOST \- valid hostname (some\-name)
\& DOMAIN \- valid domainname (www.i\-love\-bacon.com)
\& ETHER \- valid ethernet address using either : or . as separators
.Ve
.Sp
I know some of the above are US-centric, but then again that's where I live. :\-)
So if you need different processing just create your own regular expression
and pass it in. If there's something really useful let me know and maybe
I'll add it.
.Sp
You can also pass a Data::FormValidator object as the value of \f(CW\*(C`validate\*(C'\fR.
This allows you to do things like requiring any one of several fields (but
where you don't care which one). In this case, the \f(CW\*(C`required\*(C'\fR option to
\&\f(CWnew()\fR is ignored, since you should be setting the required fields through
your FormValidator profile.
.Sp
By default, FormBuilder will try to use a profile named `fb' to validate
itself. You can change this by providing a different profile name when you
call \f(CWvalidate()\fR.
.Sp
Note that currently, doing validation through a FormValidator object
doesn't generate any JavaScript validation code for you.
.PP
Note that any other options specified are passed to the \f(CW\*(C`
\&
\&
.Ve
.PP
That's all you need for a sticky search form with the above HTML layout.
Notice that you can change the HTML layout as much as you want without
having to touch your CGI code.
.SS "Ex4: user_info.cgi"
.IX Subsection "Ex4: user_info.cgi"
This script grabs the user's information out of a database and lets
them update it dynamically. The DBI information is provided as an
example, your mileage may vary:
.PP
.Vb 1
\& #!/usr/bin/perl
\&
\& use strict;
\& use CGI::FormBuilder;
\& use DBI;
\& use DBD::Oracle
\&
\& my $dbh = DBI\->connect(\*(Aqdbi:Oracle:db\*(Aq, \*(Aquser\*(Aq, \*(Aqpass\*(Aq);
\&
\& # We create a new form. Note we\*(Aqve specified very little,
\& # since we\*(Aqre getting all our values from our database.
\& my $form = CGI::FormBuilder\->new(
\& fields => [qw(username password confirm_password
\& first_name last_name email)]
\& );
\&
\& # Now get the value of the username from our app
\& my $user = $form\->cgi_param(\*(Aquser\*(Aq);
\& my $sth = $dbh\->prepare("select * from user_info where user = \*(Aq$user\*(Aq");
\& $sth\->execute;
\& my $default_hashref = $sth\->fetchrow_hashref;
\&
\& # Render our form with the defaults we got in our hashref
\& print $form\->render(values => $default_hashref,
\& title => "User information for \*(Aq$user\*(Aq",
\& header => 1);
.Ve
.SS "Ex5: add_part.cgi"
.IX Subsection "Ex5: add_part.cgi"
This presents a screen for users to add parts to an inventory database.
Notice how it makes use of the \f(CW\*(C`sticky\*(C'\fR option. If there's an error,
then the form is presented with sticky values so that the user can
correct them and resubmit. If the submission is ok, though, then the
form is presented without sticky values so that the user can enter
the next part.
.PP
.Vb 1
\& #!/usr/bin/perl
\&
\& use strict;
\& use CGI::FormBuilder;
\&
\& my $form = CGI::FormBuilder\->new(
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(sn pn model qty comments)],
\& labels => {
\& sn => \*(AqSerial Number\*(Aq,
\& pn => \*(AqPart Number\*(Aq
\& },
\& sticky => 0,
\& header => 1,
\& required => [qw(sn pn model qty)],
\& validate => {
\& sn => \*(Aq/^[PL]\ed{2}\-\ed{4}\-\ed{4}$/\*(Aq,
\& pn => \*(Aq/^[AQM]\ed{2}\-\ed{4}$/\*(Aq,
\& qty => \*(AqINT\*(Aq
\& },
\& font => \*(Aqarial,helvetica\*(Aq
\& );
\&
\& # shrink the qty field for prettiness, lengthen model
\& $form\->field(name => \*(Aqqty\*(Aq, size => 4);
\& $form\->field(name => \*(Aqmodel\*(Aq, size => 60);
\&
\& if ($form\->submitted) {
\& if ($form\->validate) {
\& # Add part to database
\& } else {
\& # Invalid; show form and allow corrections
\& print $form\->render(sticky => 1);
\& exit;
\& }
\& }
\&
\& # Print form for next part addition.
\& print $form\->render;
.Ve
.PP
With the exception of the database code, that's the whole application.
.SS "Ex6: Session Management"
.IX Subsection "Ex6: Session Management"
This creates a session via \f(CW\*(C`CGI::Session\*(C'\fR, and ties it in with \fBFormBuilder\fR:
.PP
.Vb 1
\& #!/usr/bin/perl
\&
\& use CGI::Session;
\& use CGI::FormBuilder;
\&
\& my $form = CGI::FormBuilder\->new(fields => \e@fields);
\&
\& # Initialize session
\& my $session = CGI::Session\->new(\*(Aqdriver:File\*(Aq,
\& $form\->sessionid,
\& { Directory=>\*(Aq/tmp\*(Aq });
\&
\& if ($form\->submitted && $form\->validate) {
\& # Automatically save all parameters
\& $session\->save_param($form);
\& }
\&
\& # Ensure we have the right sessionid (might be new)
\& $form\->sessionid($session\->id);
\&
\& print $form\->render;
.Ve
.PP
Yes, it's pretty much that easy. See CGI::FormBuilder::Multi for
how to tie this into a multi-page form.
.SH "FREQUENTLY ASKED QUESTIONS (FAQ)"
.IX Header "FREQUENTLY ASKED QUESTIONS (FAQ)"
There are a couple questions and subtle traps that seem to poke people
on a regular basis. Here are some hints.
.SS "I'm confused. Why doesn't this work like CGI.pm?"
.IX Subsection "I'm confused. Why doesn't this work like CGI.pm?"
If you're used to \f(CW\*(C`CGI.pm\*(C'\fR, you have to do a little bit of a brain
shift when working with this module.
.PP
\&\fBFormBuilder\fR is designed to address fields as \fIabstract entities\fR.
That is, you don't create a "checkbox" or "radio group" per se.
Instead, you create a field for the data you want to collect.
The HTML representation is just one property of this field.
.PP
So, if you want a single-option checkbox, simply say something
like this:
.PP
.Vb 2
\& $form\->field(name => \*(Aqjoin_mailing_list\*(Aq,
\& options => [\*(AqYes\*(Aq]);
.Ve
.PP
If you want it to be checked by default, you add the \f(CW\*(C`value\*(C'\fR arg:
.PP
.Vb 3
\& $form\->field(name => \*(Aqjoin_mailing_list\*(Aq,
\& options => [\*(AqYes\*(Aq],
\& value => \*(AqYes\*(Aq);
.Ve
.PP
You see, you're creating a field that has one possible option: "Yes".
Then, you're saying its current value is, in fact, "Yes". This will
result in \fBFormBuilder\fR creating a single-option field (which is
a checkbox by default) and selecting the requested value (meaning
that the box will be checked).
.PP
If you want multiple values, then all you have to do is specify
multiple options:
.PP
.Vb 3
\& $form\->field(name => \*(Aqjoin_mailing_list\*(Aq,
\& options => [\*(AqYes\*(Aq, \*(AqNo\*(Aq],
\& value => \*(AqYes\*(Aq);
.Ve
.PP
Now you'll get a radio group, and "Yes" will be selected for you!
By viewing fields as data entities (instead of HTML tags) you
get much more flexibility and less code maintenance. If you want
to be able to accept multiple values, simply use the \f(CW\*(C`multiple\*(C'\fR arg:
.PP
.Vb 3
\& $form\->field(name => \*(Aqfavorite_colors\*(Aq,
\& options => [qw(red green blue)],
\& multiple => 1);
.Ve
.PP
In all of these examples, to get the data back you just use the
\&\f(CWfield()\fR method:
.PP
.Vb 1
\& my @colors = $form\->field(\*(Aqfavorite_colors\*(Aq);
.Ve
.PP
And the rest is taken care of for you.
.SS "How do I make a multi\-screen/multi\-mode form?"
.IX Subsection "How do I make a multi-screen/multi-mode form?"
This is easily doable, but you have to remember a couple things. Most
importantly, that \fBFormBuilder\fR only knows about those fields you've
told it about. So, let's assume that you're going to use a special
parameter called \f(CW\*(C`mode\*(C'\fR to control the mode of your application so
that you can call it like this:
.PP
.Vb 3
\& myapp.cgi?mode=list&...
\& myapp.cgi?mode=edit&...
\& myapp.cgi?mode=remove&...
.Ve
.PP
And so on. You need to do two things. First, you need the \f(CW\*(C`keepextras\*(C'\fR
option:
.PP
.Vb 1
\& my $form = CGI::FormBuilder\->new(..., keepextras => 1);
.Ve
.PP
This will maintain the \f(CW\*(C`mode\*(C'\fR field as a hidden field across requests
automatically. Second, you need to realize that since the \f(CW\*(C`mode\*(C'\fR is
not a defined field, you have to get it via the \f(CWcgi_param()\fR method:
.PP
.Vb 1
\& my $mode = $form\->cgi_param(\*(Aqmode\*(Aq);
.Ve
.PP
This will allow you to build a large multiscreen application easily,
even integrating it with modules like \f(CW\*(C`CGI::Application\*(C'\fR if you want.
.PP
You can also do this by simply defining \f(CW\*(C`mode\*(C'\fR as a field in your
\&\f(CW\*(C`fields\*(C'\fR declaration. The reason this is discouraged is because
when iterating over your fields you'll get \f(CW\*(C`mode\*(C'\fR, which you likely
don't want (since it's not "real" data).
.SS "Why won't CGI::FormBuilder work with post requests?"
.IX Subsection "Why won't CGI::FormBuilder work with post requests?"
It will, but chances are you're probably doing something like this:
.PP
.Vb 2
\& use CGI qw(:standard);
\& use CGI::FormBuilder;
\&
\& # Our "mode" parameter determines what we do
\& my $mode = param(\*(Aqmode\*(Aq);
\&
\& # Change our form based on our mode
\& if ($mode eq \*(Aqview\*(Aq) {
\& my $form = CGI::FormBuilder\->new(
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(...)],
\& );
\& } elsif ($mode eq \*(Aqedit\*(Aq) {
\& my $form = CGI::FormBuilder\->new(
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(...)],
\& );
\& }
.Ve
.PP
The problem is this: Once you read a \f(CW\*(C`post\*(C'\fR request, it's gone
forever. In the above code, what you're doing is having \f(CW\*(C`CGI.pm\*(C'\fR
read the \f(CW\*(C`post\*(C'\fR request (on the first call of \f(CWparam()\fR).
.PP
Luckily, there is an easy solution. First, you need to modify
your code to use the OO form of \f(CW\*(C`CGI.pm\*(C'\fR. Then, simply specify
the \f(CW\*(C`CGI\*(C'\fR object you create to the \f(CW\*(C`params\*(C'\fR option of \fBFormBuilder\fR:
.PP
.Vb 2
\& use CGI;
\& use CGI::FormBuilder;
\&
\& my $cgi = CGI\->new;
\&
\& # Our "mode" parameter determines what we do
\& my $mode = $cgi\->param(\*(Aqmode\*(Aq);
\&
\& # Change our form based on our mode
\& # Note: since it is post, must specify the \*(Aqparams\*(Aq option
\& if ($mode eq \*(Aqview\*(Aq) {
\& my $form = CGI::FormBuilder\->new(
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(...)],
\& params => $cgi # get CGI params
\& );
\& } elsif ($mode eq \*(Aqedit\*(Aq) {
\& my $form = CGI::FormBuilder\->new(
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(...)],
\& params => $cgi # get CGI params
\& );
\& }
.Ve
.PP
Or, since \fBFormBuilder\fR gives you a \f(CWcgi_param()\fR function, you
could also modify your code so you use \fBFormBuilder\fR exclusively,
as in the previous question.
.SS "How can I change option XXX based on a conditional?"
.IX Subsection "How can I change option XXX based on a conditional?"
To change an option, simply use its accessor at any time:
.PP
.Vb 4
\& my $form = CGI::FormBuilder\->new(
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(name email phone)]
\& );
\&
\& my $mode = $form\->cgi_param(\*(Aqmode\*(Aq);
\&
\& if ($mode eq \*(Aqadd\*(Aq) {
\& $form\->title(\*(AqAdd a new entry\*(Aq);
\& } elsif ($mode eq \*(Aqedit\*(Aq) {
\& $form\->title(\*(AqEdit existing entry\*(Aq);
\&
\& # do something to select existing values
\& my %values = select_values();
\&
\& $form\->values(\e%values);
\& }
\& print $form\->render;
.Ve
.PP
Using the accessors makes permanent changes to your object, so
be aware that if you want to reset something to its original
value later, you'll have to first save it and then reset it:
.PP
.Vb 3
\& my $style = $form\->stylesheet;
\& $form\->stylesheet(0); # turn off
\& $form\->stylesheet($style); # original setting
.Ve
.PP
You can also specify options to \f(CWrender()\fR, although using the
accessors is the preferred way.
.SS "How do I manually override the value of a field?"
.IX Subsection "How do I manually override the value of a field?"
You must specify the \f(CW\*(C`force\*(C'\fR option:
.PP
.Vb 3
\& $form\->field(name => \*(Aqname_of_field\*(Aq,
\& value => $value,
\& force => 1);
.Ve
.PP
If you don't specify \f(CW\*(C`force\*(C'\fR, then the CGI value will always win.
This is because of the stateless nature of the CGI protocol.
.SS "How do I make it so that the values aren't shown in the form?"
.IX Subsection "How do I make it so that the values aren't shown in the form?"
Turn off sticky:
.PP
.Vb 1
\& my $form = CGI::FormBuilder\->new(... sticky => 0);
.Ve
.PP
By turning off the \f(CW\*(C`sticky\*(C'\fR option, you will still be able to access
the values, but they won't show up in the form.
.SS "I can't get ""validate"" to accept my regular expressions!"
.IX Subsection "I can't get ""validate"" to accept my regular expressions!"
You're probably not specifying them within single quotes. See the
section on \f(CW\*(C`validate\*(C'\fR above.
.SS "Can FormBuilder handle file uploads?"
.IX Subsection "Can FormBuilder handle file uploads?"
It sure can, and it's really easy too. Just change the \f(CW\*(C`enctype\*(C'\fR
as an option to \f(CWnew()\fR:
.PP
.Vb 6
\& use CGI::FormBuilder;
\& my $form = CGI::FormBuilder\->new(
\& enctype => \*(Aqmultipart/form\-data\*(Aq,
\& method => \*(Aqpost\*(Aq,
\& fields => [qw(filename)]
\& );
\&
\& $form\->field(name => \*(Aqfilename\*(Aq, type => \*(Aqfile\*(Aq);
.Ve
.PP
And then get to your file the same way as \f(CW\*(C`CGI.pm\*(C'\fR:
.PP
.Vb 2
\& if ($form\->submitted) {
\& my $file = $form\->field(\*(Aqfilename\*(Aq);
\&
\& # save contents in file, etc ...
\& open F, ">$dir/$file" or die $!;
\& while (<$file>) {
\& print F;
\& }
\& close F;
\&
\& print $form\->confirm(header => 1);
\& } else {
\& print $form\->render(header => 1);
\& }
.Ve
.PP
In fact, that's a whole file upload program right there.
.SH REFERENCES
.IX Header "REFERENCES"
This really doesn't belong here, but unfortunately many people are
confused by references in Perl. Don't be \- they're not that tricky.
When you take a reference, you're basically turning something into
a scalar value. Sort of. You have to do this if you want to pass
arrays intact into functions in Perl 5.
.PP
A reference is taken by preceding the variable with a backslash (\e).
In our examples above, you saw something similar to this:
.PP
.Vb 1
\& my @fields = (\*(Aqname\*(Aq, \*(Aqemail\*(Aq); # same as = qw(name email)
\&
\& my $form = CGI::FormBuilder\->new(fields => \e@fields);
.Ve
.PP
Here, \f(CW\*(C`\e@fields\*(C'\fR is a reference. Specifically, it's an array
reference, or "arrayref" for short.
.PP
Similarly, we can do the same thing with hashes:
.PP
.Vb 4
\& my %validate = (
\& name => \*(AqNAME\*(Aq;
\& email => \*(AqEMAIL\*(Aq,
\& );
\&
\& my $form = CGI::FormBuilder\->new( ... validate => \e%validate);
.Ve
.PP
Here, \f(CW\*(C`\e%validate\*(C'\fR is a hash reference, or "hashref".
.PP
Basically, if you don't understand references and are having trouble
wrapping your brain around them, you can try this simple rule: Any time
you're passing an array or hash into a function, you must precede it
with a backslash. Usually that's true for CPAN modules.
.PP
Finally, there are two more types of references: anonymous arrayrefs
and anonymous hashrefs. These are created with \f(CW\*(C`[]\*(C'\fR and \f(CW\*(C`{}\*(C'\fR,
respectively. So, for our purposes there is no real difference between
this code:
.PP
.Vb 2
\& my @fields = qw(name email);
\& my %validate = (name => \*(AqNAME\*(Aq, email => \*(AqEMAIL\*(Aq);
\&
\& my $form = CGI::FormBuilder\->new(
\& fields => \e@fields,
\& validate => \e%validate
\& );
.Ve
.PP
And this code:
.PP
.Vb 4
\& my $form = CGI::FormBuilder\->new(
\& fields => [ qw(name email) ],
\& validate => { name => \*(AqNAME\*(Aq, email => \*(AqEMAIL\*(Aq }
\& );
.Ve
.PP
Except that the latter doesn't require that we first create
\&\f(CW@fields\fR and \f(CW%validate\fR variables.
.SH "ENVIRONMENT VARIABLES"
.IX Header "ENVIRONMENT VARIABLES"
.SS FORMBUILDER_DEBUG
.IX Subsection "FORMBUILDER_DEBUG"
This toggles the debug flag, so that you can control FormBuilder
debugging globally. Helpful in mod_perl.
.SH NOTES
.IX Header "NOTES"
Parameters beginning with a leading underscore are reserved for
future use by this module. Use at your own peril.
.PP
The \f(CWfield()\fR method has the alias \f(CWparam()\fR for compatibility
with other modules, allowing you to pass a \f(CW$form\fR around just
like a \f(CW$cgi\fR object.
.PP
The output of the HTML generated natively may change slightly from
release to release. If you need precise control, use a template.
.PP
Every attempt has been made to make this module taint-safe (\-T).
However, due to the way tainting works, you may run into the
message "Insecure dependency" or "Insecure \f(CW$ENV\fR{PATH}". If so,
make sure you are setting \f(CW$ENV{PATH}\fR at the top of your script.
.SH ACKNOWLEDGEMENTS
.IX Header "ACKNOWLEDGEMENTS"
This module has really taken off, thanks to very useful input, bug
reports, and encouraging feedback from a number of people, including:
.PP
.Vb 10
\& Norton Allen
\& Mark Belanger
\& Peter Billam
\& Brad Bowman
\& Jonathan Buhacoff
\& Godfrey Carnegie
\& Jakob Curdes
\& Laurent Dami
\& Bob Egert
\& Peter Eichman
\& Adam Foxson
\& Jorge Gonzalez
\& Florian Helmberger
\& Mark Hedges
\& Mark Houliston
\& Victor Igumnov
\& Robert James Kaes
\& Dimitry Kharitonov
\& Randy Kobes
\& William Large
\& Kevin Lubic
\& Robert Mathews
\& Mehryar
\& Klaas Naajikens
\& Koos Pol
\& Shawn Poulson
\& Victor Porton
\& Dan Collis Puro
\& Wolfgang Radke
\& David Siegal
\& Stephan Springl
\& Ryan Tate
\& John Theus
\& Remi Turboult
\& Andy Wardley
\& Raphael Wegmann
\& Emanuele Zeppieri
.Ve
.PP
Thanks!
.SH "SEE ALSO"
.IX Header "SEE ALSO"
CGI::FormBuilder::Template, CGI::FormBuilder::Messages,
CGI::FormBuilder::Multi, CGI::FormBuilder::Source::File,
CGI::FormBuilder::Field, CGI::FormBuilder::Util,
CGI::FormBuilder::Util, HTML::Template, Text::Template
CGI::FastTemplate
.SH REVISION
.IX Header "REVISION"
\&\f(CW$Id:\fR FormBuilder.pm 65 2006\-09\-07 18:11:43Z nwiger $
.SH AUTHOR
.IX Header "AUTHOR"
Copyright (c) Nate Wiger . All Rights Reserved.
.PP
This module is free software; you may copy this under the terms of
the GNU General Public License, or the Artistic License, copies of
which should have accompanied your Perl kit.