| [1] | 1 | package Text::FormBuilder; | 
|---|
 | 2 |  | 
|---|
 | 3 | use strict; | 
|---|
 | 4 | use warnings; | 
|---|
 | 5 |  | 
|---|
| [53] | 6 | use base qw(Exporter Class::ParseText::Base); | 
|---|
| [42] | 7 | use vars qw($VERSION @EXPORT); | 
|---|
| [1] | 8 |  | 
|---|
| [89] | 9 | $VERSION = '0.12'; | 
|---|
| [42] | 10 | @EXPORT = qw(create_form); | 
|---|
| [19] | 11 |  | 
|---|
| [16] | 12 | use Carp; | 
|---|
| [1] | 13 | use Text::FormBuilder::Parser; | 
|---|
 | 14 | use CGI::FormBuilder; | 
|---|
 | 15 |  | 
|---|
| [69] | 16 | use Data::Dumper; | 
|---|
 | 17 | $Data::Dumper::Terse = 1;           # don't dump $VARn names | 
|---|
 | 18 | $Data::Dumper::Quotekeys = 0;       # don't quote simple string keys | 
|---|
 | 19 |  | 
|---|
| [28] | 20 | # the static default options passed to CGI::FormBuilder->new | 
|---|
| [23] | 21 | my %DEFAULT_OPTIONS = ( | 
|---|
 | 22 |     method => 'GET', | 
|---|
 | 23 |     keepextras => 1, | 
|---|
 | 24 | ); | 
|---|
 | 25 |  | 
|---|
| [30] | 26 | # the built in CSS for the template | 
|---|
 | 27 | my $DEFAULT_CSS = <<END; | 
|---|
 | 28 | table { padding: 1em; } | 
