1 | package Text::FormBuilder; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | use vars qw($VERSION); |
---|
7 | |
---|
8 | $VERSION = '0.06_02'; |
---|
9 | |
---|
10 | use Carp; |
---|
11 | use Text::FormBuilder::Parser; |
---|
12 | use CGI::FormBuilder; |
---|
13 | |
---|
14 | # the static default options passed to CGI::FormBuilder->new |
---|
15 | my %DEFAULT_OPTIONS = ( |
---|
16 | method => 'GET', |
---|
17 | javascript => 0, |
---|
18 | keepextras => 1, |
---|
19 | ); |
---|
20 | |
---|
21 | # the built in CSS for the template |
---|
22 | my $DEFAULT_CSS = <<END; |
---|
23 | table { padding: 1em; } |
---|
24 | #author, #footer { font-style: italic; } |
---|
25 | caption h2 { padding: .125em .5em; background: #ccc; text-align: left; } |
---|
26 | th { text-align: left; } |
---|
27 | th h3 { padding: .125em .5em; background: #eee; } |
---|
28 | th.label { font-weight: normal; text-align: right; vertical-align: top; } |
---|
29 | td ul { list-style: none; padding-left: 0; margin-left: 0; } |
---|
30 | .sublabel { color: #999; } |
---|
31 | .invalid { background: red; } |
---|
32 | END |
---|
33 | |
---|
34 | sub new { |
---|
35 | my $invocant = shift; |
---|
36 | my $class = ref $invocant || $invocant; |
---|
37 | my $self = { |
---|
38 | parser => Text::FormBuilder::Parser->new, |
---|
39 | }; |
---|
40 | return bless $self, $class; |
---|
41 | } |
---|
42 | |
---|
43 | sub parse { |
---|
44 | my ($self, $source) = @_; |
---|
45 | if (ref $source && ref $source eq 'SCALAR') { |
---|
46 | $self->parse_text($$source); |
---|
47 | } else { |
---|
48 | $self->parse_file($source); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | sub parse_file { |
---|
53 | my ($self, $filename) = @_; |
---|
54 | |
---|
55 | # so it can be called as a class method |
---|
56 | $self = $self->new unless ref $self; |
---|
57 | |
---|
58 | local $/ = undef; |
---|
59 | open SRC, "< $filename"; |
---|
60 | my $src = <SRC>; |
---|
61 | close SRC; |
---|
62 | |
---|
63 | return $self->parse_text($src); |
---|
64 | } |
---|
65 | |
---|
66 | sub parse_text { |
---|
67 | my ($self, $src) = @_; |
---|
68 | |
---|
69 | # so it can be called as a class method |
---|
70 | $self = $self->new unless ref $self; |
---|
71 | |
---|
72 | $self->{form_spec} = $self->{parser}->form_spec($src); |
---|
73 | |
---|
74 | # mark structures as not built (newly parsed text) |
---|
75 | $self->{built} = 0; |
---|
76 | |
---|
77 | return $self; |
---|
78 | } |
---|
79 | |
---|
80 | sub build { |
---|
81 | my ($self, %options) = @_; |
---|
82 | |
---|
83 | # save the build options so they can be used from write_module |
---|
84 | $self->{build_options} = { %options }; |
---|
85 | |
---|
86 | # our custom %options: |
---|
87 | # form_only: use only the form part of the template |
---|
88 | my $form_only = $options{form_only}; |
---|
89 | my $css; |
---|
90 | $css = $options{css} || $DEFAULT_CSS; |
---|
91 | $css .= $options{extra_css} if $options{extra_css}; |
---|
92 | |
---|
93 | # remove our custom options |
---|
94 | delete $options{$_} foreach qw(form_only css extra_css); |
---|
95 | |
---|
96 | # substitute in custom pattern definitions for field validation |
---|
97 | if (my %patterns = %{ $self->{form_spec}{patterns} || {} }) { |
---|
98 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
99 | if ($$_{validate} and exists $patterns{$$_{validate}}) { |
---|
100 | $$_{validate} = $patterns{$$_{validate}}; |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | # expand groups |
---|
106 | my %groups = %{ $self->{form_spec}{groups} || {} }; |
---|
107 | |
---|
108 | for my $section (@{ $self->{form_spec}{sections} || [] }) { |
---|
109 | ## foreach (grep { $$_[0] eq 'group' } @{ $self->{form_spec}{lines} || [] }) { |
---|
110 | foreach (grep { $$_[0] eq 'group' } @{ $$section{lines} }) { |
---|
111 | $$_[1]{group} =~ s/^\%//; # strip leading % from group var name |
---|
112 | |
---|
113 | if (exists $groups{$$_[1]{group}}) { |
---|
114 | my @fields; # fields in the group |
---|
115 | push @fields, { %$_ } foreach @{ $groups{$$_[1]{group}} }; |
---|
116 | for my $field (@fields) { |
---|
117 | $$field{label} ||= ucfirst $$field{name}; |
---|
118 | $$field{name} = "$$_[1]{name}_$$field{name}"; |
---|
119 | } |
---|
120 | $_ = [ 'group', { label => $$_[1]{label} || ucfirst(join(' ',split('_',$$_[1]{name}))), group => \@fields } ]; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | $self->{form_spec}{fields} = []; |
---|
126 | |
---|
127 | for my $section (@{ $self->{form_spec}{sections} || [] }) { |
---|
128 | #for my $line (@{ $self->{form_spec}{lines} || [] }) { |
---|
129 | for my $line (@{ $$section{lines} }) { |
---|
130 | if ($$line[0] eq 'group') { |
---|
131 | push @{ $self->{form_spec}{fields} }, $_ foreach @{ $$line[1]{group} }; |
---|
132 | } elsif ($$line[0] eq 'field') { |
---|
133 | push @{ $self->{form_spec}{fields} }, $$line[1]; |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | # substitute in list names |
---|
139 | my %lists = %{ $self->{form_spec}{lists} || {} }; |
---|
140 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
141 | next unless $$_{list}; |
---|
142 | |
---|
143 | $$_{list} =~ s/^\@//; # strip leading @ from list var name |
---|
144 | |
---|
145 | # a hack so we don't get screwy reference errors |
---|
146 | if (exists $lists{$$_{list}}) { |
---|
147 | my @list; |
---|
148 | push @list, { %$_ } foreach @{ $lists{$$_{list}} }; |
---|
149 | $$_{options} = \@list; |
---|
150 | } else { |
---|
151 | ## #TODO: this is not working in CGI::FormBuilder |
---|
152 | ## # assume that the list name is a builtin |
---|
153 | ## # and let it fall through to CGI::FormBuilder |
---|
154 | ## $$_{options} = $$_{list}; |
---|
155 | ## warn "falling through to builtin $$_{options}"; |
---|
156 | } |
---|
157 | } continue { |
---|
158 | delete $$_{list}; |
---|
159 | } |
---|
160 | |
---|
161 | # special case single-value checkboxes |
---|
162 | foreach (grep { $$_{type} && $$_{type} eq 'checkbox' } @{ $self->{form_spec}{fields} }) { |
---|
163 | unless ($$_{options}) { |
---|
164 | $$_{options} = [ { $$_{name} => $$_{label} || ucfirst join(' ',split(/_/,$$_{name})) } ]; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | # TODO: configurable threshold for this |
---|
169 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
170 | $$_{ulist} = 1 if defined $$_{options} and @{ $$_{options} } >= 3; |
---|
171 | } |
---|
172 | |
---|
173 | # remove extraneous undefined values |
---|
174 | for my $field (@{ $self->{form_spec}{fields} }) { |
---|
175 | defined $$field{$_} or delete $$field{$_} foreach keys %{ $field }; |
---|
176 | } |
---|
177 | |
---|
178 | # because this messes up things at the CGI::FormBuilder::field level |
---|
179 | # it seems to be marking required based on the existance of a 'required' |
---|
180 | # param, not whether it is true or defined |
---|
181 | $$_{required} or delete $$_{required} foreach @{ $self->{form_spec}{fields} }; |
---|
182 | |
---|
183 | # need to explicity set the fields so that simple text fields get picked up |
---|
184 | $self->{form} = CGI::FormBuilder->new( |
---|
185 | %DEFAULT_OPTIONS, |
---|
186 | fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], |
---|
187 | required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], |
---|
188 | title => $self->{form_spec}{title}, |
---|
189 | text => $self->{form_spec}{description}, |
---|
190 | template => { |
---|
191 | type => 'Text', |
---|
192 | engine => { |
---|
193 | TYPE => 'STRING', |
---|
194 | SOURCE => $form_only ? $self->_form_template : $self->_template($css), |
---|
195 | DELIMITERS => [ qw(<% %>) ], |
---|
196 | }, |
---|
197 | data => { |
---|
198 | sections => $self->{form_spec}{sections}, |
---|
199 | author => $self->{form_spec}{author}, |
---|
200 | description => $self->{form_spec}{description}, |
---|
201 | }, |
---|
202 | }, |
---|
203 | %options, |
---|
204 | ); |
---|
205 | $self->{form}->field(%{ $_ }) foreach @{ $self->{form_spec}{fields} }; |
---|
206 | |
---|
207 | # mark structures as built |
---|
208 | $self->{built} = 1; |
---|
209 | |
---|
210 | return $self; |
---|
211 | } |
---|
212 | |
---|
213 | sub write { |
---|
214 | my ($self, $outfile) = @_; |
---|
215 | |
---|
216 | # automatically call build if needed to |
---|
217 | # allow the new->parse->write shortcut |
---|
218 | $self->build unless $self->{built}; |
---|
219 | |
---|
220 | if ($outfile) { |
---|
221 | open FORM, "> $outfile"; |
---|
222 | print FORM $self->form->render; |
---|
223 | close FORM; |
---|
224 | } else { |
---|
225 | print $self->form->render; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | sub write_module { |
---|
230 | my ($self, $package, $use_tidy) = @_; |
---|
231 | |
---|
232 | croak 'Expecting a package name' unless $package; |
---|
233 | |
---|
234 | # automatically call build if needed to |
---|
235 | # allow the new->parse->write shortcut |
---|
236 | $self->build unless $self->{built}; |
---|
237 | |
---|
238 | # conditionally use Data::Dumper |
---|
239 | eval 'use Data::Dumper;'; |
---|
240 | die "Can't write module; need Data::Dumper. $@" if $@; |
---|
241 | |
---|
242 | # don't dump $VARn names |
---|
243 | $Data::Dumper::Terse = 1; |
---|
244 | |
---|
245 | my $css; |
---|
246 | $css = $self->{build_options}{css} || $DEFAULT_CSS; |
---|
247 | $css .= $self->{build_options}{extra_css} if $self->{build_options}{extra_css}; |
---|
248 | |
---|
249 | my %options = ( |
---|
250 | %DEFAULT_OPTIONS, |
---|
251 | title => $self->{form_spec}{title}, |
---|
252 | text => $self->{form_spec}{description}, |
---|
253 | fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], |
---|
254 | required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], |
---|
255 | template => { |
---|
256 | type => 'Text', |
---|
257 | engine => { |
---|
258 | TYPE => 'STRING', |
---|
259 | SOURCE => $self->{build_options}{form_only} ? $self->_form_template : $self->_template($css), |
---|
260 | DELIMITERS => [ qw(<% %>) ], |
---|
261 | }, |
---|
262 | data => { |
---|
263 | sections => $self->{form_spec}{sections}, |
---|
264 | author => $self->{form_spec}{author}, |
---|
265 | description => $self->{form_spec}{description}, |
---|
266 | }, |
---|
267 | }, |
---|
268 | %{ $self->{build_options} }, |
---|
269 | ); |
---|
270 | |
---|
271 | my $source = $options{form_only} ? $self->_form_template : $self->_template; |
---|
272 | |
---|
273 | # remove our custom options |
---|
274 | delete $options{$_} foreach qw(form_only css extra_css); |
---|
275 | |
---|
276 | my $form_options = keys %options > 0 ? Data::Dumper->Dump([\%options],['*options']) : ''; |
---|
277 | |
---|
278 | my $field_setup = join( |
---|
279 | "\n", |
---|
280 | map { '$cgi_form->field' . Data::Dumper->Dump([$_],['*field']) . ';' } @{ $self->{form_spec}{fields} } |
---|
281 | ); |
---|
282 | |
---|
283 | my $module = <<END; |
---|
284 | package $package; |
---|
285 | use strict; |
---|
286 | use warnings; |
---|
287 | |
---|
288 | use CGI::FormBuilder; |
---|
289 | |
---|
290 | sub get_form { |
---|
291 | my \$cgi = shift; |
---|
292 | my \$cgi_form = CGI::FormBuilder->new( |
---|
293 | params => \$cgi, |
---|
294 | $form_options |
---|
295 | ); |
---|
296 | |
---|
297 | $field_setup |
---|
298 | |
---|
299 | return \$cgi_form; |
---|
300 | } |
---|
301 | |
---|
302 | # module return |
---|
303 | 1; |
---|
304 | END |
---|
305 | |
---|
306 | my $outfile = (split(/::/, $package))[-1] . '.pm'; |
---|
307 | |
---|
308 | if ($use_tidy) { |
---|
309 | # clean up the generated code, if asked |
---|
310 | eval 'use Perl::Tidy'; |
---|
311 | die "Can't tidy the code: $@" if $@; |
---|
312 | Perl::Tidy::perltidy(source => \$module, destination => $outfile); |
---|
313 | } else { |
---|
314 | # otherwise, just print as is |
---|
315 | open FORM, "> $outfile"; |
---|
316 | print FORM $module; |
---|
317 | close FORM; |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | sub form { |
---|
322 | my $self = shift; |
---|
323 | |
---|
324 | # automatically call build if needed to |
---|
325 | # allow the new->parse->write shortcut |
---|
326 | $self->build unless $self->{built}; |
---|
327 | |
---|
328 | return $self->{form}; |
---|
329 | } |
---|
330 | |
---|
331 | sub _form_template { |
---|
332 | q[<% $description ? qq[<p id="description">$description</p>] : '' %> |
---|
333 | <% (grep { $_->{required} } @fields) ? qq[<p id="instructions">(Required fields are marked in <strong>bold</strong>.)</p>] : '' %> |
---|
334 | <% $start %> |
---|
335 | <% |
---|
336 | # drop in the hidden fields here |
---|
337 | $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); |
---|
338 | %> |
---|
339 | |
---|
340 | <% |
---|
341 | SECTION: while (my $section = shift @sections) { |
---|
342 | $OUT .= qq[<table id="] . ($$section{id} || '_default') . qq[">\n]; |
---|
343 | $OUT .= qq[ <caption><h2 class="sectionhead">$$section{head}</h2></caption>] if $$section{head}; |
---|
344 | TABLE_LINE: for my $line (@{ $$section{lines} }) { |
---|
345 | if ($$line[0] eq 'head') { |
---|
346 | $OUT .= qq[ <tr><th class="subhead" colspan="2"><h3>$$line[1]</h3></th></tr>\n] |
---|
347 | } elsif ($$line[0] eq 'field') { |
---|
348 | #TODO: we only need the field names, not the full field spec in the lines strucutre |
---|
349 | local $_ = $field{$$line[1]{name}}; |
---|
350 | # skip hidden fields in the table |
---|
351 | next TABLE_LINE if $$_{type} eq 'hidden'; |
---|
352 | |
---|
353 | $OUT .= $$_{invalid} ? qq[ <tr class="invalid">] : qq[ <tr>]; |
---|
354 | |
---|
355 | # special case single value checkboxes |
---|
356 | if ($$_{type} eq 'checkbox' && @{ $$_{options} } == 1) { |
---|
357 | $OUT .= qq[<th></th>]; |
---|
358 | } else { |
---|
359 | $OUT .= '<th class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}:</strong>] : "$$_{label}:") . '</th>'; |
---|
360 | } |
---|
361 | if ($$_{invalid}) { |
---|
362 | $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td>]; |
---|
363 | } else { |
---|
364 | $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; |
---|
365 | } |
---|
366 | $OUT .= qq[</tr>\n]; |
---|
367 | |
---|
368 | } elsif ($$line[0] eq 'group') { |
---|
369 | my @field_names = map { $$_{name} } @{ $$line[1]{group} }; |
---|
370 | my @group_fields = map { $field{$_} } @field_names; |
---|
371 | $OUT .= (grep { $$_{invalid} } @group_fields) ? qq[ <tr class="invalid">\n] : qq[ <tr>\n]; |
---|
372 | |
---|
373 | $OUT .= ' <th class="label">'; |
---|
374 | $OUT .= (grep { $$_{required} } @group_fields) ? qq[<strong class="required">$$line[1]{label}:</strong>] : "$$line[1]{label}:"; |
---|
375 | $OUT .= qq[</th>\n]; |
---|
376 | |
---|
377 | $OUT .= qq[ <td>]; |
---|
378 | $OUT .= join(' ', map { qq[<small class="sublabel">$$_{label}</small> $$_{field} $$_{comment}] } @group_fields); |
---|
379 | $OUT .= qq[ </td>\n]; |
---|
380 | $OUT .= qq[ </tr>\n]; |
---|
381 | } |
---|
382 | } |
---|
383 | # close the table if there are sections remaining |
---|
384 | # but leave the last one open for the submit button |
---|
385 | $OUT .= qq[</table>\n] if @sections; |
---|
386 | } |
---|
387 | %> |
---|
388 | <tr><th></th><td style="padding-top: 1em;"><% $submit %></td></tr> |
---|
389 | </table> |
---|
390 | <% $end %> |
---|
391 | ]; |
---|
392 | } |
---|
393 | |
---|
394 | sub _template { |
---|
395 | my $self = shift; |
---|
396 | my $css = shift || $DEFAULT_CSS; |
---|
397 | q[<html> |
---|
398 | <head> |
---|
399 | <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> |
---|
400 | <style type="text/css">] . |
---|
401 | $css . q[ |
---|
402 | </style> |
---|
403 | </head> |
---|
404 | <body> |
---|
405 | |
---|
406 | <h1><% $title %></h1> |
---|
407 | <% $author ? qq[<p id="author">Created by $author</p>] : '' %> |
---|
408 | ] . $self->_form_template . q[ |
---|
409 | <hr /> |
---|
410 | <div id="footer"> |
---|
411 | <p id="creator">Made with <a href="http://formbuilder.org/">CGI::FormBuilder</a> version <% CGI::FormBuilder->VERSION %>.</p> |
---|
412 | </div> |
---|
413 | </body> |
---|
414 | </html> |
---|
415 | ]; |
---|
416 | } |
---|
417 | |
---|
418 | sub dump { |
---|
419 | eval "use YAML;"; |
---|
420 | unless ($@) { |
---|
421 | print YAML::Dump(shift->{form_spec}); |
---|
422 | } else { |
---|
423 | warn "Can't dump form spec structure: $@"; |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | |
---|
428 | # module return |
---|
429 | 1; |
---|
430 | |
---|
431 | =head1 NAME |
---|
432 | |
---|
433 | Text::FormBuilder - Create CGI::FormBuilder objects from simple text descriptions |
---|
434 | |
---|
435 | =head1 SYNOPSIS |
---|
436 | |
---|
437 | use Text::FormBuilder; |
---|
438 | |
---|
439 | my $parser = Text::FormBuilder->new; |
---|
440 | $parser->parse($src_file); |
---|
441 | |
---|
442 | # returns a new CGI::FormBuilder object with |
---|
443 | # the fields from the input form spec |
---|
444 | my $form = $parser->form; |
---|
445 | |
---|
446 | # write a My::Form module to Form.pm |
---|
447 | $parser->write_module('My::Form'); |
---|
448 | |
---|
449 | =head1 REQUIRES |
---|
450 | |
---|
451 | L<Parse::RecDescent>, L<CGI::FormBuilder>, L<Text::Template> |
---|
452 | |
---|
453 | =head1 DESCRIPTION |
---|
454 | |
---|
455 | =head2 new |
---|
456 | |
---|
457 | =head2 parse |
---|
458 | |
---|
459 | # parse a file |
---|
460 | $parser->parse($filename); |
---|
461 | |
---|
462 | # or pass a scalar ref for parse a literal string |
---|
463 | $parser->parse(\$string); |
---|
464 | |
---|
465 | Parse the file or string. Returns the parser object. |
---|
466 | |
---|
467 | =head2 parse_file |
---|
468 | |
---|
469 | $parser->parse_file($src_file); |
---|
470 | |
---|
471 | # or as a class method |
---|
472 | my $parser = Text::FormBuilder->parse($src_file); |
---|
473 | |
---|
474 | =head2 parse_text |
---|
475 | |
---|
476 | $parser->parse_text($src); |
---|
477 | |
---|
478 | Parse the given C<$src> text. Returns the parser object. |
---|
479 | |
---|
480 | =head2 build |
---|
481 | |
---|
482 | $parser->build(%options); |
---|
483 | |
---|
484 | Builds the CGI::FormBuilder object. Options directly used by C<build> are: |
---|
485 | |
---|
486 | =over |
---|
487 | |
---|
488 | =item C<form_only> |
---|
489 | |
---|
490 | Only uses the form portion of the template, and omits the surrounding html, |
---|
491 | title, author, and the standard footer. This does, however, include the |
---|
492 | description as specified with the C<!description> directive. |
---|
493 | |
---|
494 | =item C<css>, C<extra_css> |
---|
495 | |
---|
496 | These options allow you to tell Text::FormBuilder to use different |
---|
497 | CSS styles for the built in template. A value given a C<css> will |
---|
498 | replace the existing CSS, and a value given as C<extra_css> will be |
---|
499 | appended to the CSS. If both options are given, then the CSS that is |
---|
500 | used will be C<css> concatenated with C<extra_css>. |
---|
501 | |
---|
502 | =back |
---|
503 | |
---|
504 | All other options given to C<build> are passed on verbatim to the |
---|
505 | L<CGI::FormBuilder> constructor. Any options given here override the |
---|
506 | defaults that this module uses. |
---|
507 | |
---|
508 | The C<form>, C<write>, and C<write_module> methods will all call |
---|
509 | C<build> with no options for you if you do not do so explicitly. |
---|
510 | This allows you to say things like this: |
---|
511 | |
---|
512 | my $form = Text::FormBuilder->new->parse('formspec.txt')->form; |
---|
513 | |
---|
514 | However, if you need to specify options to C<build>, you must call it |
---|
515 | explictly after C<parse>. |
---|
516 | |
---|
517 | =head2 form |
---|
518 | |
---|
519 | my $form = $parser->form; |
---|
520 | |
---|
521 | Returns the L<CGI::FormBuilder> object. Remember that you can modify |
---|
522 | this object directly, in order to (for example) dynamically populate |
---|
523 | dropdown lists or change input types at runtime. |
---|
524 | |
---|
525 | =head2 write |
---|
526 | |
---|
527 | $parser->write($out_file); |
---|
528 | # or just print to STDOUT |
---|
529 | $parser->write; |
---|
530 | |
---|
531 | Calls C<render> on the FormBuilder form, and either writes the resulting |
---|
532 | HTML to a file, or to STDOUT if no filename is given. |
---|
533 | |
---|
534 | CSS Hint: to get multiple sections to all line up their fields, set a |
---|
535 | standard width for th.label |
---|
536 | |
---|
537 | =head2 write_module |
---|
538 | |
---|
539 | $parser->write_module($package, $use_tidy); |
---|
540 | |
---|
541 | Takes a package name, and writes out a new module that can be used by your |
---|
542 | CGI script to render the form. This way, you only need CGI::FormBuilder on |
---|
543 | your server, and you don't have to parse the form spec each time you want |
---|
544 | to display your form. The generated module has one function (not exported) |
---|
545 | called C<get_form>, that takes a CGI object as its only argument, and returns |
---|
546 | a CGI::FormBuilder object. |
---|
547 | |
---|
548 | First, you parse the formspec and write the module, which you can do as a one-liner: |
---|
549 | |
---|
550 | $ perl -MText::FormBuilder -e"Text::FormBuilder->parse('formspec.txt')->write_module('My::Form')" |
---|
551 | |
---|
552 | And then, in your CGI script, use the new module: |
---|
553 | |
---|
554 | #!/usr/bin/perl -w |
---|
555 | use strict; |
---|
556 | |
---|
557 | use CGI; |
---|
558 | use My::Form; |
---|
559 | |
---|
560 | my $q = CGI->new; |
---|
561 | my $form = My::Form::get_form($q); |
---|
562 | |
---|
563 | # do the standard CGI::FormBuilder stuff |
---|
564 | if ($form->submitted && $form->validate) { |
---|
565 | # process results |
---|
566 | } else { |
---|
567 | print $q->header; |
---|
568 | print $form->render; |
---|
569 | } |
---|
570 | |
---|
571 | If you pass a true value as the second argument to C<write_module>, the parser |
---|
572 | will run L<Perl::Tidy> on the generated code before writing the module file. |
---|
573 | |
---|
574 | # write tidier code |
---|
575 | $parser->write_module('My::Form', 1); |
---|
576 | |
---|
577 | =head2 dump |
---|
578 | |
---|
579 | Uses L<YAML> to print out a human-readable representation of the parsed |
---|
580 | form spec. |
---|
581 | |
---|
582 | =head1 LANGUAGE |
---|
583 | |
---|
584 | field_name[size]|descriptive label[hint]:type=default{option1[display string],...}//validate |
---|
585 | |
---|
586 | !title ... |
---|
587 | |
---|
588 | !author ... |
---|
589 | |
---|
590 | !description { |
---|
591 | ... |
---|
592 | } |
---|
593 | |
---|
594 | !pattern name /regular expression/ |
---|
595 | |
---|
596 | !list name { |
---|
597 | option1[display string], |
---|
598 | option2[display string], |
---|
599 | ... |
---|
600 | } |
---|
601 | |
---|
602 | !list name &{ CODE } |
---|
603 | |
---|
604 | !section id heading |
---|
605 | |
---|
606 | !head ... |
---|
607 | |
---|
608 | =head2 Directives |
---|
609 | |
---|
610 | =over |
---|
611 | |
---|
612 | =item C<!pattern> |
---|
613 | |
---|
614 | Defines a validation pattern. |
---|
615 | |
---|
616 | =item C<!list> |
---|
617 | |
---|
618 | Defines a list for use in a C<radio>, C<checkbox>, or C<select> field. |
---|
619 | |
---|
620 | =item C<!title> |
---|
621 | |
---|
622 | =item C<!author> |
---|
623 | |
---|
624 | =item C<!description> |
---|
625 | |
---|
626 | A brief description of the form. Suitable for special instructions on how to |
---|
627 | fill out the form. |
---|
628 | |
---|
629 | =item C<!section> |
---|
630 | |
---|
631 | Starts a new section. Each section has its own heading and id, which are |
---|
632 | written by default into spearate tables. |
---|
633 | |
---|
634 | =item C<!head> |
---|
635 | |
---|
636 | Inserts a heading between two fields. There can only be one heading between |
---|
637 | any two fields; the parser will warn you if you try to put two headings right |
---|
638 | next to each other. |
---|
639 | |
---|
640 | =back |
---|
641 | |
---|
642 | =head2 Fields |
---|
643 | |
---|
644 | First, a note about multiword strings in the fields. Anywhere where it says |
---|
645 | that you may use a multiword string, this means that you can do one of two |
---|
646 | things. For strings that consist solely of alphanumeric characters (i.e. |
---|
647 | C<\w+>) and spaces, the string will be recognized as is: |
---|
648 | |
---|
649 | field_1|A longer label |
---|
650 | |
---|
651 | If you want to include non-alphanumerics (e.g. punctuation), you must |
---|
652 | single-quote the string: |
---|
653 | |
---|
654 | field_2|'Dept./Org.' |
---|
655 | |
---|
656 | To include a literal single-quote in a single-quoted string, escape it with |
---|
657 | a backslash: |
---|
658 | |
---|
659 | field_3|'\'Official\' title' |
---|
660 | |
---|
661 | Now, back to the basics. Form fields are each described on a single line. |
---|
662 | The simplest field is just a name (which cannot contain any whitespace): |
---|
663 | |
---|
664 | color |
---|
665 | |
---|
666 | This yields a form with one text input field of the default size named `color'. |
---|
667 | The label for this field as generated by CGI::FormBuilder would be ``Color''. |
---|
668 | To add a longer or more descriptive label, use: |
---|
669 | |
---|
670 | color|Favorite color |
---|
671 | |
---|
672 | The descriptive label can be a multiword string, as described above. |
---|
673 | |
---|
674 | To use a different input type: |
---|
675 | |
---|
676 | color|Favorite color:select{red,blue,green} |
---|
677 | |
---|
678 | Recognized input types are the same as those used by CGI::FormBuilder: |
---|
679 | |
---|
680 | text # the default |
---|
681 | textarea |
---|
682 | password |
---|
683 | file |
---|
684 | checkbox |
---|
685 | radio |
---|
686 | select |
---|
687 | hidden |
---|
688 | static |
---|
689 | |
---|
690 | To change the size of the input field, add a bracketed subscript after the |
---|
691 | field name (but before the descriptive label): |
---|
692 | |
---|
693 | # for a single line field, sets size="40" |
---|
694 | title[40]:text |
---|
695 | |
---|
696 | # for a multiline field, sets rows="4" and cols="30" |
---|
697 | description[4,30]:textarea |
---|
698 | |
---|
699 | For the input types that can have options (C<select>, C<radio>, and |
---|
700 | C<checkbox>), here's how you do it: |
---|
701 | |
---|
702 | color|Favorite color:select{red,blue,green} |
---|
703 | |
---|
704 | Values are in a comma-separated list of single words or multiword strings |
---|
705 | inside curly braces. Whitespace between values is irrelevant. |
---|
706 | |
---|
707 | To add more descriptive display text to a value in a list, add a square-bracketed |
---|
708 | ``subscript,'' as in: |
---|
709 | |
---|
710 | ...:select{red[Scarlet],blue[Azure],green[Olive Drab]} |
---|
711 | |
---|
712 | If you have a list of options that is too long to fit comfortably on one line, |
---|
713 | you should use the C<!list> directive: |
---|
714 | |
---|
715 | !list MONTHS { |
---|
716 | 1[January], |
---|
717 | 2[February], |
---|
718 | 3[March], |
---|
719 | # and so on... |
---|
720 | } |
---|
721 | |
---|
722 | month:select@MONTHS |
---|
723 | |
---|
724 | There is another form of the C<!list> directive: the dynamic list: |
---|
725 | |
---|
726 | !list RANDOM &{ map { rand } (0..5) } |
---|
727 | |
---|
728 | The code inside the C<&{ ... }> is C<eval>ed by C<build>, and the results |
---|
729 | are stuffed into the list. The C<eval>ed code can either return a simple |
---|
730 | list, as the example does, or the fancier C<< ( { value1 => 'Description 1'}, |
---|
731 | { value2 => 'Description 2}, ... ) >> form. |
---|
732 | |
---|
733 | I<B<NOTE:> This feature of the language may go away unless I find a compelling |
---|
734 | reason for it in the next few versions. What I really wanted was lists that |
---|
735 | were filled in at run-time (e.g. from a database), and that can be done easily |
---|
736 | enough with the CGI::FormBuilder object directly.> |
---|
737 | |
---|
738 | If you want to have a single checkbox (e.g. for a field that says ``I want to |
---|
739 | recieve more information''), you can just specify the type as checkbox without |
---|
740 | supplying any options: |
---|
741 | |
---|
742 | moreinfo|I want to recieve more information:checkbox |
---|
743 | |
---|
744 | In this case, the label ``I want to recieve more information'' will be |
---|
745 | printed to the right of the checkbox. |
---|
746 | |
---|
747 | You can also supply a default value to the field. To get a default value of |
---|
748 | C<green> for the color field: |
---|
749 | |
---|
750 | color|Favorite color:select=green{red,blue,green} |
---|
751 | |
---|
752 | Default values can also be either single words or multiword strings. |
---|
753 | |
---|
754 | To validate a field, include a validation type at the end of the field line: |
---|
755 | |
---|
756 | email|Email address//EMAIL |
---|
757 | |
---|
758 | Valid validation types include any of the builtin defaults from L<CGI::FormBuilder>, |
---|
759 | or the name of a pattern that you define with the C<!pattern> directive elsewhere |
---|
760 | in your form spec: |
---|
761 | |
---|
762 | !pattern DAY /^([1-3][0-9])|[1-9]$/ |
---|
763 | |
---|
764 | last_day//DAY |
---|
765 | |
---|
766 | If you just want a required value, use the builtin validation type C<VALUE>: |
---|
767 | |
---|
768 | title//VALUE |
---|
769 | |
---|
770 | By default, adding a validation type to a field makes that field required. To |
---|
771 | change this, add a C<?> to the end of the validation type: |
---|
772 | |
---|
773 | contact//EMAIL? |
---|
774 | |
---|
775 | In this case, you would get a C<contact> field that was optional, but if it |
---|
776 | were filled in, would have to validate as an C<EMAIL>. |
---|
777 | |
---|
778 | =head2 Comments |
---|
779 | |
---|
780 | # comment ... |
---|
781 | |
---|
782 | Any line beginning with a C<#> is considered a comment. |
---|
783 | |
---|
784 | =head1 TODO |
---|
785 | |
---|
786 | Use the custom message file format for messages in the built in template |
---|
787 | |
---|
788 | Maybe use HTML::Template instead of Text::Template for the built in template |
---|
789 | (since CGI::FormBuilder users may be more likely to already have HTML::Template) |
---|
790 | |
---|
791 | Better examples in the docs (maybe a standalone or two as well) |
---|
792 | |
---|
793 | Document the defaults that are passed to CGI::FormBuilder |
---|
794 | |
---|
795 | C<!include> directive to include external formspec files |
---|
796 | |
---|
797 | Better tests! |
---|
798 | |
---|
799 | =head1 BUGS |
---|
800 | |
---|
801 | I'm sure they're in there, I just haven't tripped over any new ones lately. :-) |
---|
802 | |
---|
803 | =head1 SEE ALSO |
---|
804 | |
---|
805 | L<CGI::FormBuilder> |
---|
806 | |
---|
807 | =head1 THANKS |
---|
808 | |
---|
809 | Thanks to eszpee for pointing out some bugs in the default value parsing, |
---|
810 | as well as some suggestions for i18n/l10n and splitting up long forms into |
---|
811 | sections. |
---|
812 | |
---|
813 | =head1 AUTHOR |
---|
814 | |
---|
815 | Peter Eichman <peichman@cpan.org> |
---|
816 | |
---|
817 | =head1 COPYRIGHT AND LICENSE |
---|
818 | |
---|
819 | Copyright E<copy>2004 by Peter Eichman. |
---|
820 | |
---|
821 | This program is free software; you can redistribute it and/or |
---|
822 | modify it under the same terms as Perl itself. |
---|
823 | |
---|
824 | =cut |
---|