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