|---|
| [60] | 29 | td table { padding: 0; } /* exclude the inner checkbox tables */ | 
|---|
| [30] | 30 | #author, #footer { font-style: italic; } | 
|---|
 | 31 | caption h2 { padding: .125em .5em; background: #ccc; text-align: left; } | 
|---|
| [71] | 32 | fieldset { margin: 1em 0; border: none; border-top: 2px solid #999; } | 
|---|
| [67] | 33 | legend { font-size: 1.25em; font-weight: bold; background: #ccc; padding: .125em .25em; border: 1px solid #666; } | 
|---|
| [30] | 34 | th { text-align: left; } | 
|---|
| [71] | 35 | th h2 { padding: .125em .5em; background: #eee; font-size: 1.25em; } | 
|---|
| [82] | 36 | .label { font-weight: normal; text-align: right; vertical-align: top; } | 
|---|
| [30] | 37 | td ul { list-style: none; padding-left: 0; margin-left: 0; } | 
|---|
| [71] | 38 | .note { background: #eee; padding: .5em 1em; } | 
|---|
| [30] | 39 | .sublabel { color: #999; } | 
|---|
 | 40 | .invalid { background: red; } | 
|---|
 | 41 | END | 
|---|
 | 42 |  | 
|---|
| [34] | 43 | # default messages that can be localized | 
|---|
| [33] | 44 | my %DEFAULT_MESSAGES = ( | 
|---|
| [34] | 45 |     text_author   => 'Created by %s', | 
|---|
 | 46 |     text_madewith => 'Made with %s version %s', | 
|---|
| [83] | 47 |     text_required => 'Fields that are <strong>highlighted</strong> are required.', | 
|---|
| [34] | 48 |     text_invalid  => 'Missing or invalid value.', | 
|---|
| [33] | 49 | ); | 
|---|
 | 50 |  | 
|---|
| [34] | 51 | my $DEFAULT_CHARSET = 'iso-8859-1'; | 
|---|
 | 52 |  | 
|---|
| [39] | 53 | # options to clean up the code with Perl::Tidy | 
|---|
 | 54 | my $TIDY_OPTIONS = '-nolq -ci=4 -ce'; | 
|---|
 | 55 |  | 
|---|
| [42] | 56 | my $HTML_EXTS   = qr/\.html?$/; | 
|---|
| [46] | 57 | my $MODULE_EXTS = qr/\.pm$/; | 
|---|
| [42] | 58 | my $SCRIPT_EXTS = qr/\.(pl|cgi)$/; | 
|---|
 | 59 |  | 
|---|
 | 60 | # superautomagical exported function | 
|---|
 | 61 | sub create_form { | 
|---|
 | 62 |     my ($source, $options, $destination) = @_; | 
|---|
 | 63 |     my $parser = __PACKAGE__->parse($source); | 
|---|
 | 64 |     $parser->build(%{ $options || {} }); | 
|---|
 | 65 |     if ($destination) { | 
|---|
 | 66 |         if (ref $destination) { | 
|---|
| [53] | 67 |             croak '[' . (caller(0))[3] . "] Don't know what to do with a ref for $destination"; | 
|---|
 | 68 |             #TODO: what DO ref dests mean? | 
|---|
| [42] | 69 |         } else { | 
|---|
 | 70 |             # write webpage, script, or module | 
|---|
| [46] | 71 |             if ($destination =~ $MODULE_EXTS) { | 
|---|
| [52] | 72 |                 $parser->write_module($destination, 1); | 
|---|
| [42] | 73 |             } elsif ($destination =~ $SCRIPT_EXTS) { | 
|---|
| [52] | 74 |                 $parser->write_script($destination, 1); | 
|---|
| [42] | 75 |             } else { | 
|---|
| [46] | 76 |                 $parser->write($destination); | 
|---|
| [42] | 77 |             } | 
|---|
 | 78 |         } | 
|---|
 | 79 |     } else { | 
|---|
 | 80 |         defined wantarray ? return $parser->form : $parser->write; | 
|---|
 | 81 |     } | 
|---|
 | 82 | } | 
|---|
 | 83 |  | 
|---|
| [53] | 84 | # subclass of Class::ParseText::Base | 
|---|
 | 85 | sub init { | 
|---|
 | 86 |     my $self = shift; | 
|---|
 | 87 |     $self->{parser}         = Text::FormBuilder::Parser->new; | 
|---|
 | 88 |     $self->{start_rule}     = 'form_spec'; | 
|---|
 | 89 |     $self->{ensure_newline} = 1; | 
|---|
| [42] | 90 |     return $self; | 
|---|
 | 91 | } | 
|---|
 | 92 |  | 
|---|
| [33] | 93 | # this is where a lot of the magic happens | 
|---|
| [1] | 94 | sub build { | 
|---|
 | 95 |     my ($self, %options) = @_; | 
|---|
| [12] | 96 |      | 
|---|
 | 97 |     # our custom %options: | 
|---|
 | 98 |     # form_only: use only the form part of the template | 
|---|
 | 99 |     my $form_only = $options{form_only}; | 
|---|
| [33] | 100 |      | 
|---|
 | 101 |     # css, extra_css: allow for custom inline stylesheets | 
|---|
| [38] | 102 |     #   neat trick: css => '@import(my_external_stylesheet.css);' | 
|---|
| [33] | 103 |     #   will let you use an external stylesheet | 
|---|
| [34] | 104 |     #   CSS Hint: to get multiple sections to all line up their fields, | 
|---|
 | 105 |     #   set a standard width for th.label | 
|---|
| [72] | 106 |     # external_css: scalar for a single external stylesheet; array for | 
|---|
 | 107 |     #   multiple sheets; prepended to the beginning of the CSS as @import | 
|---|
 | 108 |     #   statetments | 
|---|
| [30] | 109 |     my $css; | 
|---|
 | 110 |     $css = $options{css} || $DEFAULT_CSS; | 
|---|
| [72] | 111 |     if ($options{external_css}) { | 
|---|
 | 112 |         my $ref = ref $options{external_css}; | 
|---|
 | 113 |         if ($ref eq 'ARRAY') { | 
|---|
 | 114 |             # loop over the list of external sheets | 
|---|
 | 115 |             my $external_sheets = join("\n", map { "\@import url($_);" } @{ $options{external_css} }); | 
|---|
 | 116 |             $css = "$external_sheets\n$css"; | 
|---|
 | 117 |         } elsif ($ref) { | 
|---|
 | 118 |             croak '[' . (caller(0))[3] . "] Don't know how to handle $ref reference as an argument to external_css"; | 
|---|
 | 119 |         } else { | 
|---|
 | 120 |             $css = "\@import url($options{external_css});\n$css"; | 
|---|
 | 121 |         } | 
|---|
 | 122 |     } | 
|---|
| [30] | 123 |     $css .= $options{extra_css} if $options{extra_css}; | 
|---|
| [12] | 124 |      | 
|---|
| [33] | 125 |     # messages | 
|---|
| [38] | 126 |     # code pulled (with modifications) from CGI::FormBuilder | 
|---|
| [33] | 127 |     if ($options{messages}) { | 
|---|
 | 128 |         # if its a hashref, we'll just pass it on to CGI::FormBuilder | 
|---|
 | 129 |          | 
|---|
 | 130 |         if (my $ref = ref $options{messages}) { | 
|---|
 | 131 |             # hashref pass on to CGI::FormBuilder | 
|---|
 | 132 |             croak "[Text::FormBuilder] Argument to 'messages' option must be a filename or hashref" unless $ref eq 'HASH'; | 
|---|
 | 133 |             while (my ($key,$value) = each %DEFAULT_MESSAGES) { | 
|---|
 | 134 |                 $options{messages}{$key} ||= $DEFAULT_MESSAGES{$key}; | 
|---|
 | 135 |             } | 
|---|
 | 136 |         } else { | 
|---|
 | 137 |             # filename, just *warn* on missing, and use defaults | 
|---|
 | 138 |             if (-f $options{messages} && -r _ && open(MESSAGES, "< $options{messages}")) { | 
|---|
| [34] | 139 |                 $options{messages} = { %DEFAULT_MESSAGES }; | 
|---|
| [33] | 140 |                 while(<MESSAGES>) { | 
|---|
 | 141 |                     next if /^\s*#/ || /^\s*$/; | 
|---|
 | 142 |                     chomp; | 
|---|
 | 143 |                     my($key,$value) = split ' ', $_, 2; | 
|---|
 | 144 |                     ($options{messages}{$key} = $value) =~ s/\s+$//; | 
|---|
 | 145 |                 } | 
|---|
 | 146 |                 close MESSAGES; | 
|---|
 | 147 |             } else { | 
|---|
| [53] | 148 |                 carp '[' . (caller(0))[3] . "] Could not read messages file $options{messages}: $!"; | 
|---|
| [33] | 149 |             } | 
|---|
| [1] | 150 |         } | 
|---|
| [34] | 151 |     } else { | 
|---|
 | 152 |         $options{messages} = { %DEFAULT_MESSAGES }; | 
|---|
| [1] | 153 |     } | 
|---|
 | 154 |      | 
|---|
| [53] | 155 |     # character set | 
|---|
| [34] | 156 |     my $charset = $options{charset}; | 
|---|
 | 157 |      | 
|---|
| [33] | 158 |     # save the build options so they can be used from write_module | 
|---|
 | 159 |     $self->{build_options} = { %options }; | 
|---|
 | 160 |      | 
|---|
 | 161 |     # remove our custom options before we hand off to CGI::FormBuilder | 
|---|
| [34] | 162 |     delete $options{$_} foreach qw(form_only css extra_css charset); | 
|---|
| [33] | 163 |      | 
|---|
| [21] | 164 |     # expand groups | 
|---|
| [70] | 165 |     if (my %groups = %{ $self->{form_spec}{groups} || {} }) {         | 
|---|
| [42] | 166 |         for my $section (@{ $self->{form_spec}{sections} || [] }) { | 
|---|
 | 167 |             foreach (grep { $$_[0] eq 'group' } @{ $$section{lines} }) { | 
|---|
 | 168 |                 $$_[1]{group} =~ s/^\%//;       # strip leading % from group var name | 
|---|
 | 169 |                  | 
|---|
 | 170 |                 if (exists $groups{$$_[1]{group}}) { | 
|---|
 | 171 |                     my @fields; # fields in the group | 
|---|
 | 172 |                     push @fields, { %$_ } foreach @{ $groups{$$_[1]{group}} }; | 
|---|
 | 173 |                     for my $field (@fields) { | 
|---|
 | 174 |                         $$field{label} ||= ucfirst $$field{name}; | 
|---|
 | 175 |                         $$field{name} = "$$_[1]{name}_$$field{name}";                 | 
|---|
 | 176 |                     } | 
|---|
| [73] | 177 |                     $_ = [ | 
|---|
 | 178 |                         'group', | 
|---|
 | 179 |                         { | 
|---|
 | 180 |                             label => $$_[1]{label} || ucfirst(join(' ',split('_',$$_[1]{name}))), | 
|---|
 | 181 |                             comment => $$_[1]{comment}, | 
|---|
 | 182 |                             group => \@fields, | 
|---|
 | 183 |                         }, | 
|---|
 | 184 |                     ]; | 
|---|
| [29] | 185 |                 } | 
|---|
| [21] | 186 |             } | 
|---|
 | 187 |         } | 
|---|
 | 188 |     } | 
|---|
| [1] | 189 |      | 
|---|
| [33] | 190 |     # the actual fields that are given to CGI::FormBuilder | 
|---|
| [42] | 191 |     # make copies so that when we trim down the sections | 
|---|
 | 192 |     # we don't lose the form field information | 
|---|
| [21] | 193 |     $self->{form_spec}{fields} = []; | 
|---|
| [29] | 194 |      | 
|---|
 | 195 |     for my $section (@{ $self->{form_spec}{sections} || [] }) { | 
|---|
 | 196 |         for my $line (@{ $$section{lines} }) { | 
|---|
 | 197 |             if ($$line[0] eq 'group') { | 
|---|
| [42] | 198 |                 push @{ $self->{form_spec}{fields} }, { %{$_} } foreach @{ $$line[1]{group} }; | 
|---|
| [29] | 199 |             } elsif ($$line[0] eq 'field') { | 
|---|
| [88] | 200 |                 #die $$line[1] unless ref $$line[1]; | 
|---|
| [42] | 201 |                 push @{ $self->{form_spec}{fields} }, { %{$$line[1]} }; | 
|---|
| [29] | 202 |             } | 
|---|
| [21] | 203 |         } | 
|---|
 | 204 |     } | 
|---|
 | 205 |      | 
|---|
| [39] | 206 |     # substitute in custom validation subs and pattern definitions for field validation | 
|---|
 | 207 |     my %patterns = %{ $self->{form_spec}{patterns} || {} }; | 
|---|
 | 208 |     my %subs = %{ $self->{form_spec}{subs} || {} }; | 
|---|
 | 209 |      | 
|---|
 | 210 |     foreach (@{ $self->{form_spec}{fields} }) { | 
|---|
 | 211 |         if ($$_{validate}) { | 
|---|
 | 212 |             if (exists $patterns{$$_{validate}}) { | 
|---|
| [33] | 213 |                 $$_{validate} = $patterns{$$_{validate}}; | 
|---|
| [39] | 214 |             # TODO: need the Data::Dumper code to work for this | 
|---|
 | 215 |             # for now, we just warn that it doesn't work | 
|---|
 | 216 |             } elsif (exists $subs{$$_{validate}}) { | 
|---|
| [53] | 217 |                 warn '[' . (caller(0))[3] . "] validate coderefs don't work yet"; | 
|---|
| [39] | 218 |                 delete $$_{validate}; | 
|---|
| [42] | 219 | ##                 $$_{validate} = $subs{$$_{validate}}; | 
|---|
| [33] | 220 |             } | 
|---|
 | 221 |         } | 
|---|
 | 222 |     } | 
|---|
 | 223 |      | 
|---|
| [50] | 224 |     # get user-defined lists; can't make this conditional because | 
|---|
 | 225 |     # we need to be able to fall back to CGI::FormBuilder's lists | 
|---|
 | 226 |     # even if the user didn't define any | 
|---|
 | 227 |     my %lists = %{ $self->{form_spec}{lists} || {} }; | 
|---|
 | 228 |      | 
|---|
| [1] | 229 |     # substitute in list names | 
|---|
| [50] | 230 |     foreach (@{ $self->{form_spec}{fields} }) { | 
|---|
 | 231 |         next unless $$_{list}; | 
|---|
 | 232 |          | 
|---|
 | 233 |         $$_{list} =~ s/^\@//;   # strip leading @ from list var name | 
|---|
 | 234 |          | 
|---|
 | 235 |         # a hack so we don't get screwy reference errors | 
|---|
 | 236 |         if (exists $lists{$$_{list}}) { | 
|---|
 | 237 |             my @list; | 
|---|
 | 238 |             push @list, { %$_ } foreach @{ $lists{$$_{list}} }; | 
|---|
 | 239 |             $$_{options} = \@list; | 
|---|
 | 240 |         } else { | 
|---|
 | 241 |             # assume that the list name is a builtin  | 
|---|
 | 242 |             # and let it fall through to CGI::FormBuilder | 
|---|
 | 243 |             $$_{options} = $$_{list}; | 
|---|
| [1] | 244 |         } | 
|---|
| [50] | 245 |     } continue { | 
|---|
 | 246 |         delete $$_{list}; | 
|---|
| [30] | 247 |     } | 
|---|
| [21] | 248 |      | 
|---|
| [30] | 249 |     # special case single-value checkboxes | 
|---|
 | 250 |     foreach (grep { $$_{type} && $$_{type} eq 'checkbox' } @{ $self->{form_spec}{fields} }) { | 
|---|
 | 251 |         unless ($$_{options}) { | 
|---|
 | 252 |             $$_{options} = [ { $$_{name} => $$_{label} || ucfirst join(' ',split(/_/,$$_{name})) } ]; | 
|---|
 | 253 |         } | 
|---|
 | 254 |     } | 
|---|
 | 255 |      | 
|---|
| [63] | 256 |     # use columns for displaying checkbox fields larger than 2 items | 
|---|
| [14] | 257 |     foreach (@{ $self->{form_spec}{fields} }) { | 
|---|
| [60] | 258 |         if (ref $$_{options} and @{ $$_{options} } >= 3) { | 
|---|
 | 259 |             $$_{columns} = int(@{ $$_{options} } / 8) + 1; | 
|---|
 | 260 |         } | 
|---|
| [14] | 261 |     } | 
|---|
| [1] | 262 |      | 
|---|
| [24] | 263 |     # remove extraneous undefined values | 
|---|
| [64] | 264 |     # also check for approriate version of CGI::FormBuilder | 
|---|
 | 265 |     # for some advanced options | 
|---|
 | 266 |     my $FB_version = CGI::FormBuilder->VERSION; | 
|---|
| [24] | 267 |     for my $field (@{ $self->{form_spec}{fields} }) { | 
|---|
 | 268 |         defined $$field{$_} or delete $$field{$_} foreach keys %{ $field }; | 
|---|
| [64] | 269 |          | 
|---|
| [66] | 270 |         unless ($FB_version >= '3.02') { | 
|---|
 | 271 |             for (qw(growable other)) { | 
|---|
 | 272 |                 if ($$field{$_}) { | 
|---|
 | 273 |                     warn '[' . (caller(0))[3] . "] '$_' fields not supported by FB $FB_version (requires 3.02)"; | 
|---|
 | 274 |                     delete $$field{$_}; | 
|---|
 | 275 |                 } | 
|---|
| [64] | 276 |             } | 
|---|
 | 277 |         } | 
|---|
| [68] | 278 |     } | 
|---|
| [24] | 279 |      | 
|---|
| [64] | 280 |     # assign the field names to the sections | 
|---|
| [39] | 281 |     foreach (@{ $self->{form_spec}{sections} }) { | 
|---|
| [42] | 282 |         for my $line (@{ $$_{lines} }) { | 
|---|
 | 283 |             if ($$line[0] eq 'field') { | 
|---|
 | 284 |                 $$line[1] = $$line[1]{name}; | 
|---|
 | 285 |             } | 
|---|
| [39] | 286 |         } | 
|---|
 | 287 |     } | 
|---|
 | 288 |      | 
|---|
| [87] | 289 |     my %fb_params; | 
|---|
 | 290 |     if ($self->{form_spec}->{fb_params}) { | 
|---|
 | 291 |         require YAML; | 
|---|
 | 292 |         eval { %fb_params = %{ YAML::Load($self->{form_spec}->{fb_params}) } }; | 
|---|
 | 293 |         if ($@) { | 
|---|
 | 294 |             warn '[' . (caller(0))[3] . "] Bad !fb parameter block:\n$@"; | 
|---|
 | 295 |         } | 
|---|
 | 296 |     } | 
|---|
 | 297 |      | 
|---|
| [69] | 298 |     # gather together all of the form options | 
|---|
 | 299 |     $self->{form_options} = { | 
|---|
| [23] | 300 |         %DEFAULT_OPTIONS, | 
|---|
| [33] | 301 |         # need to explicity set the fields so that simple text fields get picked up | 
|---|
| [29] | 302 |         fields   => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], | 
|---|
| [24] | 303 |         required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], | 
|---|
 | 304 |         title => $self->{form_spec}{title}, | 
|---|
| [25] | 305 |         text  => $self->{form_spec}{description}, | 
|---|
| [81] | 306 |         # use 'defined' so we are able to differentiate between 'submit = 0' (no submit button) | 
|---|
 | 307 |         # and 'submit = undef' (use default submit button) | 
|---|
 | 308 |         ( defined $self->{form_spec}{submit} ? (submit => $self->{form_spec}{submit}) : () ), | 
|---|
 | 309 |         reset => $self->{form_spec}{reset}, | 
|---|
| [1] | 310 |         template => { | 
|---|
 | 311 |             type => 'Text', | 
|---|
 | 312 |             engine => { | 
|---|
 | 313 |                 TYPE       => 'STRING', | 
|---|
| [34] | 314 |                 SOURCE     => $form_only ? $self->_form_template : $self->_template($css, $charset), | 
|---|
| [11] | 315 |                 DELIMITERS => [ qw(<% %>) ], | 
|---|
| [1] | 316 |             }, | 
|---|
 | 317 |             data => { | 
|---|
| [81] | 318 |                 #TODO: make FB aware of sections | 
|---|
| [29] | 319 |                 sections    => $self->{form_spec}{sections}, | 
|---|
| [14] | 320 |                 author      => $self->{form_spec}{author}, | 
|---|
 | 321 |                 description => $self->{form_spec}{description}, | 
|---|
| [1] | 322 |             }, | 
|---|
 | 323 |         }, | 
|---|
| [87] | 324 |         #TODO: fields in fb_params are not getting recognized | 
|---|
 | 325 |         %fb_params,     # params from the formspec file | 
|---|
 | 326 |         %options,       # params from this method invocation | 
|---|
| [69] | 327 |     }; | 
|---|
 | 328 |      | 
|---|
 | 329 |     # create the form object | 
|---|
 | 330 |     $self->{form} = CGI::FormBuilder->new(%{ $self->{form_options} }); | 
|---|
 | 331 |      | 
|---|
 | 332 |     # ...and set up its fields | 
|---|
| [1] | 333 |     $self->{form}->field(%{ $_ }) foreach @{ $self->{form_spec}{fields} }; | 
|---|
 | 334 |      | 
|---|
| [16] | 335 |     # mark structures as built | 
|---|
 | 336 |     $self->{built} = 1; | 
|---|
 | 337 |      | 
|---|
| [1] | 338 |     return $self; | 
|---|
 | 339 | } | 
|---|
 | 340 |  | 
|---|
 | 341 | sub write { | 
|---|
 | 342 |     my ($self, $outfile) = @_; | 
|---|
| [16] | 343 |      | 
|---|
 | 344 |     # automatically call build if needed to | 
|---|
 | 345 |     # allow the new->parse->write shortcut | 
|---|
 | 346 |     $self->build unless $self->{built}; | 
|---|
 | 347 |      | 
|---|
| [1] | 348 |     if ($outfile) { | 
|---|
 | 349 |         open FORM, "> $outfile"; | 
|---|
 | 350 |         print FORM $self->form->render; | 
|---|
 | 351 |         close FORM; | 
|---|
 | 352 |     } else { | 
|---|
 | 353 |         print $self->form->render; | 
|---|
 | 354 |     } | 
|---|
 | 355 | } | 
|---|
 | 356 |  | 
|---|
| [69] | 357 | # dump the form options as eval-able code | 
|---|
 | 358 | sub _form_options_code { | 
|---|
| [39] | 359 |     my $self = shift; | 
|---|
| [69] | 360 |     my $d = Data::Dumper->new([ $self->{form_options} ], [ '*options' ]); | 
|---|
 | 361 |     return keys %{ $self->{form_options} } > 0 ? $d->Dump : '';     | 
|---|
 | 362 | } | 
|---|
 | 363 | # dump the field setup subs as eval-able code | 
|---|
 | 364 | # pass in the variable name of the form object | 
|---|
 | 365 | # (defaults to '$form') | 
|---|
| [70] | 366 | # TODO: revise this code to use the new 'fieldopts' | 
|---|
 | 367 | # option to the FB constructor (requires FB 3.02) | 
|---|
| [69] | 368 | sub _field_setup_code { | 
|---|
 | 369 |     my $self = shift; | 
|---|
 | 370 |     my $object_name = shift || '$form'; | 
|---|
 | 371 |     return join( | 
|---|
| [12] | 372 |         "\n",  | 
|---|
| [69] | 373 |         map { $object_name . '->field' . Data::Dumper->Dump([$_],['*field']) . ';' } @{ $self->{form_spec}{fields} } | 
|---|
| [12] | 374 |     ); | 
|---|
| [82] | 375 | } | 
|---|
| [39] | 376 |  | 
|---|
| [82] | 377 | sub as_module { | 
|---|
| [39] | 378 |     my ($self, $package, $use_tidy) = @_; | 
|---|
 | 379 |  | 
|---|
| [53] | 380 |     croak '[' . (caller(0))[3] . '] Expecting a package name' unless $package; | 
|---|
| [39] | 381 |      | 
|---|
| [52] | 382 |     # remove a trailing .pm | 
|---|
 | 383 |     $package =~ s/\.pm$//; | 
|---|
| [82] | 384 |  | 
|---|
 | 385 |     # auto-build | 
|---|
 | 386 |     $self->build unless $self->{built}; | 
|---|
 | 387 |  | 
|---|
| [69] | 388 |     my $form_options = $self->_form_options_code; | 
|---|
 | 389 |     my $field_setup = $self->_field_setup_code('$self'); | 
|---|
| [39] | 390 |      | 
|---|
| [69] | 391 |     # old style of module | 
|---|
 | 392 |     # TODO: how to keep this (as deprecated method) | 
|---|
 | 393 |     my $old_module = <<END; | 
|---|
| [12] | 394 | package $package; | 
|---|
 | 395 | use strict; | 
|---|
 | 396 | use warnings; | 
|---|
 | 397 |  | 
|---|
 | 398 | use CGI::FormBuilder; | 
|---|
 | 399 |  | 
|---|
| [15] | 400 | sub get_form { | 
|---|
| [39] | 401 |     my \$q = shift; | 
|---|
 | 402 |  | 
|---|
| [69] | 403 |     my \$self = CGI::FormBuilder->new( | 
|---|
 | 404 |         $form_options, | 
|---|
 | 405 |         \@_, | 
|---|
 | 406 |     ); | 
|---|
| [12] | 407 |      | 
|---|
| [69] | 408 |     $field_setup | 
|---|
 | 409 |      | 
|---|
 | 410 |     return \$self; | 
|---|
| [12] | 411 | } | 
|---|
 | 412 |  | 
|---|
 | 413 | # module return | 
|---|
 | 414 | 1; | 
|---|
 | 415 | END | 
|---|
| [23] | 416 |  | 
|---|
| [69] | 417 |     # new style of module | 
|---|
 | 418 |     my $module = <<END; | 
|---|
 | 419 | package $package; | 
|---|
 | 420 | use strict; | 
|---|
 | 421 | use warnings; | 
|---|
 | 422 |  | 
|---|
 | 423 | use base qw(CGI::FormBuilder); | 
|---|
 | 424 |  | 
|---|
 | 425 | sub new { | 
|---|
 | 426 |     my \$invocant = shift; | 
|---|
 | 427 |     my \$class = ref \$invocant || \$invocant; | 
|---|
 | 428 |      | 
|---|
 | 429 |     my \$self = CGI::FormBuilder->new( | 
|---|
 | 430 |         $form_options, | 
|---|
 | 431 |         \@_, | 
|---|
 | 432 |     ); | 
|---|
 | 433 |      | 
|---|
 | 434 |     $field_setup | 
|---|
 | 435 |      | 
|---|
 | 436 |     # re-bless into this class | 
|---|
 | 437 |     bless \$self, \$class; | 
|---|
 | 438 | } | 
|---|
 | 439 |  | 
|---|
 | 440 | # module return | 
|---|
 | 441 | 1; | 
|---|
 | 442 | END | 
|---|
| [82] | 443 |  | 
|---|
 | 444 |     $module = _tidy_code($module, $use_tidy) if $use_tidy; | 
|---|
 | 445 |      | 
|---|
 | 446 |     return $module; | 
|---|
 | 447 | } | 
|---|
 | 448 |  | 
|---|
 | 449 | sub write_module { | 
|---|
| [83] | 450 |     my ($self, $package, $use_tidy) = @_; | 
|---|
| [82] | 451 |      | 
|---|
 | 452 |     my $module = $self->as_module($package, $use_tidy); | 
|---|
 | 453 |      | 
|---|
| [83] | 454 |     my $outfile = (split(/::/, $package))[-1]; | 
|---|
 | 455 |     $outfile .= '.pm' unless $outfile =~ /\.pm$/; | 
|---|
 | 456 |     _write_output_file($module, $outfile); | 
|---|
| [39] | 457 |     return $self; | 
|---|
 | 458 | } | 
|---|
 | 459 |  | 
|---|
| [82] | 460 | sub as_script { | 
|---|
 | 461 |     my ($self, $use_tidy) = @_; | 
|---|
| [39] | 462 |      | 
|---|
| [82] | 463 |     # auto-build | 
|---|
 | 464 |     $self->build unless $self->{built}; | 
|---|
 | 465 |      | 
|---|
| [69] | 466 |     my $form_options = $self->_form_options_code; | 
|---|
 | 467 |     my $field_setup = $self->_field_setup_code('$form'); | 
|---|
 | 468 |  | 
|---|
| [39] | 469 |     my $script = <<END; | 
|---|
 | 470 | #!/usr/bin/perl | 
|---|
 | 471 | use strict; | 
|---|
 | 472 | use warnings; | 
|---|
 | 473 |  | 
|---|
 | 474 | use CGI::FormBuilder; | 
|---|
 | 475 |  | 
|---|
| [69] | 476 | my \$form = CGI::FormBuilder->new( | 
|---|
 | 477 |     $form_options | 
|---|
 | 478 | ); | 
|---|
| [39] | 479 |  | 
|---|
| [69] | 480 | $field_setup | 
|---|
| [39] | 481 |      | 
|---|
 | 482 | unless (\$form->submitted && \$form->validate) { | 
|---|
 | 483 |     print \$form->render; | 
|---|
 | 484 | } else { | 
|---|
 | 485 |     # do something with the entered data | 
|---|
 | 486 | } | 
|---|
 | 487 | END | 
|---|
| [82] | 488 |     $script = _tidy_code($script, $use_tidy) if $use_tidy; | 
|---|
| [39] | 489 |      | 
|---|
| [82] | 490 |     return $script; | 
|---|
 | 491 | } | 
|---|
 | 492 |      | 
|---|
 | 493 | sub write_script { | 
|---|
 | 494 |     my ($self, $script_name, $use_tidy) = @_; | 
|---|
 | 495 |  | 
|---|
 | 496 |     croak '[' . (caller(0))[3] . '] Expecting a script name' unless $script_name; | 
|---|
 | 497 |  | 
|---|
 | 498 |     my $script = $self->as_script($use_tidy); | 
|---|
 | 499 |      | 
|---|
 | 500 |     _write_output_file($script, $script_name);    | 
|---|
| [39] | 501 |     return $self; | 
|---|
 | 502 | } | 
|---|
 | 503 |  | 
|---|
| [82] | 504 | sub _tidy_code { | 
|---|
 | 505 |     my ($source_code, $use_tidy) = @_; | 
|---|
 | 506 |     eval 'use Perl::Tidy'; | 
|---|
 | 507 |     carp '[' . (caller(0))[3] . "] Can't tidy the code: $@" and return $source_code if $@; | 
|---|
 | 508 |      | 
|---|
 | 509 |     # use the options string only if it begins with '_' | 
|---|
 | 510 |     my $options = ($use_tidy =~ /^-/) ? $use_tidy : undef; | 
|---|
 | 511 |      | 
|---|
 | 512 |     my $tidy_code; | 
|---|
 | 513 |     Perl::Tidy::perltidy(source => \$source_code, destination => \$tidy_code, argv => $options || $TIDY_OPTIONS); | 
|---|
 | 514 |      | 
|---|
 | 515 |     return $tidy_code; | 
|---|
 | 516 | } | 
|---|
 | 517 |  | 
|---|
 | 518 |  | 
|---|
| [39] | 519 | sub _write_output_file { | 
|---|
| [82] | 520 |     my ($source_code, $outfile) = @_;     | 
|---|
 | 521 |     open OUT, "> $outfile" or croak '[' . (caller(1))[3] . "] Can't open $outfile for writing: $!"; | 
|---|
 | 522 |     print OUT $source_code; | 
|---|
 | 523 |     close OUT; | 
|---|
| [12] | 524 | } | 
|---|
 | 525 |  | 
|---|
| [39] | 526 |  | 
|---|
| [16] | 527 | sub form { | 
|---|
 | 528 |     my $self = shift; | 
|---|
 | 529 |      | 
|---|
 | 530 |     # automatically call build if needed to | 
|---|
 | 531 |     # allow the new->parse->write shortcut | 
|---|
 | 532 |     $self->build unless $self->{built}; | 
|---|
| [1] | 533 |  | 
|---|
| [16] | 534 |     return $self->{form}; | 
|---|
 | 535 | } | 
|---|
 | 536 |  | 
|---|
| [12] | 537 | sub _form_template { | 
|---|
| [33] | 538 |     my $self = shift; | 
|---|
| [34] | 539 |     my $msg_required = $self->{build_options}{messages}{text_required}; | 
|---|
 | 540 |     my $msg_invalid = $self->{build_options}{messages}{text_invalid}; | 
|---|
 | 541 |     return q{<% $description ? qq[<p id="description">$description</p>] : '' %> | 
|---|
 | 542 | <% (grep { $_->{required} } @fields) ? qq[<p id="instructions">} . $msg_required . q{</p>] : '' %> | 
|---|
| [12] | 543 | <% $start %> | 
|---|
| [23] | 544 | <% | 
|---|
 | 545 |     # drop in the hidden fields here | 
|---|
 | 546 |     $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); | 
|---|
| [34] | 547 | %>} . | 
|---|
 | 548 | q[ | 
|---|
| [29] | 549 | <% | 
|---|
 | 550 |     SECTION: while (my $section = shift @sections) { | 
|---|
| [67] | 551 |         $OUT .= qq[<fieldset>\n]; | 
|---|
 | 552 |         $OUT .= qq[  <legend>$$section{head}</legend>] if $$section{head}; | 
|---|
| [30] | 553 |         $OUT .= qq[<table id="] . ($$section{id} || '_default') . qq[">\n]; | 
|---|
| [67] | 554 |         #$OUT .= qq[  <caption><h2 class="sectionhead">$$section{head}</h2></caption>] if $$section{head}; | 
|---|
| [29] | 555 |         TABLE_LINE: for my $line (@{ $$section{lines} }) { | 
|---|
 | 556 |             if ($$line[0] eq 'head') { | 
|---|
| [71] | 557 |                 $OUT .= qq[  <tr><th class="subhead" colspan="2"><h2>$$line[1]</h2></th></tr>\n] | 
|---|
| [50] | 558 |             } elsif ($$line[0] eq 'note') { | 
|---|
 | 559 |                 $OUT .= qq[  <tr><td class="note" colspan="2">$$line[1]</td></tr>\n] | 
|---|
| [29] | 560 |             } elsif ($$line[0] eq 'field') { | 
|---|
| [42] | 561 |                 local $_ = $field{$$line[1]}; | 
|---|
| [34] | 562 |                  | 
|---|
| [29] | 563 |                 # skip hidden fields in the table | 
|---|
 | 564 |                 next TABLE_LINE if $$_{type} eq 'hidden'; | 
|---|
 | 565 |                  | 
|---|
 | 566 |                 $OUT .= $$_{invalid} ? qq[  <tr class="invalid">] : qq[  <tr>]; | 
|---|
| [30] | 567 |                  | 
|---|
 | 568 |                 # special case single value checkboxes | 
|---|
 | 569 |                 if ($$_{type} eq 'checkbox' && @{ $$_{options} } == 1) { | 
|---|
| [82] | 570 |                     $OUT .= qq[<td></td>]; | 
|---|
| [30] | 571 |                 } else { | 
|---|
| [83] | 572 |                     $OUT .= '<td class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}</strong>] : "$$_{label}") . '</td>'; | 
|---|
| [30] | 573 |                 } | 
|---|
| [33] | 574 |                  | 
|---|
 | 575 |                 # mark invalid fields | 
|---|
| [29] | 576 |                 if ($$_{invalid}) { | 
|---|
| [81] | 577 |                     $OUT .= qq[<td>$$_{field} <span class="comment">$$_{comment}</span> $$_{error}</td>]; | 
|---|
| [29] | 578 |                 } else { | 
|---|
| [73] | 579 |                     $OUT .= qq[<td>$$_{field} <span class="comment">$$_{comment}</span></td>]; | 
|---|
| [29] | 580 |                 } | 
|---|
| [33] | 581 |                  | 
|---|
| [30] | 582 |                 $OUT .= qq[</tr>\n]; | 
|---|
 | 583 |                  | 
|---|
| [29] | 584 |             } elsif ($$line[0] eq 'group') { | 
|---|
| [42] | 585 |                 my @group_fields = map { $field{$_} } map { $$_{name} } @{ $$line[1]{group} }; | 
|---|
| [29] | 586 |                 $OUT .= (grep { $$_{invalid} } @group_fields) ? qq[  <tr class="invalid">\n] : qq[  <tr>\n]; | 
|---|
 | 587 |                  | 
|---|
| [82] | 588 |                 $OUT .= '    <td class="label">'; | 
|---|
| [83] | 589 |                 $OUT .= (grep { $$_{required} } @group_fields) ? qq[<strong class="required">$$line[1]{label}</strong>] : "$$line[1]{label}"; | 
|---|
| [82] | 590 |                 $OUT .= qq[</td>\n]; | 
|---|
| [29] | 591 |                  | 
|---|
| [59] | 592 |                 $OUT .= qq[    <td><span class="fieldgroup">]; | 
|---|
| [29] | 593 |                 $OUT .= join(' ', map { qq[<small class="sublabel">$$_{label}</small> $$_{field} $$_{comment}] } @group_fields); | 
|---|
| [81] | 594 |                 if (my @invalid = grep { $$_{invalid} } @group_fields) { | 
|---|
 | 595 |                     $OUT .= ' ' . join('; ', map { $$_{error} } @invalid); | 
|---|
 | 596 |                 }                 | 
|---|
| [73] | 597 |                 $OUT .= qq[ <span class="comment">$$line[1]{comment}</span></span></td>\n]; | 
|---|
| [29] | 598 |                 $OUT .= qq[  </tr>\n]; | 
|---|
 | 599 |             }    | 
|---|
| [21] | 600 |         } | 
|---|
| [29] | 601 |         # close the table if there are sections remaining | 
|---|
 | 602 |         # but leave the last one open for the submit button | 
|---|
| [67] | 603 |         if (@sections) { | 
|---|
 | 604 |             $OUT .= qq[</table>\n]; | 
|---|
 | 605 |             $OUT .= qq[</fieldset>\n]; | 
|---|
 | 606 |         } | 
|---|
| [29] | 607 |     } | 
|---|
 | 608 | %> | 
|---|
| [81] | 609 |   <tr><th></th><td style="padding-top: 1em;"><% $submit %> <% $reset %></td></tr> | 
|---|
| [12] | 610 | </table> | 
|---|
| [67] | 611 | </fieldset> | 
|---|
| [12] | 612 | <% $end %> | 
|---|
 | 613 | ]; | 
|---|
 | 614 | } | 
|---|
 | 615 |  | 
|---|
| [34] | 616 | # usage: $self->_pre_template($css, $charset) | 
|---|
| [33] | 617 | sub _pre_template { | 
|---|
| [12] | 618 |     my $self = shift; | 
|---|
| [30] | 619 |     my $css = shift || $DEFAULT_CSS; | 
|---|
| [34] | 620 |     my $charset = shift || $DEFAULT_CHARSET; | 
|---|
 | 621 |     my $msg_author = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_author}) . '", $author)'; | 
|---|
| [33] | 622 |     return  | 
|---|
| [12] | 623 | q[<html> | 
|---|
| [1] | 624 | <head> | 
|---|
| [34] | 625 |   <meta http-equiv="Content-Type" content="text/html; charset=] . $charset . q[" /> | 
|---|
| [1] | 626 |   <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> | 
|---|
| [33] | 627 |   <style type="text/css"> | 
|---|
| [42] | 628 | ] . $css . q[  </style> | 
|---|
| [33] | 629 |   <% $jshead %> | 
|---|
| [1] | 630 | </head> | 
|---|
 | 631 | <body> | 
|---|
 | 632 |  | 
|---|
 | 633 | <h1><% $title %></h1> | 
|---|
| [34] | 634 | <% $author ? qq[<p id="author">] . ] . $msg_author . q{ . q[</p>] : '' %> | 
|---|
 | 635 | }; | 
|---|
| [33] | 636 | } | 
|---|
 | 637 |  | 
|---|
 | 638 | sub _post_template { | 
|---|
| [34] | 639 |     my $self = shift; | 
|---|
 | 640 |     my $msg_madewith = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_madewith}) . | 
