[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 | |
---|
[30] | 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 | |
---|
[1] | 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 { |
---|
[19] | 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 { |
---|
[1] | 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; |
---|
[32] | 59 | open SRC, "< $filename" or croak "[Text::FormBuilder::parse_file] Can't open $filename: $!" and return; |
---|
[1] | 60 | my $src = <SRC>; |
---|
| 61 | close SRC; |
---|
| 62 | |
---|
| 63 | return $self->parse_text($src); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | sub parse_text { |
---|
| 67 | my ($self, $src) = @_; |
---|
[16] | 68 | |
---|
[1] | 69 | # so it can be called as a class method |
---|
| 70 | $self = $self->new unless ref $self; |
---|
[16] | 71 | |
---|
[32] | 72 | # append a newline so that it can be called on a single field easily |
---|
| 73 | $src .= "\n"; |
---|
| 74 | |
---|
[1] | 75 | $self->{form_spec} = $self->{parser}->form_spec($src); |
---|
[16] | 76 | |
---|
| 77 | # mark structures as not built (newly parsed text) |
---|
| 78 | $self->{built} = 0; |
---|
| 79 | |
---|
[1] | 80 | return $self; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | sub build { |
---|
| 84 | my ($self, %options) = @_; |
---|
[16] | 85 | |
---|
| 86 | # save the build options so they can be used from write_module |
---|
[12] | 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}; |
---|
[30] | 92 | my $css; |
---|
| 93 | $css = $options{css} || $DEFAULT_CSS; |
---|
| 94 | $css .= $options{extra_css} if $options{extra_css}; |
---|
[12] | 95 | |
---|
[30] | 96 | # remove our custom options |
---|
| 97 | delete $options{$_} foreach qw(form_only css extra_css); |
---|
| 98 | |
---|
[1] | 99 | # substitute in custom pattern definitions for field validation |
---|
[28] | 100 | if (my %patterns = %{ $self->{form_spec}{patterns} || {} }) { |
---|
[1] | 101 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
| 102 | if ($$_{validate} and exists $patterns{$$_{validate}}) { |
---|
| 103 | $$_{validate} = $patterns{$$_{validate}}; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
[21] | 108 | # expand groups |
---|
[28] | 109 | my %groups = %{ $self->{form_spec}{groups} || {} }; |
---|
[29] | 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 } ]; |
---|
[21] | 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
[1] | 125 | |
---|
[21] | 126 | $self->{form_spec}{fields} = []; |
---|
[29] | 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 | } |
---|
[21] | 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
[1] | 139 | # substitute in list names |
---|
[28] | 140 | my %lists = %{ $self->{form_spec}{lists} || {} }; |
---|
[25] | 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 { |
---|
[32] | 152 | # assume that the list name is a builtin |
---|
| 153 | # and let it fall through to CGI::FormBuilder |
---|
| 154 | $$_{options} = $$_{list}; |
---|
[1] | 155 | } |
---|
[25] | 156 | } continue { |
---|
| 157 | delete $$_{list}; |
---|
[30] | 158 | } |
---|
[21] | 159 | |
---|
[30] | 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 | |
---|
[16] | 167 | # TODO: configurable threshold for this |
---|
[14] | 168 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
[32] | 169 | $$_{ulist} = 1 if ref $$_{options} and @{ $$_{options} } >= 3; |
---|
[14] | 170 | } |
---|
[1] | 171 | |
---|
[24] | 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} }; |
---|
[23] | 181 | |
---|
[29] | 182 | # need to explicity set the fields so that simple text fields get picked up |
---|
[1] | 183 | $self->{form} = CGI::FormBuilder->new( |
---|
[23] | 184 | %DEFAULT_OPTIONS, |
---|
[29] | 185 | fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], |
---|
[24] | 186 | required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], |
---|
| 187 | title => $self->{form_spec}{title}, |
---|
[25] | 188 | text => $self->{form_spec}{description}, |
---|
[1] | 189 | template => { |
---|
| 190 | type => 'Text', |
---|
| 191 | engine => { |
---|
| 192 | TYPE => 'STRING', |
---|
[30] | 193 | SOURCE => $form_only ? $self->_form_template : $self->_template($css), |
---|
[11] | 194 | DELIMITERS => [ qw(<% %>) ], |
---|
[1] | 195 | }, |
---|
| 196 | data => { |
---|
[29] | 197 | sections => $self->{form_spec}{sections}, |
---|
[14] | 198 | author => $self->{form_spec}{author}, |
---|
| 199 | description => $self->{form_spec}{description}, |
---|
[1] | 200 | }, |
---|
| 201 | }, |
---|
| 202 | %options, |
---|
| 203 | ); |
---|
| 204 | $self->{form}->field(%{ $_ }) foreach @{ $self->{form_spec}{fields} }; |
---|
| 205 | |
---|
[16] | 206 | # mark structures as built |
---|
| 207 | $self->{built} = 1; |
---|
| 208 | |
---|
[1] | 209 | return $self; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | sub write { |
---|
| 213 | my ($self, $outfile) = @_; |
---|
[16] | 214 | |
---|
| 215 | # automatically call build if needed to |
---|
| 216 | # allow the new->parse->write shortcut |
---|
| 217 | $self->build unless $self->{built}; |
---|
| 218 | |
---|
[1] | 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 | |
---|
[12] | 228 | sub write_module { |
---|
[16] | 229 | my ($self, $package, $use_tidy) = @_; |
---|
[12] | 230 | |
---|
[32] | 231 | croak '[Text::FormBuilder::write_module] Expecting a package name' unless $package; |
---|
[16] | 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 | |
---|
[12] | 241 | # don't dump $VARn names |
---|
| 242 | $Data::Dumper::Terse = 1; |
---|
| 243 | |
---|
[30] | 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 | |
---|
[23] | 248 | my %options = ( |
---|
| 249 | %DEFAULT_OPTIONS, |
---|
[24] | 250 | title => $self->{form_spec}{title}, |
---|
[25] | 251 | text => $self->{form_spec}{description}, |
---|
[29] | 252 | fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], |
---|
[24] | 253 | required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], |
---|
[23] | 254 | template => { |
---|
| 255 | type => 'Text', |
---|
| 256 | engine => { |
---|
| 257 | TYPE => 'STRING', |
---|
[30] | 258 | SOURCE => $self->{build_options}{form_only} ? $self->_form_template : $self->_template($css), |
---|
[23] | 259 | DELIMITERS => [ qw(<% %>) ], |
---|
| 260 | }, |
---|
| 261 | data => { |
---|
[29] | 262 | sections => $self->{form_spec}{sections}, |
---|
[23] | 263 | author => $self->{form_spec}{author}, |
---|
| 264 | description => $self->{form_spec}{description}, |
---|
| 265 | }, |
---|
| 266 | }, |
---|
| 267 | %{ $self->{build_options} }, |
---|
| 268 | ); |
---|
| 269 | |
---|
[16] | 270 | my $source = $options{form_only} ? $self->_form_template : $self->_template; |
---|
[12] | 271 | |
---|
[30] | 272 | # remove our custom options |
---|
| 273 | delete $options{$_} foreach qw(form_only css extra_css); |
---|
[16] | 274 | |
---|
[23] | 275 | my $form_options = keys %options > 0 ? Data::Dumper->Dump([\%options],['*options']) : ''; |
---|
[16] | 276 | |
---|
[12] | 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 | |
---|
[15] | 289 | sub get_form { |
---|
[12] | 290 | my \$cgi = shift; |
---|
[23] | 291 | my \$cgi_form = CGI::FormBuilder->new( |
---|
[12] | 292 | params => \$cgi, |
---|
[16] | 293 | $form_options |
---|
[12] | 294 | ); |
---|
| 295 | |
---|
| 296 | $field_setup |
---|
| 297 | |
---|
| 298 | return \$cgi_form; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | # module return |
---|
| 302 | 1; |
---|
| 303 | END |
---|
[23] | 304 | |
---|
[12] | 305 | my $outfile = (split(/::/, $package))[-1] . '.pm'; |
---|
| 306 | |
---|
[16] | 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 |
---|
[12] | 314 | open FORM, "> $outfile"; |
---|
| 315 | print FORM $module; |
---|
| 316 | close FORM; |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | |
---|
[16] | 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}; |
---|
[1] | 326 | |
---|
[16] | 327 | return $self->{form}; |
---|
| 328 | } |
---|
| 329 | |
---|
[12] | 330 | sub _form_template { |
---|
[15] | 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>] : '' %> |
---|
[12] | 333 | <% $start %> |
---|
[23] | 334 | <% |
---|
| 335 | # drop in the hidden fields here |
---|
| 336 | $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); |
---|
| 337 | %> |
---|
| 338 | |
---|
[29] | 339 | <% |
---|
| 340 | SECTION: while (my $section = shift @sections) { |
---|
[30] | 341 | $OUT .= qq[<table id="] . ($$section{id} || '_default') . qq[">\n]; |
---|
| 342 | $OUT .= qq[ <caption><h2 class="sectionhead">$$section{head}</h2></caption>] if $$section{head}; |
---|
[29] | 343 | TABLE_LINE: for my $line (@{ $$section{lines} }) { |
---|
| 344 | if ($$line[0] eq 'head') { |
---|
[30] | 345 | $OUT .= qq[ <tr><th class="subhead" colspan="2"><h3>$$line[1]</h3></th></tr>\n] |
---|
[29] | 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>]; |
---|
[30] | 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 | } |
---|
[29] | 360 | if ($$_{invalid}) { |
---|
[30] | 361 | $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td>]; |
---|
[29] | 362 | } else { |
---|
[30] | 363 | $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; |
---|
[29] | 364 | } |
---|
[30] | 365 | $OUT .= qq[</tr>\n]; |
---|
| 366 | |
---|
[29] | 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 | } |
---|
[21] | 381 | } |
---|
[29] | 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 | %> |
---|
[12] | 387 | <tr><th></th><td style="padding-top: 1em;"><% $submit %></td></tr> |
---|
| 388 | </table> |
---|
| 389 | <% $end %> |
---|
| 390 | ]; |
---|
| 391 | } |
---|
| 392 | |
---|
[1] | 393 | sub _template { |
---|
[12] | 394 | my $self = shift; |
---|
[30] | 395 | my $css = shift || $DEFAULT_CSS; |
---|
[12] | 396 | q[<html> |
---|
[1] | 397 | <head> |
---|
| 398 | <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> |
---|
[30] | 399 | <style type="text/css">] . |
---|
| 400 | $css . q[ |
---|
[1] | 401 | </style> |
---|
| 402 | </head> |
---|
| 403 | <body> |
---|
| 404 | |
---|
| 405 | <h1><% $title %></h1> |
---|
| 406 | <% $author ? qq[<p id="author">Created by $author</p>] : '' %> |
---|
[12] | 407 | ] . $self->_form_template . q[ |
---|
[1] | 408 | <hr /> |
---|
| 409 | <div id="footer"> |
---|
[16] | 410 | <p id="creator">Made with <a href="http://formbuilder.org/">CGI::FormBuilder</a> version <% CGI::FormBuilder->VERSION %>.</p> |
---|
[1] | 411 | </div> |
---|
| 412 | </body> |
---|
| 413 | </html> |
---|
| 414 | ]; |
---|
| 415 | } |
---|
| 416 | |
---|
[7] | 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 | } |
---|
[1] | 425 | |
---|
| 426 | |
---|
| 427 | # module return |
---|
| 428 | 1; |
---|
| 429 | |
---|
| 430 | =head1 NAME |
---|
| 431 | |
---|
[21] | 432 | Text::FormBuilder - Create CGI::FormBuilder objects from simple text descriptions |
---|
[1] | 433 | |
---|
| 434 | =head1 SYNOPSIS |
---|
| 435 | |
---|
[16] | 436 | use Text::FormBuilder; |
---|
| 437 | |
---|
[1] | 438 | my $parser = Text::FormBuilder->new; |
---|
| 439 | $parser->parse($src_file); |
---|
| 440 | |
---|
[16] | 441 | # returns a new CGI::FormBuilder object with |
---|
| 442 | # the fields from the input form spec |
---|
[7] | 443 | my $form = $parser->form; |
---|
[19] | 444 | |
---|
| 445 | # write a My::Form module to Form.pm |
---|
| 446 | $parser->write_module('My::Form'); |
---|
[1] | 447 | |
---|
[23] | 448 | =head1 REQUIRES |
---|
| 449 | |
---|
| 450 | L<Parse::RecDescent>, L<CGI::FormBuilder>, L<Text::Template> |
---|
| 451 | |
---|
[1] | 452 | =head1 DESCRIPTION |
---|
| 453 | |
---|
| 454 | =head2 new |
---|
| 455 | |
---|
| 456 | =head2 parse |
---|
| 457 | |
---|
[19] | 458 | # parse a file |
---|
| 459 | $parser->parse($filename); |
---|
[7] | 460 | |
---|
[19] | 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 | |
---|
[7] | 470 | # or as a class method |
---|
[16] | 471 | my $parser = Text::FormBuilder->parse($src_file); |
---|
[7] | 472 | |
---|
| 473 | =head2 parse_text |
---|
| 474 | |
---|
[16] | 475 | $parser->parse_text($src); |
---|
| 476 | |
---|
[19] | 477 | Parse the given C<$src> text. Returns the parser object. |
---|
[16] | 478 | |
---|
[1] | 479 | =head2 build |
---|
| 480 | |
---|
[12] | 481 | $parser->build(%options); |
---|
[7] | 482 | |
---|
[12] | 483 | Builds the CGI::FormBuilder object. Options directly used by C<build> are: |
---|
| 484 | |
---|
| 485 | =over |
---|
| 486 | |
---|
[19] | 487 | =item C<form_only> |
---|
[12] | 488 | |
---|
| 489 | Only uses the form portion of the template, and omits the surrounding html, |
---|
[19] | 490 | title, author, and the standard footer. This does, however, include the |
---|
| 491 | description as specified with the C<!description> directive. |
---|
[12] | 492 | |
---|
[30] | 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 | |
---|
[12] | 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 | |
---|
[16] | 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 | |
---|
[7] | 516 | =head2 form |
---|
| 517 | |
---|
| 518 | my $form = $parser->form; |
---|
| 519 | |
---|
[16] | 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. |
---|
[7] | 523 | |
---|
[1] | 524 | =head2 write |
---|
| 525 | |
---|
[7] | 526 | $parser->write($out_file); |
---|
| 527 | # or just print to STDOUT |
---|
| 528 | $parser->write; |
---|
| 529 | |
---|
[29] | 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. |
---|
[7] | 532 | |
---|
[29] | 533 | CSS Hint: to get multiple sections to all line up their fields, set a |
---|
| 534 | standard width for th.label |
---|
| 535 | |
---|
[12] | 536 | =head2 write_module |
---|
| 537 | |
---|
[16] | 538 | $parser->write_module($package, $use_tidy); |
---|
[12] | 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 |
---|
[16] | 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. |
---|
[12] | 546 | |
---|
[16] | 547 | First, you parse the formspec and write the module, which you can do as a one-liner: |
---|
| 548 | |
---|
[19] | 549 | $ perl -MText::FormBuilder -e"Text::FormBuilder->parse('formspec.txt')->write_module('My::Form')" |
---|
[16] | 550 | |
---|
| 551 | And then, in your CGI script, use the new module: |
---|
| 552 | |
---|
[12] | 553 | #!/usr/bin/perl -w |
---|
| 554 | use strict; |
---|
| 555 | |
---|
| 556 | use CGI; |
---|
[19] | 557 | use My::Form; |
---|
[12] | 558 | |
---|
| 559 | my $q = CGI->new; |
---|
[19] | 560 | my $form = My::Form::get_form($q); |
---|
[12] | 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 | |
---|
[16] | 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 | |
---|
[19] | 573 | # write tidier code |
---|
| 574 | $parser->write_module('My::Form', 1); |
---|
| 575 | |
---|
[7] | 576 | =head2 dump |
---|
| 577 | |
---|
[16] | 578 | Uses L<YAML> to print out a human-readable representation of the parsed |
---|
[7] | 579 | form spec. |
---|
| 580 | |
---|
[1] | 581 | =head1 LANGUAGE |
---|
| 582 | |
---|
[19] | 583 | field_name[size]|descriptive label[hint]:type=default{option1[display string],...}//validate |
---|
[1] | 584 | |
---|
| 585 | !title ... |
---|
| 586 | |
---|
[12] | 587 | !author ... |
---|
| 588 | |
---|
[16] | 589 | !description { |
---|
| 590 | ... |
---|
| 591 | } |
---|
| 592 | |
---|
[1] | 593 | !pattern name /regular expression/ |
---|
[16] | 594 | |
---|
[1] | 595 | !list name { |
---|
[7] | 596 | option1[display string], |
---|
| 597 | option2[display string], |
---|
[1] | 598 | ... |
---|
| 599 | } |
---|
[12] | 600 | |
---|
| 601 | !list name &{ CODE } |
---|
| 602 | |
---|
[29] | 603 | !section id heading |
---|
| 604 | |
---|
[12] | 605 | !head ... |
---|
[1] | 606 | |
---|
| 607 | =head2 Directives |
---|
| 608 | |
---|
| 609 | =over |
---|
| 610 | |
---|
| 611 | =item C<!pattern> |
---|
| 612 | |
---|
[12] | 613 | Defines a validation pattern. |
---|
| 614 | |
---|
[1] | 615 | =item C<!list> |
---|
| 616 | |
---|
[12] | 617 | Defines a list for use in a C<radio>, C<checkbox>, or C<select> field. |
---|
| 618 | |
---|
[1] | 619 | =item C<!title> |
---|
| 620 | |
---|
[7] | 621 | =item C<!author> |
---|
| 622 | |
---|
[16] | 623 | =item C<!description> |
---|
| 624 | |
---|
[19] | 625 | A brief description of the form. Suitable for special instructions on how to |
---|
| 626 | fill out the form. |
---|
| 627 | |
---|
[29] | 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 | |
---|
[12] | 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 | |
---|
[1] | 639 | =back |
---|
| 640 | |
---|
| 641 | =head2 Fields |
---|
| 642 | |
---|
[24] | 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: |
---|
[1] | 647 | |
---|
[24] | 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 | |
---|
[19] | 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 | |
---|
[24] | 671 | The descriptive label can be a multiword string, as described above. |
---|
[19] | 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 |
---|
[23] | 681 | password |
---|
| 682 | file |
---|
| 683 | checkbox |
---|
| 684 | radio |
---|
[19] | 685 | select |
---|
[23] | 686 | hidden |
---|
[19] | 687 | static |
---|
| 688 | |
---|
[21] | 689 | To change the size of the input field, add a bracketed subscript after the |
---|
| 690 | field name (but before the descriptive label): |
---|
[19] | 691 | |
---|
[21] | 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 | |
---|
[24] | 703 | Values are in a comma-separated list of single words or multiword strings |
---|
| 704 | inside curly braces. Whitespace between values is irrelevant. |
---|
[21] | 705 | |
---|
[26] | 706 | To add more descriptive display text to a value in a list, add a square-bracketed |
---|
[19] | 707 | ``subscript,'' as in: |
---|
| 708 | |
---|
| 709 | ...:select{red[Scarlet],blue[Azure],green[Olive Drab]} |
---|
| 710 | |
---|
[1] | 711 | If you have a list of options that is too long to fit comfortably on one line, |
---|
[26] | 712 | you should use the C<!list> directive: |
---|
[1] | 713 | |
---|
[19] | 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 |
---|
[21] | 729 | list, as the example does, or the fancier C<< ( { value1 => 'Description 1'}, |
---|
| 730 | { value2 => 'Description 2}, ... ) >> form. |
---|
[19] | 731 | |
---|
[24] | 732 | I<B<NOTE:> This feature of the language may go away unless I find a compelling |
---|
[19] | 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 |
---|
[24] | 735 | enough with the CGI::FormBuilder object directly.> |
---|
[19] | 736 | |
---|
[26] | 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 | |
---|
[30] | 743 | In this case, the label ``I want to recieve more information'' will be |
---|
| 744 | printed to the right of the checkbox. |
---|
[26] | 745 | |
---|
[19] | 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 | |
---|
[24] | 751 | Default values can also be either single words or multiword strings. |
---|
| 752 | |
---|
[19] | 753 | To validate a field, include a validation type at the end of the field line: |
---|
| 754 | |
---|
| 755 | email|Email address//EMAIL |
---|
| 756 | |
---|
[21] | 757 | Valid validation types include any of the builtin defaults from L<CGI::FormBuilder>, |
---|
[19] | 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 | |
---|
[24] | 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 | |
---|
[1] | 777 | =head2 Comments |
---|
| 778 | |
---|
| 779 | # comment ... |
---|
| 780 | |
---|
| 781 | Any line beginning with a C<#> is considered a comment. |
---|
| 782 | |
---|
[7] | 783 | =head1 TODO |
---|
| 784 | |
---|
[31] | 785 | Allow for custom wrappers around the C<form_template> |
---|
| 786 | |
---|
[23] | 787 | Use the custom message file format for messages in the built in template |
---|
| 788 | |
---|
[30] | 789 | Maybe use HTML::Template instead of Text::Template for the built in template |
---|
[28] | 790 | (since CGI::FormBuilder users may be more likely to already have HTML::Template) |
---|
| 791 | |
---|
[23] | 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 | |
---|
[16] | 796 | C<!include> directive to include external formspec files |
---|
[7] | 797 | |
---|
[19] | 798 | Better tests! |
---|
[16] | 799 | |
---|
[23] | 800 | =head1 BUGS |
---|
| 801 | |
---|
[30] | 802 | I'm sure they're in there, I just haven't tripped over any new ones lately. :-) |
---|
[26] | 803 | |
---|
[1] | 804 | =head1 SEE ALSO |
---|
| 805 | |
---|
| 806 | L<CGI::FormBuilder> |
---|
| 807 | |
---|
[23] | 808 | =head1 THANKS |
---|
| 809 | |
---|
[26] | 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 |
---|
[29] | 812 | sections. |
---|
[23] | 813 | |
---|
[16] | 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 | |
---|
[1] | 825 | =cut |
---|