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