|---|
 | 641 |         '", q[<a href="http://formbuilder.org/">CGI::FormBuilder</a>], CGI::FormBuilder->VERSION)'; | 
|---|
 | 642 |      | 
|---|
 | 643 |     return qq[<hr /> | 
|---|
| [1] | 644 | <div id="footer"> | 
|---|
| [34] | 645 |   <p id="creator"><% $msg_madewith %></p> | 
|---|
| [1] | 646 | </div> | 
|---|
 | 647 | </body> | 
|---|
 | 648 | </html> | 
|---|
 | 649 | ]; | 
|---|
 | 650 | } | 
|---|
 | 651 |  | 
|---|
| [42] | 652 | # usage: $self->_template($css, $charset) | 
|---|
| [33] | 653 | sub _template { | 
|---|
 | 654 |     my $self = shift; | 
|---|
| [34] | 655 |     return $self->_pre_template(@_) . $self->_form_template . $self->_post_template; | 
|---|
| [33] | 656 | } | 
|---|
 | 657 |  | 
|---|
| [7] | 658 | sub dump {  | 
|---|
 | 659 |     eval "use YAML;"; | 
|---|
 | 660 |     unless ($@) { | 
|---|
 | 661 |         print YAML::Dump(shift->{form_spec}); | 
|---|
 | 662 |     } else { | 
|---|
| [53] | 663 |         warn '[' . (caller(0))[3] . "] Can't dump form spec structure using YAML: $@"; | 
|---|
| [7] | 664 |     } | 
|---|
 | 665 | } | 
