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