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