|---|
| [1] | 666 |  | 
|---|
 | 667 |  | 
|---|
 | 668 | # module return | 
|---|
 | 669 | 1; | 
|---|
 | 670 |  | 
|---|
 | 671 | =head1 NAME | 
|---|
 | 672 |  | 
|---|
| [21] | 673 | Text::FormBuilder - Create CGI::FormBuilder objects from simple text descriptions | 
|---|
| [1] | 674 |  | 
|---|
 | 675 | =head1 SYNOPSIS | 
|---|
 | 676 |  | 
|---|
| [16] | 677 |     use Text::FormBuilder; | 
|---|
 | 678 |      | 
|---|
| [1] | 679 |     my $parser = Text::FormBuilder->new; | 
|---|
 | 680 |     $parser->parse($src_file); | 
|---|
 | 681 |      | 
|---|
| [16] | 682 |     # returns a new CGI::FormBuilder object with | 
|---|
 | 683 |     # the fields from the input form spec | 
|---|
| [7] | 684 |     my $form = $parser->form; | 
|---|
| [19] | 685 |      | 
|---|
 | 686 |     # write a My::Form module to Form.pm | 
|---|
 | 687 |     $parser->write_module('My::Form'); | 
|---|
| [1] | 688 |  | 
|---|
| [23] | 689 | =head1 REQUIRES | 
|---|
 | 690 |  | 
|---|
| [56] | 691 | L<Parse::RecDescent>, | 
|---|
 | 692 | L<CGI::FormBuilder>, | 
|---|
 | 693 | L<Text::Template>, | 
|---|
 | 694 | L<Class::Base> | 
|---|
| [23] | 695 |  | 
|---|
| [87] | 696 | You will also need L<YAML>, if you want to use the L<C<dump>|/dump> | 
|---|
 | 697 | method, or the L<C<!fb>|/!fb> directive in your formspec files. | 
|---|
 | 698 |  | 
|---|
| [1] | 699 | =head1 DESCRIPTION | 
|---|
 | 700 |  | 
|---|
| [38] | 701 | This module is intended to extend the idea of making it easy to create | 
|---|
 | 702 | web forms by allowing you to describe them with a simple langauge. These | 
|---|
 | 703 | I<formspecs> are then passed through this module's parser and converted | 
|---|
 | 704 | into L<CGI::FormBuilder> objects that you can easily use in your CGI | 
|---|
 | 705 | scripts. In addition, this module can generate code for standalone modules | 
|---|
 | 706 | which allow you to separate your form design from your script code. | 
|---|
 | 707 |  | 
|---|
 | 708 | A simple formspec looks like this: | 
|---|
 | 709 |  | 
|---|
 | 710 |     name//VALUE | 
|---|
 | 711 |     email//EMAIL | 
|---|
 | 712 |     langauge:select{English,Spanish,French,German} | 
|---|
 | 713 |     moreinfo|Send me more information:checkbox | 
|---|
 | 714 |     interests:checkbox{Perl,karate,bass guitar} | 
|---|
 | 715 |  | 
|---|
 | 716 | This will produce a required C<name> test field, a required C<email> text | 
|---|
 | 717 | field that must look like an email address, an optional select dropdown | 
|---|
 | 718 | field C<langauge> with the choices English, Spanish, French, and German, | 
|---|
 | 719 | an optional C<moreinfo> checkbox labeled ``Send me more information'', and | 
|---|
 | 720 | finally a set of checkboxes named C<interests> with the choices Perl, | 
|---|
 | 721 | karate, and bass guitar. | 
|---|
 | 722 |  | 
|---|
| [46] | 723 | =head1 METHODS | 
|---|
 | 724 |  | 
|---|
| [1] | 725 | =head2 new | 
|---|
 | 726 |  | 
|---|
| [38] | 727 |     my $parser = Text::FormBuilder->new; | 
|---|
 | 728 |  | 
|---|
| [1] | 729 | =head2 parse | 
|---|
 | 730 |  | 
|---|
| [42] | 731 |     # parse a file (regular scalar) | 
|---|
| [19] | 732 |     $parser->parse($filename); | 
|---|
| [7] | 733 |      | 
|---|
| [19] | 734 |     # or pass a scalar ref for parse a literal string | 
|---|
 | 735 |     $parser->parse(\$string); | 
|---|
| [42] | 736 |      | 
|---|
 | 737 |     # or an array ref to parse lines | 
|---|
 | 738 |     $parser->parse(\@lines); | 
|---|
| [19] | 739 |  | 
|---|
| [42] | 740 | Parse the file or string. Returns the parser object. This method, | 
|---|
 | 741 | along with all of its C<parse_*> siblings, may be called as a class | 
|---|
 | 742 | method to construct a new object. | 
|---|
| [19] | 743 |  | 
|---|
 | 744 | =head2 parse_file | 
|---|
 | 745 |  | 
|---|
 | 746 |     $parser->parse_file($src_file); | 
|---|
 | 747 |      | 
|---|
| [7] | 748 |     # or as a class method | 
|---|
| [16] | 749 |     my $parser = Text::FormBuilder->parse($src_file); | 
|---|
| [7] | 750 |  | 
|---|
 | 751 | =head2 parse_text | 
|---|
 | 752 |  | 
|---|
| [16] | 753 |     $parser->parse_text($src); | 
|---|
 | 754 |  | 
|---|
| [19] | 755 | Parse the given C<$src> text. Returns the parser object. | 
|---|
| [16] | 756 |  | 
|---|
| [42] | 757 | =head2 parse_array | 
|---|
 | 758 |  | 
|---|
 | 759 |     $parser->parse_array(@lines); | 
|---|
 | 760 |  | 
|---|
 | 761 | Concatenates and parses C<@lines>. Returns the parser object. | 
|---|
 | 762 |  | 
|---|
| [1] | 763 | =head2 build | 
|---|
 | 764 |  | 
|---|
| [12] | 765 |     $parser->build(%options); | 
|---|
| [7] | 766 |  | 
|---|
| [12] | 767 | Builds the CGI::FormBuilder object. Options directly used by C<build> are: | 
|---|
 | 768 |  | 
|---|
 | 769 | =over | 
|---|
 | 770 |  | 
|---|
| [19] | 771 | =item C<form_only> | 
|---|
| [12] | 772 |  | 
|---|
 | 773 | Only uses the form portion of the template, and omits the surrounding html, | 
|---|
| [19] | 774 | title, author, and the standard footer. This does, however, include the | 
|---|
 | 775 | description as specified with the C<!description> directive. | 
|---|
| [12] | 776 |  | 
|---|
| [30] | 777 | =item C<css>, C<extra_css> | 
|---|
 | 778 |  | 
|---|
 | 779 | These options allow you to tell Text::FormBuilder to use different | 
|---|
 | 780 | CSS styles for the built in template. A value given a C<css> will | 
|---|
 | 781 | replace the existing CSS, and a value given as C<extra_css> will be | 
|---|
 | 782 | appended to the CSS. If both options are given, then the CSS that is | 
|---|
 | 783 | used will be C<css> concatenated with C<extra_css>. | 
|---|
 | 784 |  | 
|---|
| [38] | 785 | If you want to use an external stylesheet, a quick way to get this is | 
|---|
 | 786 | to set the C<css> parameter to import your file: | 
|---|
 | 787 |  | 
|---|
 | 788 |     css => '@import(my_external_stylesheet.css);' | 
|---|
 | 789 |  | 
|---|
| [72] | 790 | =item C<external_css> | 
|---|
 | 791 |  | 
|---|
 | 792 | If you want to use multiple external stylesheets, or an external stylesheet | 
|---|
 | 793 | in conojunction with the default styles, use the C<external_css> option: | 
|---|
 | 794 |  | 
|---|
 | 795 |     # single external sheet | 
|---|
 | 796 |     external_css => 'my_styles.css' | 
|---|
 | 797 |      | 
|---|
 | 798 |     # mutliple sheets | 
|---|
 | 799 |     external_css => [ | 
|---|
 | 800 |         'my_style_A.css', | 
|---|
 | 801 |         'my_style_B.css', | 
|---|
 | 802 |     ] | 
|---|
 | 803 |  | 
|---|
| [34] | 804 | =item C<messages> | 
|---|
 | 805 |  | 
|---|
 | 806 | This works the same way as the C<messages> parameter to  | 
|---|
 | 807 | C<< CGI::FormBuilder->new >>; you can provide either a hashref of messages | 
|---|
 | 808 | or a filename. | 
|---|
 | 809 |  | 
|---|
 | 810 | The default messages used by Text::FormBuilder are: | 
|---|
 | 811 |  | 
|---|
 | 812 |     text_author       Created by %s | 
|---|
 | 813 |     text_madewith     Made with %s version %s | 
|---|
 | 814 |     text_required     (Required fields are marked in <strong>bold</strong>.) | 
|---|
 | 815 |     text_invalid      Missing or invalid value. | 
|---|
 | 816 |  | 
|---|
 | 817 | Any messages you set here get passed on to CGI::FormBuilder, which means | 
|---|
 | 818 | that you should be able to put all of your customization messages in one | 
|---|
 | 819 | big file. | 
|---|
 | 820 |  | 
|---|
 | 821 | =item C<charset> | 
|---|
 | 822 |  | 
|---|
 | 823 | Sets the character encoding for the generated page. The default is ISO-8859-1. | 
|---|
 | 824 |  | 
|---|
| [12] | 825 | =back | 
|---|
 | 826 |  | 
|---|
 | 827 | All other options given to C<build> are passed on verbatim to the | 
|---|
 | 828 | L<CGI::FormBuilder> constructor. Any options given here override the | 
|---|
 | 829 | defaults that this module uses. | 
|---|
 | 830 |  | 
|---|
| [16] | 831 | The C<form>, C<write>, and C<write_module> methods will all call | 
|---|
 | 832 | C<build> with no options for you if you do not do so explicitly. | 
|---|
 | 833 | This allows you to say things like this: | 
|---|
 | 834 |  | 
|---|
 | 835 |     my $form = Text::FormBuilder->new->parse('formspec.txt')->form; | 
|---|
 | 836 |  | 
|---|
 | 837 | However, if you need to specify options to C<build>, you must call it | 
|---|
 | 838 | explictly after C<parse>. | 
|---|
 | 839 |  | 
|---|
| [7] | 840 | =head2 form | 
|---|
 | 841 |  | 
|---|
 | 842 |     my $form = $parser->form; | 
|---|
 | 843 |  | 
|---|
| [16] | 844 | Returns the L<CGI::FormBuilder> object. Remember that you can modify | 
|---|
 | 845 | this object directly, in order to (for example) dynamically populate | 
|---|
 | 846 | dropdown lists or change input types at runtime. | 
|---|
| [7] | 847 |  | 
|---|
| [1] | 848 | =head2 write | 
|---|
 | 849 |  | 
|---|
| [7] | 850 |     $parser->write($out_file); | 
|---|
 | 851 |     # or just print to STDOUT | 
|---|
 | 852 |     $parser->write; | 
|---|
 | 853 |  | 
|---|
| [29] | 854 | Calls C<render> on the FormBuilder form, and either writes the resulting | 
|---|
 | 855 | HTML to a file, or to STDOUT if no filename is given. | 
|---|
| [7] | 856 |  | 
|---|
| [82] | 857 | =head2 as_module | 
|---|
 | 858 |  | 
|---|
 | 859 |     my $module_code = $parser->as_module($package, $use_tidy); | 
|---|
 | 860 |  | 
|---|
| [12] | 861 | =head2 write_module | 
|---|
 | 862 |  | 
|---|
| [70] | 863 | I<B<Note:> The code output from the C<write_*> methods may be in flux for | 
|---|
 | 864 | the next few versions, as I coordinate with the B<FormBuilder> project.> | 
|---|
 | 865 |  | 
|---|
| [16] | 866 |     $parser->write_module($package, $use_tidy); | 
|---|
| [12] | 867 |  | 
|---|
 | 868 | Takes a package name, and writes out a new module that can be used by your | 
|---|
 | 869 | CGI script to render the form. This way, you only need CGI::FormBuilder on | 
|---|
 | 870 | your server, and you don't have to parse the form spec each time you want  | 
|---|
| [74] | 871 | to display your form. The generated module is a subclass of L<CGI::FormBuilder>, | 
|---|
| [77] | 872 | that will pass along any constructor arguments to FormBuilder, and set up | 
|---|
| [74] | 873 | the fields for you. | 
|---|
| [12] | 874 |  | 
|---|
| [16] | 875 | First, you parse the formspec and write the module, which you can do as a one-liner: | 
|---|
 | 876 |  | 
|---|
| [19] | 877 |     $ perl -MText::FormBuilder -e"Text::FormBuilder->parse('formspec.txt')->write_module('My::Form')" | 
|---|
| [16] | 878 |  | 
|---|
| [74] | 879 | And then, in your CGI script, use the new module: | 
|---|
| [16] | 880 |  | 
|---|
| [12] | 881 |     #!/usr/bin/perl -w | 
|---|
 | 882 |     use strict; | 
|---|
 | 883 |      | 
|---|
 | 884 |     use CGI; | 
|---|
| [19] | 885 |     use My::Form; | 
|---|
| [12] | 886 |      | 
|---|
 | 887 |     my $q = CGI->new; | 
|---|
| [74] | 888 |     my $form = My::Form->new; | 
|---|
| [12] | 889 |      | 
|---|
 | 890 |     # do the standard CGI::FormBuilder stuff | 
|---|
 | 891 |     if ($form->submitted && $form->validate) { | 
|---|
 | 892 |         # process results | 
|---|
 | 893 |     } else { | 
|---|
 | 894 |         print $q->header; | 
|---|
 | 895 |         print $form->render; | 
|---|
 | 896 |     } | 
|---|
 | 897 |  | 
|---|
| [16] | 898 | If you pass a true value as the second argument to C<write_module>, the parser | 
|---|
 | 899 | will run L<Perl::Tidy> on the generated code before writing the module file. | 
|---|
 | 900 |  | 
|---|
| [19] | 901 |     # write tidier code | 
|---|
 | 902 |     $parser->write_module('My::Form', 1); | 
|---|
 | 903 |  | 
|---|
| [82] | 904 | If you set C<$use_tidy> to a string beginning with `-' C<write_module> will | 
|---|
 | 905 | interpret C<$use_tidy> as the formatting option switches to pass to Perl::Tidy. | 
|---|
 | 906 |  | 
|---|
 | 907 | =head2 as_script | 
|---|
 | 908 |  | 
|---|
 | 909 |     my $script_code = $parser->as_script($use_tidy); | 
|---|
 | 910 |  | 
|---|
| [39] | 911 | =head2 write_script | 
|---|
 | 912 |  | 
|---|
 | 913 |     $parser->write_script($filename, $use_tidy); | 
|---|
 | 914 |  | 
|---|
 | 915 | If you don't need the reuseability of a separate module, you can have | 
|---|
 | 916 | Text::FormBuilder write the form object to a script for you, along with | 
|---|
 | 917 | the simplest framework for using it, to which you can add your actual | 
|---|
 | 918 | form processing code. | 
|---|
 | 919 |  | 
|---|
 | 920 | The generated script looks like this: | 
|---|
 | 921 |  | 
|---|
| [74] | 922 |     #!/usr/bin/perl | 
|---|
 | 923 |     use strict; | 
|---|
 | 924 |     use warnings; | 
|---|
| [39] | 925 |      | 
|---|
| [74] | 926 |     use CGI::FormBuilder; | 
|---|
 | 927 |      | 
|---|
 | 928 |     my $form = CGI::FormBuilder->new( | 
|---|
 | 929 |         # lots of stuff here... | 
|---|
 | 930 |     ); | 
|---|
 | 931 |      | 
|---|
 | 932 |     # ...and your field setup subs are here | 
|---|
 | 933 |     $form->field(name => '...'); | 
|---|
 | 934 |          | 
|---|
 | 935 |     unless ($form->submitted && $form->validate) { | 
|---|
 | 936 |         print $form->render; | 
|---|
 | 937 |     } else { | 
|---|
 | 938 |         # do something with the entered data | 
|---|
| [39] | 939 |     } | 
|---|
 | 940 |  | 
|---|
 | 941 | Like C<write_module>, you can optionally pass a true value as the second | 
|---|
 | 942 | argument to have Perl::Tidy make the generated code look nicer. | 
|---|
 | 943 |  | 
|---|
| [7] | 944 | =head2 dump | 
|---|
 | 945 |  | 
|---|
| [16] | 946 | Uses L<YAML> to print out a human-readable representation of the parsed | 
|---|
| [7] | 947 | form spec. | 
|---|
 | 948 |  | 
|---|
| [46] | 949 | =head1 EXPORTS | 
|---|
 | 950 |  | 
|---|
 | 951 | There is one exported function, C<create_form>, that is intended to ``do the | 
|---|
 | 952 | right thing'' in simple cases. | 
|---|
 | 953 |  | 
|---|
 | 954 | =head2 create_form | 
|---|
 | 955 |  | 
|---|
 | 956 |     # get a CGI::FormBuilder object | 
|---|
 | 957 |     my $form = create_form($source, $options, $destination); | 
|---|
 | 958 |      | 
|---|
 | 959 |     # or just write the form immediately | 
|---|
 | 960 |     create_form($source, $options, $destination); | 
|---|
 | 961 |  | 
|---|
 | 962 | C<$source> accepts any of the types of arguments that C<parse> does. C<$options> | 
|---|
 | 963 | is a hashref of options that should be passed to C<build>. Finally, C<$destination> | 
|---|
 | 964 | is a simple scalar that determines where and what type of output C<create_form> | 
|---|
 | 965 | should generate. | 
|---|
 | 966 |  | 
|---|
 | 967 |     /\.pm$/             ->write_module($destination) | 
|---|
 | 968 |     /\.(cgi|pl)$/       ->write_script($destination) | 
|---|
 | 969 |     everything else     ->write($destination) | 
|---|
 | 970 |  | 
|---|
 | 971 | For anything more than simple, one-off cases, you are usually better off using the | 
|---|
 | 972 | object-oriented interface, since that gives you more control over things. | 
|---|
 | 973 |  | 
|---|
| [33] | 974 | =head1 DEFAULTS | 
|---|
 | 975 |  | 
|---|
 | 976 | These are the default settings that are passed to C<< CGI::FormBuilder->new >>: | 
|---|
 | 977 |  | 
|---|
 | 978 |     method => 'GET' | 
|---|
 | 979 |     keepextras => 1 | 
|---|
 | 980 |  | 
|---|
 | 981 | Any of these can be overriden by the C<build> method: | 
|---|
 | 982 |  | 
|---|
 | 983 |     # use POST instead | 
|---|
 | 984 |     $parser->build(method => 'POST')->write; | 
|---|
 | 985 |  | 
|---|
| [1] | 986 | =head1 LANGUAGE | 
|---|
 | 987 |  | 
|---|
| [74] | 988 |     # name field_size growable label hint type other default option_list validate | 
|---|
 | 989 |      | 
|---|
| [19] | 990 |     field_name[size]|descriptive label[hint]:type=default{option1[display string],...}//validate | 
|---|
| [1] | 991 |      | 
|---|
 | 992 |     !title ... | 
|---|
 | 993 |      | 
|---|
| [12] | 994 |     !author ... | 
|---|
 | 995 |      | 
|---|
| [16] | 996 |     !description { | 
|---|
 | 997 |         ... | 
|---|
 | 998 |     } | 
|---|
 | 999 |      | 
|---|
| [42] | 1000 |     !pattern NAME /regular expression/ | 
|---|
| [16] | 1001 |      | 
|---|
| [42] | 1002 |     !list NAME { | 
|---|
| [7] | 1003 |         option1[display string], | 
|---|
 | 1004 |         option2[display string], | 
|---|
| [1] | 1005 |         ... | 
|---|
 | 1006 |     } | 
|---|
| [12] | 1007 |      | 
|---|
| [42] | 1008 |     !group NAME { | 
|---|
 | 1009 |         field1 | 
|---|
 | 1010 |         field2 | 
|---|
 | 1011 |         ... | 
|---|
 | 1012 |     } | 
|---|
 | 1013 |      | 
|---|
| [29] | 1014 |     !section id heading | 
|---|
 | 1015 |      | 
|---|
| [12] | 1016 |     !head ... | 
|---|
| [50] | 1017 |      | 
|---|
 | 1018 |     !note { | 
|---|
 | 1019 |         ... | 
|---|
 | 1020 |     } | 
|---|
| [80] | 1021 |      | 
|---|
| [81] | 1022 |     !submit label, label 2, ... | 
|---|
 | 1023 |      | 
|---|
 | 1024 |     !reset label | 
|---|
| [1] | 1025 |  | 
|---|
 | 1026 | =head2 Directives | 
|---|
 | 1027 |  | 
|---|
| [87] | 1028 | All directives start with a C<!> followed by a keyword. There are two types of | 
|---|
 | 1029 | directives: | 
|---|
 | 1030 |  | 
|---|
| [1] | 1031 | =over | 
|---|
 | 1032 |  | 
|---|
| [87] | 1033 | =item Line directives | 
|---|
 | 1034 |  | 
|---|
 | 1035 | Line directives occur all on one line, and require no special punctuation. Examples | 
|---|
 | 1036 | of line directives are L<C<!title>|/!title> and L<C<!section>|/!section>. | 
|---|
 | 1037 |  | 
|---|
 | 1038 | =item Block directives | 
|---|
 | 1039 |  | 
|---|
 | 1040 | Block directives consist of a directive keyword followed by a curly-brace delimited | 
|---|
 | 1041 | block. Examples of these are L<C<!group>|/!group> and L<C<!description>|/!description>. | 
|---|
 | 1042 | Some of these directives have their own internal structure; see the list of directives | 
|---|
 | 1043 | below for an explanation. | 
|---|
 | 1044 |  | 
|---|
 | 1045 | =back | 
|---|
 | 1046 |  | 
|---|
 | 1047 | And here is the complete list of directives | 
|---|
 | 1048 |  | 
|---|
 | 1049 | =over | 
|---|
 | 1050 |  | 
|---|
| [1] | 1051 | =item C<!pattern> | 
|---|
 | 1052 |  | 
|---|
| [12] | 1053 | Defines a validation pattern. | 
|---|
 | 1054 |  | 
|---|
| [1] | 1055 | =item C<!list> | 
|---|
 | 1056 |  | 
|---|
| [12] | 1057 | Defines a list for use in a C<radio>, C<checkbox>, or C<select> field. | 
|---|
 | 1058 |  | 
|---|
| [42] | 1059 | =item C<!group> | 
|---|
 | 1060 |  | 
|---|
 | 1061 | Define a named group of fields that are displayed all on one line. Use with | 
|---|
 | 1062 | the C<!field> directive. | 
|---|
 | 1063 |  | 
|---|
 | 1064 | =item C<!field> | 
|---|
 | 1065 |  | 
|---|
| [87] | 1066 | B<DEPRECATED> Include a named instance of a group defined with C<!group>. See | 
|---|
 | 1067 | L<Field Groups|/Field Groups> for an explanation of the new way to include | 
|---|
 | 1068 | groups. | 
|---|
| [42] | 1069 |  | 
|---|
| [1] | 1070 | =item C<!title> | 
|---|
 | 1071 |  | 
|---|
| [87] | 1072 | Line directive contianing the title of the form. | 
|---|
| [50] | 1073 |  | 
|---|
| [7] | 1074 | =item C<!author> | 
|---|
 | 1075 |  | 
|---|
| [87] | 1076 | Line directive naming the author of the form. | 
|---|
| [50] | 1077 |  | 
|---|
| [16] | 1078 | =item C<!description> | 
|---|
 | 1079 |  | 
|---|
| [87] | 1080 | A block directive containing a brief description of the form. Suitable for  | 
|---|
 | 1081 | special instructions on how to fill out the form. All of the text within the | 
|---|
 | 1082 | block is folded into a single paragraph. | 
|---|
| [19] | 1083 |  | 
|---|
| [29] | 1084 | =item C<!section> | 
|---|
 | 1085 |  | 
|---|
| [87] | 1086 | A line directive that starts a new section. Each section has its own heading | 
|---|
 | 1087 | and id, which by default are rendered into spearate tables. | 
|---|
| [29] | 1088 |  | 
|---|
| [12] | 1089 | =item C<!head> | 
|---|
 | 1090 |  | 
|---|
| [87] | 1091 | A line directive that inserts a heading between two fields. There can only be | 
|---|
 | 1092 | one heading between any two fields; the parser will warn you if you try to put | 
|---|
 | 1093 | two headings right next to each other. | 
|---|
| [12] | 1094 |  | 
|---|
| [50] | 1095 | =item C<!note> | 
|---|
 | 1096 |  | 
|---|
| [87] | 1097 | A block directive containing a text note that can be inserted as a row in the | 
|---|
 | 1098 | form. This is useful for special instructions at specific points in a long form. | 
|---|
 | 1099 | Like L<C<!description>|/!description>, the text content is folded into a single | 
|---|
 | 1100 | paragraph. | 
|---|
| [50] | 1101 |  | 
|---|
| [80] | 1102 | =item C<!submit> | 
|---|
 | 1103 |  | 
|---|
| [87] | 1104 | A line directive with  one or more submit button labels in a comma-separated list. | 
|---|
 | 1105 | Each label is a L<string|/Strings>. Multiple instances of this directive may be | 
|---|
 | 1106 | used; later lists are simply appended to the earlier lists. All the submit buttons | 
|---|
 | 1107 | are rendered together at the bottom of the form. See L<CGI::FormBuilder> for an | 
|---|
| [80] | 1108 | explanation of how the multiple submit buttons work together in a form. | 
|---|
 | 1109 |  | 
|---|
| [81] | 1110 | To disable the display of any submit button, use C<!submit 0> | 
|---|
 | 1111 |  | 
|---|
 | 1112 | =item C<!reset> | 
|---|
 | 1113 |  | 
|---|
| [87] | 1114 | Line directive giving a label for the a reset button at the end of the form. No  | 
|---|
 | 1115 | reset button will be rendered unless you use this directive. | 
|---|
| [81] | 1116 |  | 
|---|
| [87] | 1117 | =item C<!fb> | 
|---|
 | 1118 |  | 
|---|
 | 1119 | The C<!fb> block directive allows you to include any parameters you want passed | 
|---|
 | 1120 | directly to the CGI::FormBuilder constructor. The block should be a hashref of | 
|---|
 | 1121 | parameters serialized as L<YAML>. Be sure to place the closing of the block on | 
|---|
 | 1122 | its own line, flush to the left edge, and to watch your indentation. Multiple | 
|---|
 | 1123 | C<!fb> blocks are concatenated, and the result is interpeted as one big chunk | 
|---|
 | 1124 | of YAML code. | 
|---|
 | 1125 |  | 
|---|
 | 1126 |     !fb{ | 
|---|
 | 1127 |     method: POST | 
|---|
 | 1128 |     action: '/custom/action' | 
|---|
 | 1129 |     javascript: 0 | 
|---|
 | 1130 |     } | 
|---|
 | 1131 |  | 
|---|
| [1] | 1132 | =back | 
|---|
 | 1133 |  | 
|---|
| [74] | 1134 | =head2 Strings | 
|---|
| [1] | 1135 |  | 
|---|
| [87] | 1136 | Anywhere that it says that you may use a multiword string, this means you can | 
|---|
 | 1137 | do one of two things. For strings that consist solely of alphanumeric characters  | 
|---|
 | 1138 | (i.e. C<\w+>) and spaces, the string will be recognized as is: | 
|---|
| [1] | 1139 |  | 
|---|
| [24] | 1140 |     field_1|A longer label | 
|---|
 | 1141 |  | 
|---|
 | 1142 | If you want to include non-alphanumerics (e.g. punctuation), you must  | 
|---|
 | 1143 | single-quote the string: | 
|---|
 | 1144 |  | 
|---|
 | 1145 |     field_2|'Dept./Org.' | 
|---|
 | 1146 |  | 
|---|
 | 1147 | To include a literal single-quote in a single-quoted string, escape it with | 
|---|
 | 1148 | a backslash: | 
|---|
 | 1149 |  | 
|---|
 | 1150 |     field_3|'\'Official\' title' | 
|---|
 | 1151 |  | 
|---|
| [71] | 1152 | Quoted strings are also how you can set the label for a field to be blank: | 
|---|
 | 1153 |  | 
|---|
 | 1154 |     unlabeled_field|'' | 
|---|
 | 1155 |  | 
|---|
| [74] | 1156 | =head2 Fields | 
|---|
| [24] | 1157 |  | 
|---|
| [74] | 1158 | Form fields are each described on a single line. The simplest field is | 
|---|
 | 1159 | just a name (which cannot contain any whitespace): | 
|---|
 | 1160 |  | 
|---|
| [19] | 1161 |     color | 
|---|
 | 1162 |  | 
|---|
 | 1163 | This yields a form with one text input field of the default size named `color'. | 
|---|
| [74] | 1164 | The generated label for this field would be ``Color''. To add a longer or more | 
|---|
| [34] | 1165 | descriptive label, use: | 
|---|
| [19] | 1166 |  | 
|---|
 | 1167 |     color|Favorite color | 
|---|
 | 1168 |  | 
|---|
| [34] | 1169 | The descriptive label can be a multiword string, as described above. So if you | 
|---|
 | 1170 | want punctuation in the label, you should single quote it: | 
|---|
| [19] | 1171 |  | 
|---|
| [34] | 1172 |     color|'Fav. color' | 
|---|
 | 1173 |  | 
|---|
| [19] | 1174 | To use a different input type: | 
|---|
 | 1175 |  | 
|---|
 | 1176 |     color|Favorite color:select{red,blue,green} | 
|---|
 | 1177 |  | 
|---|
 | 1178 | Recognized input types are the same as those used by CGI::FormBuilder: | 
|---|
 | 1179 |  | 
|---|
 | 1180 |     text        # the default | 
|---|
 | 1181 |     textarea | 
|---|
| [23] | 1182 |     password | 
|---|
 | 1183 |     file | 
|---|
 | 1184 |     checkbox | 
|---|
 | 1185 |     radio | 
|---|
| [19] | 1186 |     select | 
|---|
| [23] | 1187 |     hidden | 
|---|
| [19] | 1188 |     static | 
|---|
 | 1189 |  | 
|---|
| [84] | 1190 | For multi-select fields, append a C<*> to the field type: | 
|---|
 | 1191 |  | 
|---|
 | 1192 |     colors:select* | 
|---|
 | 1193 |  | 
|---|
| [21] | 1194 | To change the size of the input field, add a bracketed subscript after the | 
|---|
 | 1195 | field name (but before the descriptive label): | 
|---|
| [19] | 1196 |  | 
|---|
| [21] | 1197 |     # for a single line field, sets size="40" | 
|---|
 | 1198 |     title[40]:text | 
|---|
 | 1199 |      | 
|---|
 | 1200 |     # for a multiline field, sets rows="4" and cols="30" | 
|---|
 | 1201 |     description[4,30]:textarea | 
|---|
 | 1202 |  | 
|---|
| [56] | 1203 | To also set the C<maxlength> attribute for text fields, add a C<!> after | 
|---|
 | 1204 | the size: | 
|---|
 | 1205 |  | 
|---|
 | 1206 |     # ensure that all titles entered are 40 characters or less | 
|---|
 | 1207 |     title[40!]:text | 
|---|
 | 1208 |  | 
|---|
 | 1209 | This currently only works for single line text fields. | 
|---|
| [61] | 1210 |  | 
|---|
 | 1211 | To create a growable field, add a C<*> after the name (and size, if | 
|---|
 | 1212 | given). Growable fields have a button that allows the user to add a | 
|---|
 | 1213 | copy of the field input. Currently, this only works for C<text> and | 
|---|
| [64] | 1214 | C<file> fields, and you must have L<CGI::FormBuilder> 3.02 or higher. | 
|---|
 | 1215 | Growable fields also require JavaScript to function correctly. | 
|---|
| [61] | 1216 |  | 
|---|
 | 1217 |     # you can have as many people as you like | 
|---|
 | 1218 |     person*:text | 
|---|
 | 1219 |  | 
|---|
| [80] | 1220 | To set a limit to the maximum number of inputs a field can grow to, add | 
|---|
 | 1221 | a number after the C<*>: | 
|---|
 | 1222 |  | 
|---|
 | 1223 |     # allow up to 5 musicians | 
|---|
 | 1224 |     musician*5:text | 
|---|
 | 1225 |  | 
|---|
| [74] | 1226 | To create a C<radio> or C<select> field that includes an "other" option, | 
|---|
 | 1227 | append the string C<+other> to the field type: | 
|---|
 | 1228 |  | 
|---|
 | 1229 |     position:select+other | 
|---|
 | 1230 |  | 
|---|
 | 1231 | Or, to let FormBuilder decide whether to use radio buttons or a dropdown: | 
|---|
 | 1232 |  | 
|---|
 | 1233 |     position+other | 
|---|
 | 1234 |  | 
|---|
 | 1235 | Like growable fields, 'other' fields require FormBuilder 3.02 or higher. | 
|---|
 | 1236 |  | 
|---|
| [21] | 1237 | For the input types that can have options (C<select>, C<radio>, and | 
|---|
 | 1238 | C<checkbox>), here's how you do it: | 
|---|
 | 1239 |  | 
|---|
 | 1240 |     color|Favorite color:select{red,blue,green} | 
|---|
 | 1241 |  | 
|---|
| [24] | 1242 | Values are in a comma-separated list of single words or multiword strings | 
|---|
 | 1243 | inside curly braces. Whitespace between values is irrelevant. | 
|---|
| [21] | 1244 |  | 
|---|
| [26] | 1245 | To add more descriptive display text to a value in a list, add a square-bracketed | 
|---|
| [19] | 1246 | ``subscript,'' as in: | 
|---|
 | 1247 |  | 
|---|
 | 1248 |     ...:select{red[Scarlet],blue[Azure],green[Olive Drab]} | 
|---|
 | 1249 |  | 
|---|
| [1] | 1250 | If you have a list of options that is too long to fit comfortably on one line, | 
|---|
| [26] | 1251 | you should use the C<!list> directive: | 
|---|
| [1] | 1252 |  | 
|---|
| [19] | 1253 |     !list MONTHS { | 
|---|
 | 1254 |         1[January], | 
|---|
 | 1255 |         2[February], | 
|---|
 | 1256 |         3[March], | 
|---|
 | 1257 |         # and so on... | 
|---|
 | 1258 |     } | 
|---|
 | 1259 |      | 
|---|
 | 1260 |     month:select@MONTHS | 
|---|
 | 1261 |  | 
|---|
| [26] | 1262 | If you want to have a single checkbox (e.g. for a field that says ``I want to | 
|---|
 | 1263 | recieve more information''), you can just specify the type as checkbox without | 
|---|
 | 1264 | supplying any options: | 
|---|
 | 1265 |  | 
|---|
 | 1266 |     moreinfo|I want to recieve more information:checkbox | 
|---|
 | 1267 |  | 
|---|
| [30] | 1268 | In this case, the label ``I want to recieve more information'' will be | 
|---|
 | 1269 | printed to the right of the checkbox. | 
|---|
| [26] | 1270 |  | 
|---|
| [19] | 1271 | You can also supply a default value to the field. To get a default value of | 
|---|
 | 1272 | C<green> for the color field: | 
|---|
 | 1273 |  | 
|---|
 | 1274 |     color|Favorite color:select=green{red,blue,green} | 
|---|
 | 1275 |  | 
|---|
| [24] | 1276 | Default values can also be either single words or multiword strings. | 
|---|
 | 1277 |  | 
|---|
| [19] | 1278 | To validate a field, include a validation type at the end of the field line: | 
|---|
 | 1279 |  | 
|---|
 | 1280 |     email|Email address//EMAIL | 
|---|
 | 1281 |  | 
|---|
| [21] | 1282 | Valid validation types include any of the builtin defaults from L<CGI::FormBuilder>, | 
|---|
| [19] | 1283 | or the name of a pattern that you define with the C<!pattern> directive elsewhere | 
|---|
 | 1284 | in your form spec: | 
|---|
 | 1285 |  | 
|---|
 | 1286 |     !pattern DAY /^([1-3][0-9])|[1-9]$/ | 
|---|
 | 1287 |      | 
|---|
 | 1288 |     last_day//DAY | 
|---|
 | 1289 |  | 
|---|
 | 1290 | If you just want a required value, use the builtin validation type C<VALUE>: | 
|---|
 | 1291 |  | 
|---|
 | 1292 |     title//VALUE | 
|---|
 | 1293 |  | 
|---|
| [24] | 1294 | By default, adding a validation type to a field makes that field required. To | 
|---|
 | 1295 | change this, add a C<?> to the end of the validation type: | 
|---|
 | 1296 |  | 
|---|
 | 1297 |     contact//EMAIL? | 
|---|
 | 1298 |  | 
|---|
 | 1299 | In this case, you would get a C<contact> field that was optional, but if it | 
|---|
 | 1300 | were filled in, would have to validate as an C<EMAIL>. | 
|---|
 | 1301 |  | 
|---|
| [42] | 1302 | =head2 Field Groups | 
|---|
 | 1303 |  | 
|---|
 | 1304 | You can define groups of fields using the C<!group> directive: | 
|---|
 | 1305 |  | 
|---|
 | 1306 |     !group DATE { | 
|---|
 | 1307 |         month:select@MONTHS//INT | 
|---|
 | 1308 |         day[2]//INT | 
|---|
 | 1309 |         year[4]//INT | 
|---|
 | 1310 |     } | 
|---|
 | 1311 |  | 
|---|
| [87] | 1312 | You can also use groups in normal field lines: | 
|---|
| [42] | 1313 |  | 
|---|
| [87] | 1314 |     birthday|Your birthday:DATE | 
|---|
| [42] | 1315 |  | 
|---|
| [87] | 1316 | This will create a line in the form labeled ``Your birthday'' which contains | 
|---|
| [42] | 1317 | a month dropdown, and day and year text entry fields. The actual input field | 
|---|
 | 1318 | names are formed by concatenating the C<!field> name (e.g. C<birthday>) with | 
|---|
 | 1319 | the name of the subfield defined in the group (e.g. C<month>, C<day>, C<year>). | 
|---|
 | 1320 | Thus in this example, you would end up with the form fields C<birthday_month>, | 
|---|
 | 1321 | C<birthday_day>, and C<birthday_year>. | 
|---|
 | 1322 |  | 
|---|
| [63] | 1323 | The only (currently) supported pieces of a fieldspec that may be used with a | 
|---|
| [73] | 1324 | group in this notation are name, label, and hint. | 
|---|
| [63] | 1325 |  | 
|---|
| [87] | 1326 | The old method of using field groups was with the C<!field> directive: | 
|---|
 | 1327 |  | 
|---|
 | 1328 |     !field %DATE birthday | 
|---|
 | 1329 |  | 
|---|
| [89] | 1330 | This format is now B<deprecated>, and although it still works, the parser will | 
|---|
 | 1331 | warn you if you use it. | 
|---|
| [87] | 1332 |  | 
|---|
| [1] | 1333 | =head2 Comments | 
|---|
 | 1334 |  | 
|---|
 | 1335 |     # comment ... | 
|---|
 | 1336 |  | 
|---|
| [73] | 1337 | Any line beginning with a C<#> is considered a comment. Comments can also appear | 
|---|
 | 1338 | after any field line. They I<cannot> appear between items in a C<!list>, or on | 
|---|
 | 1339 | the same line as any of the directives. | 
|---|
| [1] | 1340 |  | 
|---|
| [7] | 1341 | =head1 TODO | 
|---|
 | 1342 |  | 
|---|
| [66] | 1343 | =head2 Documentation/Tests | 
|---|
| [52] | 1344 |  | 
|---|
| [64] | 1345 | Document use of the parser as a standalone module | 
|---|
 | 1346 |  | 
|---|
| [72] | 1347 | Make sure that the docs match the generated code. | 
|---|
 | 1348 |  | 
|---|
| [66] | 1349 | Better tests! | 
|---|
 | 1350 |  | 
|---|
 | 1351 | =head2 Language/Parser | 
|---|
 | 1352 |  | 
|---|
| [52] | 1353 | Pieces that wouldn't make sense in a group field: size, row/col, options, | 
|---|
 | 1354 | validate. These should cause C<build> to emit a warning before ignoring them. | 
|---|
 | 1355 |  | 
|---|
| [66] | 1356 | C<!include> directive to include external formspec files | 
|---|
 | 1357 |  | 
|---|
| [89] | 1358 | Better recovery from parse errors | 
|---|
 | 1359 |  | 
|---|
| [66] | 1360 | =head2 Code generation/Templates | 
|---|
 | 1361 |  | 
|---|
| [70] | 1362 | Revise the generated form constructing code to use the C<fieldopts> | 
|---|
 | 1363 | option to C<< FB->new >>; will require FB 3.02 to run. | 
|---|
 | 1364 |  | 
|---|
| [80] | 1365 | Better integration with L<CGI::FormBuilder>'s templating system; rely on the | 
|---|
 | 1366 | FB messages instead of trying to make our own. | 
|---|
| [66] | 1367 |  | 
|---|
| [31] | 1368 | Allow for custom wrappers around the C<form_template> | 
|---|
 | 1369 |  | 
|---|
| [30] | 1370 | Maybe use HTML::Template instead of Text::Template for the built in template | 
|---|
| [28] | 1371 | (since CGI::FormBuilder users may be more likely to already have HTML::Template) | 
|---|
 | 1372 |  | 
|---|
| [23] | 1373 | =head1 BUGS | 
|---|
 | 1374 |  | 
|---|
| [87] | 1375 | Placing C<fields> in a C<!fb> directive does not behave as expected (i.e. they | 
|---|
 | 1376 | don't show up). This is not a big issue, since you should be defining your fields | 
|---|
 | 1377 | in the body of the formspec file, but for completeness' sake I would like to get | 
|---|
 | 1378 | this figured out. | 
|---|
 | 1379 |  | 
|---|
| [42] | 1380 | I'm sure there are more in there, I just haven't tripped over any new ones lately. :-) | 
|---|
 | 1381 |  | 
|---|
 | 1382 | Suggestions on how to improve the (currently tiny) test suite would be appreciated. | 
|---|
 | 1383 |  | 
|---|
| [1] | 1384 | =head1 SEE ALSO | 
|---|
 | 1385 |  | 
|---|
| [50] | 1386 | L<http://textformbuilder.berlios.de> | 
|---|
| [1] | 1387 |  | 
|---|
| [50] | 1388 | L<CGI::FormBuilder>, L<http://formbuilder.org> | 
|---|
 | 1389 |  | 
|---|
| [23] | 1390 | =head1 THANKS | 
|---|
 | 1391 |  | 
|---|
| [26] | 1392 | Thanks to eszpee for pointing out some bugs in the default value parsing, | 
|---|
 | 1393 | as well as some suggestions for i18n/l10n and splitting up long forms into | 
|---|
| [29] | 1394 | sections. | 
|---|
| [23] | 1395 |  | 
|---|
| [87] | 1396 | And of course, to Nathan Wiger, for giving us CGI::FormBuilder in the | 
|---|
| [61] | 1397 | first place. Thanks Nate! | 
|---|
 | 1398 |  | 
|---|
| [16] | 1399 | =head1 AUTHOR | 
|---|
 | 1400 |  | 
|---|
| [34] | 1401 | Peter Eichman C<< <peichman@cpan.org> >> | 
|---|
| [16] | 1402 |  | 
|---|
 | 1403 | =head1 COPYRIGHT AND LICENSE | 
|---|
 | 1404 |  | 
|---|
| [53] | 1405 | Copyright E<copy>2004-2005 by Peter Eichman. | 
|---|
| [16] | 1406 |  | 
|---|
 | 1407 | This program is free software; you can redistribute it and/or | 
|---|
 | 1408 | modify it under the same terms as Perl itself. | 
|---|
 | 1409 |  | 
|---|
| [1] | 1410 | =cut | 
|---|