[1] | 1 | package Text::FormBuilder; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | |
---|
[42] | 6 | use base qw(Exporter); |
---|
| 7 | use vars qw($VERSION @EXPORT); |
---|
[1] | 8 | |
---|
[52] | 9 | $VERSION = '0.08_01'; |
---|
[42] | 10 | @EXPORT = qw(create_form); |
---|
[19] | 11 | |
---|
[16] | 12 | use Carp; |
---|
[1] | 13 | use Text::FormBuilder::Parser; |
---|
| 14 | use CGI::FormBuilder; |
---|
| 15 | |
---|
[28] | 16 | # the static default options passed to CGI::FormBuilder->new |
---|
[23] | 17 | my %DEFAULT_OPTIONS = ( |
---|
| 18 | method => 'GET', |
---|
| 19 | javascript => 0, |
---|
| 20 | keepextras => 1, |
---|
| 21 | ); |
---|
| 22 | |
---|
[30] | 23 | # the built in CSS for the template |
---|
| 24 | my $DEFAULT_CSS = <<END; |
---|
| 25 | table { padding: 1em; } |
---|
| 26 | #author, #footer { font-style: italic; } |
---|
| 27 | caption h2 { padding: .125em .5em; background: #ccc; text-align: left; } |
---|
| 28 | th { text-align: left; } |
---|
| 29 | th h3 { padding: .125em .5em; background: #eee; } |
---|
| 30 | th.label { font-weight: normal; text-align: right; vertical-align: top; } |
---|
| 31 | td ul { list-style: none; padding-left: 0; margin-left: 0; } |
---|
[50] | 32 | .note { background: #eee; } |
---|
[30] | 33 | .sublabel { color: #999; } |
---|
| 34 | .invalid { background: red; } |
---|
| 35 | END |
---|
| 36 | |
---|
[34] | 37 | # default messages that can be localized |
---|
[33] | 38 | my %DEFAULT_MESSAGES = ( |
---|
[34] | 39 | text_author => 'Created by %s', |
---|
| 40 | text_madewith => 'Made with %s version %s', |
---|
| 41 | text_required => '(Required fields are marked in <strong>bold</strong>.)', |
---|
| 42 | text_invalid => 'Missing or invalid value.', |
---|
[33] | 43 | ); |
---|
| 44 | |
---|
[34] | 45 | my $DEFAULT_CHARSET = 'iso-8859-1'; |
---|
| 46 | |
---|
[39] | 47 | # options to clean up the code with Perl::Tidy |
---|
| 48 | my $TIDY_OPTIONS = '-nolq -ci=4 -ce'; |
---|
| 49 | |
---|
[42] | 50 | my $HTML_EXTS = qr/\.html?$/; |
---|
[46] | 51 | my $MODULE_EXTS = qr/\.pm$/; |
---|
[42] | 52 | my $SCRIPT_EXTS = qr/\.(pl|cgi)$/; |
---|
| 53 | |
---|
| 54 | # superautomagical exported function |
---|
| 55 | sub create_form { |
---|
| 56 | my ($source, $options, $destination) = @_; |
---|
| 57 | my $parser = __PACKAGE__->parse($source); |
---|
| 58 | $parser->build(%{ $options || {} }); |
---|
| 59 | if ($destination) { |
---|
| 60 | if (ref $destination) { |
---|
| 61 | croak "[Text::FormBuilder::create_form] Don't know what to do with a ref for $destination"; |
---|
| 62 | #TODO: what do ref dests mean? |
---|
| 63 | } else { |
---|
| 64 | # write webpage, script, or module |
---|
[46] | 65 | if ($destination =~ $MODULE_EXTS) { |
---|
[52] | 66 | $parser->write_module($destination, 1); |
---|
[42] | 67 | } elsif ($destination =~ $SCRIPT_EXTS) { |
---|
[52] | 68 | $parser->write_script($destination, 1); |
---|
[42] | 69 | } else { |
---|
[46] | 70 | $parser->write($destination); |
---|
[42] | 71 | } |
---|
| 72 | } |
---|
| 73 | } else { |
---|
| 74 | defined wantarray ? return $parser->form : $parser->write; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
[1] | 79 | sub new { |
---|
| 80 | my $invocant = shift; |
---|
| 81 | my $class = ref $invocant || $invocant; |
---|
| 82 | my $self = { |
---|
| 83 | parser => Text::FormBuilder::Parser->new, |
---|
| 84 | }; |
---|
| 85 | return bless $self, $class; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | sub parse { |
---|
[19] | 89 | my ($self, $source) = @_; |
---|
[42] | 90 | if (my $type = ref $source) { |
---|
| 91 | if ($type eq 'SCALAR') { |
---|
| 92 | $self->parse_text($$source); |
---|
| 93 | } elsif ($type eq 'ARRAY') { |
---|
| 94 | $self->parse_array(@$source); |
---|
| 95 | } else { |
---|
| 96 | croak "[Text::FormBuilder::parse] Unknown ref type $type passed as source"; |
---|
| 97 | } |
---|
[19] | 98 | } else { |
---|
| 99 | $self->parse_file($source); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
[42] | 103 | sub parse_array { |
---|
| 104 | my ($self, @lines) = @_; |
---|
| 105 | # so it can be called as a class method |
---|
| 106 | $self = $self->new unless ref $self; |
---|
| 107 | $self->parse_text(join("\n", @lines)); |
---|
| 108 | return $self; |
---|
| 109 | } |
---|
| 110 | |
---|
[19] | 111 | sub parse_file { |
---|
[1] | 112 | my ($self, $filename) = @_; |
---|
| 113 | |
---|
| 114 | # so it can be called as a class method |
---|
| 115 | $self = $self->new unless ref $self; |
---|
| 116 | |
---|
| 117 | local $/ = undef; |
---|
[32] | 118 | open SRC, "< $filename" or croak "[Text::FormBuilder::parse_file] Can't open $filename: $!" and return; |
---|
[1] | 119 | my $src = <SRC>; |
---|
| 120 | close SRC; |
---|
| 121 | |
---|
| 122 | return $self->parse_text($src); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | sub parse_text { |
---|
| 126 | my ($self, $src) = @_; |
---|
[16] | 127 | |
---|
[1] | 128 | # so it can be called as a class method |
---|
| 129 | $self = $self->new unless ref $self; |
---|
[16] | 130 | |
---|
[32] | 131 | # append a newline so that it can be called on a single field easily |
---|
| 132 | $src .= "\n"; |
---|
| 133 | |
---|
[1] | 134 | $self->{form_spec} = $self->{parser}->form_spec($src); |
---|
[16] | 135 | |
---|
| 136 | # mark structures as not built (newly parsed text) |
---|
| 137 | $self->{built} = 0; |
---|
| 138 | |
---|
[1] | 139 | return $self; |
---|
| 140 | } |
---|
| 141 | |
---|
[33] | 142 | # this is where a lot of the magic happens |
---|
[1] | 143 | sub build { |
---|
| 144 | my ($self, %options) = @_; |
---|
[12] | 145 | |
---|
| 146 | # our custom %options: |
---|
| 147 | # form_only: use only the form part of the template |
---|
| 148 | my $form_only = $options{form_only}; |
---|
[33] | 149 | |
---|
| 150 | # css, extra_css: allow for custom inline stylesheets |
---|
[38] | 151 | # neat trick: css => '@import(my_external_stylesheet.css);' |
---|
[33] | 152 | # will let you use an external stylesheet |
---|
[34] | 153 | # CSS Hint: to get multiple sections to all line up their fields, |
---|
| 154 | # set a standard width for th.label |
---|
[30] | 155 | my $css; |
---|
| 156 | $css = $options{css} || $DEFAULT_CSS; |
---|
| 157 | $css .= $options{extra_css} if $options{extra_css}; |
---|
[12] | 158 | |
---|
[33] | 159 | # messages |
---|
[38] | 160 | # code pulled (with modifications) from CGI::FormBuilder |
---|
[33] | 161 | if ($options{messages}) { |
---|
| 162 | # if its a hashref, we'll just pass it on to CGI::FormBuilder |
---|
| 163 | |
---|
| 164 | if (my $ref = ref $options{messages}) { |
---|
| 165 | # hashref pass on to CGI::FormBuilder |
---|
| 166 | croak "[Text::FormBuilder] Argument to 'messages' option must be a filename or hashref" unless $ref eq 'HASH'; |
---|
| 167 | while (my ($key,$value) = each %DEFAULT_MESSAGES) { |
---|
| 168 | $options{messages}{$key} ||= $DEFAULT_MESSAGES{$key}; |
---|
| 169 | } |
---|
| 170 | } else { |
---|
| 171 | # filename, just *warn* on missing, and use defaults |
---|
| 172 | if (-f $options{messages} && -r _ && open(MESSAGES, "< $options{messages}")) { |
---|
[34] | 173 | $options{messages} = { %DEFAULT_MESSAGES }; |
---|
[33] | 174 | while(<MESSAGES>) { |
---|
| 175 | next if /^\s*#/ || /^\s*$/; |
---|
| 176 | chomp; |
---|
| 177 | my($key,$value) = split ' ', $_, 2; |
---|
| 178 | ($options{messages}{$key} = $value) =~ s/\s+$//; |
---|
| 179 | } |
---|
| 180 | close MESSAGES; |
---|
| 181 | } else { |
---|
[34] | 182 | carp "[Text::FormBuilder] Could not read messages file $options{messages}: $!"; |
---|
[33] | 183 | } |
---|
[1] | 184 | } |
---|
[34] | 185 | } else { |
---|
| 186 | $options{messages} = { %DEFAULT_MESSAGES }; |
---|
[1] | 187 | } |
---|
| 188 | |
---|
[34] | 189 | my $charset = $options{charset}; |
---|
| 190 | |
---|
[33] | 191 | # save the build options so they can be used from write_module |
---|
| 192 | $self->{build_options} = { %options }; |
---|
| 193 | |
---|
| 194 | # remove our custom options before we hand off to CGI::FormBuilder |
---|
[34] | 195 | delete $options{$_} foreach qw(form_only css extra_css charset); |
---|
[33] | 196 | |
---|
[21] | 197 | # expand groups |
---|
[42] | 198 | if (my %groups = %{ $self->{form_spec}{groups} || {} }) { |
---|
| 199 | for my $section (@{ $self->{form_spec}{sections} || [] }) { |
---|
| 200 | foreach (grep { $$_[0] eq 'group' } @{ $$section{lines} }) { |
---|
| 201 | $$_[1]{group} =~ s/^\%//; # strip leading % from group var name |
---|
| 202 | |
---|
| 203 | if (exists $groups{$$_[1]{group}}) { |
---|
| 204 | my @fields; # fields in the group |
---|
| 205 | push @fields, { %$_ } foreach @{ $groups{$$_[1]{group}} }; |
---|
| 206 | for my $field (@fields) { |
---|
| 207 | $$field{label} ||= ucfirst $$field{name}; |
---|
| 208 | $$field{name} = "$$_[1]{name}_$$field{name}"; |
---|
| 209 | } |
---|
| 210 | $_ = [ 'group', { label => $$_[1]{label} || ucfirst(join(' ',split('_',$$_[1]{name}))), group => \@fields } ]; |
---|
[29] | 211 | } |
---|
[21] | 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
[1] | 215 | |
---|
[33] | 216 | # the actual fields that are given to CGI::FormBuilder |
---|
[42] | 217 | # make copies so that when we trim down the sections |
---|
| 218 | # we don't lose the form field information |
---|
[21] | 219 | $self->{form_spec}{fields} = []; |
---|
[29] | 220 | |
---|
| 221 | for my $section (@{ $self->{form_spec}{sections} || [] }) { |
---|
| 222 | for my $line (@{ $$section{lines} }) { |
---|
| 223 | if ($$line[0] eq 'group') { |
---|
[42] | 224 | push @{ $self->{form_spec}{fields} }, { %{$_} } foreach @{ $$line[1]{group} }; |
---|
[29] | 225 | } elsif ($$line[0] eq 'field') { |
---|
[42] | 226 | push @{ $self->{form_spec}{fields} }, { %{$$line[1]} }; |
---|
[29] | 227 | } |
---|
[21] | 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
[39] | 231 | # substitute in custom validation subs and pattern definitions for field validation |
---|
| 232 | my %patterns = %{ $self->{form_spec}{patterns} || {} }; |
---|
| 233 | my %subs = %{ $self->{form_spec}{subs} || {} }; |
---|
| 234 | |
---|
| 235 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
| 236 | if ($$_{validate}) { |
---|
| 237 | if (exists $patterns{$$_{validate}}) { |
---|
[33] | 238 | $$_{validate} = $patterns{$$_{validate}}; |
---|
[39] | 239 | # TODO: need the Data::Dumper code to work for this |
---|
| 240 | # for now, we just warn that it doesn't work |
---|
| 241 | } elsif (exists $subs{$$_{validate}}) { |
---|
| 242 | warn "[Text::FormBuilder] validate coderefs don't work yet"; |
---|
| 243 | delete $$_{validate}; |
---|
[42] | 244 | ## $$_{validate} = $subs{$$_{validate}}; |
---|
[33] | 245 | } |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
[50] | 249 | # get user-defined lists; can't make this conditional because |
---|
| 250 | # we need to be able to fall back to CGI::FormBuilder's lists |
---|
| 251 | # even if the user didn't define any |
---|
| 252 | my %lists = %{ $self->{form_spec}{lists} || {} }; |
---|
| 253 | |
---|
[1] | 254 | # substitute in list names |
---|
[50] | 255 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
| 256 | next unless $$_{list}; |
---|
| 257 | |
---|
| 258 | $$_{list} =~ s/^\@//; # strip leading @ from list var name |
---|
| 259 | |
---|
| 260 | # a hack so we don't get screwy reference errors |
---|
| 261 | if (exists $lists{$$_{list}}) { |
---|
| 262 | my @list; |
---|
| 263 | push @list, { %$_ } foreach @{ $lists{$$_{list}} }; |
---|
| 264 | $$_{options} = \@list; |
---|
| 265 | } else { |
---|
| 266 | # assume that the list name is a builtin |
---|
| 267 | # and let it fall through to CGI::FormBuilder |
---|
| 268 | $$_{options} = $$_{list}; |
---|
[1] | 269 | } |
---|
[50] | 270 | } continue { |
---|
| 271 | delete $$_{list}; |
---|
[30] | 272 | } |
---|
[21] | 273 | |
---|
[30] | 274 | # special case single-value checkboxes |
---|
| 275 | foreach (grep { $$_{type} && $$_{type} eq 'checkbox' } @{ $self->{form_spec}{fields} }) { |
---|
| 276 | unless ($$_{options}) { |
---|
| 277 | $$_{options} = [ { $$_{name} => $$_{label} || ucfirst join(' ',split(/_/,$$_{name})) } ]; |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
[42] | 281 | # use the list for displaying checkbox groups |
---|
[14] | 282 | foreach (@{ $self->{form_spec}{fields} }) { |
---|
[32] | 283 | $$_{ulist} = 1 if ref $$_{options} and @{ $$_{options} } >= 3; |
---|
[14] | 284 | } |
---|
[1] | 285 | |
---|
[24] | 286 | # remove extraneous undefined values |
---|
| 287 | for my $field (@{ $self->{form_spec}{fields} }) { |
---|
| 288 | defined $$field{$_} or delete $$field{$_} foreach keys %{ $field }; |
---|
| 289 | } |
---|
| 290 | |
---|
[33] | 291 | # remove false $$_{required} params because this messes up things at |
---|
| 292 | # the CGI::FormBuilder::field level; it seems to be marking required |
---|
| 293 | # based on the existance of a 'required' param, not whether it is |
---|
| 294 | # true or defined |
---|
[24] | 295 | $$_{required} or delete $$_{required} foreach @{ $self->{form_spec}{fields} }; |
---|
[23] | 296 | |
---|
[39] | 297 | foreach (@{ $self->{form_spec}{sections} }) { |
---|
[42] | 298 | #for my $line (grep { $$_[0] eq 'field' } @{ $$_{lines} }) { |
---|
| 299 | for my $line (@{ $$_{lines} }) { |
---|
| 300 | if ($$line[0] eq 'field') { |
---|
| 301 | $$line[1] = $$line[1]{name}; |
---|
| 302 | ## $_ eq 'name' or delete $$line[1]{$_} foreach keys %{ $$line[1] }; |
---|
| 303 | ## } elsif ($$line[0] eq 'group') { |
---|
| 304 | ## $$line[1] = [ map { $$_{name} } @{ $$line[1]{group} } ]; |
---|
| 305 | } |
---|
[39] | 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
[1] | 309 | $self->{form} = CGI::FormBuilder->new( |
---|
[23] | 310 | %DEFAULT_OPTIONS, |
---|
[33] | 311 | # need to explicity set the fields so that simple text fields get picked up |
---|
[29] | 312 | fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], |
---|
[24] | 313 | required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], |
---|
| 314 | title => $self->{form_spec}{title}, |
---|
[25] | 315 | text => $self->{form_spec}{description}, |
---|
[1] | 316 | template => { |
---|
| 317 | type => 'Text', |
---|
| 318 | engine => { |
---|
| 319 | TYPE => 'STRING', |
---|
[34] | 320 | SOURCE => $form_only ? $self->_form_template : $self->_template($css, $charset), |
---|
[11] | 321 | DELIMITERS => [ qw(<% %>) ], |
---|
[1] | 322 | }, |
---|
| 323 | data => { |
---|
[29] | 324 | sections => $self->{form_spec}{sections}, |
---|
[14] | 325 | author => $self->{form_spec}{author}, |
---|
| 326 | description => $self->{form_spec}{description}, |
---|
[1] | 327 | }, |
---|
| 328 | }, |
---|
| 329 | %options, |
---|
| 330 | ); |
---|
| 331 | $self->{form}->field(%{ $_ }) foreach @{ $self->{form_spec}{fields} }; |
---|
| 332 | |
---|
[16] | 333 | # mark structures as built |
---|
| 334 | $self->{built} = 1; |
---|
| 335 | |
---|
[1] | 336 | return $self; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | sub write { |
---|
| 340 | my ($self, $outfile) = @_; |
---|
[16] | 341 | |
---|
| 342 | # automatically call build if needed to |
---|
| 343 | # allow the new->parse->write shortcut |
---|
| 344 | $self->build unless $self->{built}; |
---|
| 345 | |
---|
[1] | 346 | if ($outfile) { |
---|
| 347 | open FORM, "> $outfile"; |
---|
| 348 | print FORM $self->form->render; |
---|
| 349 | close FORM; |
---|
| 350 | } else { |
---|
| 351 | print $self->form->render; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
[42] | 355 | # generates the core code to create the $form object |
---|
| 356 | # the generated code assumes that you have a CGI.pm |
---|
| 357 | # object named $q |
---|
[39] | 358 | sub _form_code { |
---|
| 359 | my $self = shift; |
---|
[42] | 360 | |
---|
[16] | 361 | # automatically call build if needed to |
---|
| 362 | # allow the new->parse->write shortcut |
---|
| 363 | $self->build unless $self->{built}; |
---|
| 364 | |
---|
| 365 | # conditionally use Data::Dumper |
---|
| 366 | eval 'use Data::Dumper;'; |
---|
| 367 | die "Can't write module; need Data::Dumper. $@" if $@; |
---|
| 368 | |
---|
[33] | 369 | $Data::Dumper::Terse = 1; # don't dump $VARn names |
---|
| 370 | $Data::Dumper::Quotekeys = 0; # don't quote simple string keys |
---|
[12] | 371 | |
---|
[30] | 372 | my $css; |
---|
| 373 | $css = $self->{build_options}{css} || $DEFAULT_CSS; |
---|
| 374 | $css .= $self->{build_options}{extra_css} if $self->{build_options}{extra_css}; |
---|
| 375 | |
---|
[23] | 376 | my %options = ( |
---|
| 377 | %DEFAULT_OPTIONS, |
---|
[24] | 378 | title => $self->{form_spec}{title}, |
---|
[25] | 379 | text => $self->{form_spec}{description}, |
---|
[29] | 380 | fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], |
---|
[24] | 381 | required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], |
---|
[23] | 382 | template => { |
---|
| 383 | type => 'Text', |
---|
| 384 | engine => { |
---|
| 385 | TYPE => 'STRING', |
---|
[34] | 386 | SOURCE => $self->{build_options}{form_only} ? |
---|
| 387 | $self->_form_template : |
---|
| 388 | $self->_template($css, $self->{build_options}{charset}), |
---|
[23] | 389 | DELIMITERS => [ qw(<% %>) ], |
---|
| 390 | }, |
---|
| 391 | data => { |
---|
[29] | 392 | sections => $self->{form_spec}{sections}, |
---|
[23] | 393 | author => $self->{form_spec}{author}, |
---|
| 394 | description => $self->{form_spec}{description}, |
---|
| 395 | }, |
---|
| 396 | }, |
---|
| 397 | %{ $self->{build_options} }, |
---|
| 398 | ); |
---|
| 399 | |
---|
[30] | 400 | # remove our custom options |
---|
| 401 | delete $options{$_} foreach qw(form_only css extra_css); |
---|
[16] | 402 | |
---|
[39] | 403 | my %module_subs; |
---|
| 404 | my $d = Data::Dumper->new([ \%options ], [ '*options' ]); |
---|
[16] | 405 | |
---|
[42] | 406 | use B::Deparse; |
---|
| 407 | my $deparse = B::Deparse->new; |
---|
| 408 | ## |
---|
| 409 | ## #TODO: need a workaround/better solution since Data::Dumper doesn't like dumping coderefs |
---|
[39] | 410 | ## foreach (@{ $self->{form_spec}{fields} }) { |
---|
| 411 | ## if (ref $$_{validate} eq 'CODE') { |
---|
[42] | 412 | ## my $body = $deparse->coderef2text($$_{validate}); |
---|
| 413 | ## #$d->Seen({ "*_validate_$$_{name}" => $$_{validate} }); |
---|
| 414 | ## #$module_subs{$$_{name}} = "sub _validate_$$_{name} $$_{validate}"; |
---|
[39] | 415 | ## } |
---|
[42] | 416 | ## } |
---|
[39] | 417 | ## my $sub_code = join("\n", each %module_subs); |
---|
[42] | 418 | |
---|
[39] | 419 | my $form_options = keys %options > 0 ? $d->Dump : ''; |
---|
| 420 | |
---|
[12] | 421 | my $field_setup = join( |
---|
| 422 | "\n", |
---|
[39] | 423 | map { '$form->field' . Data::Dumper->Dump([$_],['*field']) . ';' } @{ $self->{form_spec}{fields} } |
---|
[12] | 424 | ); |
---|
| 425 | |
---|
[39] | 426 | return <<END; |
---|
| 427 | my \$form = CGI::FormBuilder->new( |
---|
| 428 | params => \$q, |
---|
| 429 | $form_options |
---|
| 430 | ); |
---|
| 431 | |
---|
| 432 | $field_setup |
---|
| 433 | END |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | sub write_module { |
---|
| 437 | my ($self, $package, $use_tidy) = @_; |
---|
| 438 | |
---|
| 439 | croak '[Text::FormBuilder::write_module] Expecting a package name' unless $package; |
---|
| 440 | |
---|
[52] | 441 | # remove a trailing .pm |
---|
| 442 | $package =~ s/\.pm$//; |
---|
| 443 | ## warn "[Text::FromBuilder::write_module] Removed extra '.pm' from package name\n" if $package =~ s/\.pm$//; |
---|
| 444 | |
---|
[39] | 445 | my $form_code = $self->_form_code; |
---|
| 446 | |
---|
[12] | 447 | my $module = <<END; |
---|
| 448 | package $package; |
---|
| 449 | use strict; |
---|
| 450 | use warnings; |
---|
| 451 | |
---|
| 452 | use CGI::FormBuilder; |
---|
| 453 | |
---|
[15] | 454 | sub get_form { |
---|
[39] | 455 | my \$q = shift; |
---|
| 456 | |
---|
| 457 | $form_code |
---|
[12] | 458 | |
---|
[39] | 459 | return \$form; |
---|
[12] | 460 | } |
---|
| 461 | |
---|
| 462 | # module return |
---|
| 463 | 1; |
---|
| 464 | END |
---|
[23] | 465 | |
---|
[42] | 466 | _write_output_file($module, (split(/::/, $package))[-1] . '.pm', $use_tidy); |
---|
[39] | 467 | return $self; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | sub write_script { |
---|
| 471 | my ($self, $script_name, $use_tidy) = @_; |
---|
| 472 | |
---|
| 473 | croak '[Text::FormBuilder::write_script] Expecting a script name' unless $script_name; |
---|
| 474 | |
---|
| 475 | my $form_code = $self->_form_code; |
---|
| 476 | |
---|
| 477 | my $script = <<END; |
---|
| 478 | #!/usr/bin/perl |
---|
| 479 | use strict; |
---|
| 480 | use warnings; |
---|
| 481 | |
---|
| 482 | use CGI; |
---|
| 483 | use CGI::FormBuilder; |
---|
| 484 | |
---|
| 485 | my \$q = CGI->new; |
---|
| 486 | |
---|
| 487 | $form_code |
---|
| 488 | |
---|
| 489 | unless (\$form->submitted && \$form->validate) { |
---|
| 490 | print \$form->render; |
---|
| 491 | } else { |
---|
| 492 | # do something with the entered data |
---|
| 493 | } |
---|
| 494 | END |
---|
| 495 | |
---|
| 496 | _write_output_file($script, $script_name, $use_tidy); |
---|
| 497 | return $self; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | sub _write_output_file { |
---|
| 501 | my ($source_code, $outfile, $use_tidy) = @_; |
---|
[16] | 502 | if ($use_tidy) { |
---|
| 503 | # clean up the generated code, if asked |
---|
| 504 | eval 'use Perl::Tidy'; |
---|
[52] | 505 | unless ($@) { |
---|
| 506 | Perl::Tidy::perltidy(source => \$source_code, destination => $outfile, argv => $TIDY_OPTIONS); |
---|
| 507 | } else { |
---|
| 508 | carp "Can't tidy the code: $@" if $@; |
---|
| 509 | # fallback to just writing it as-is |
---|
| 510 | open OUT, "> $outfile" or die $!; |
---|
| 511 | print OUT $source_code; |
---|
| 512 | close OUT; |
---|
| 513 | } |
---|
[16] | 514 | } else { |
---|
| 515 | # otherwise, just print as is |
---|
[39] | 516 | open OUT, "> $outfile" or die $!; |
---|
| 517 | print OUT $source_code; |
---|
| 518 | close OUT; |
---|
[12] | 519 | } |
---|
| 520 | } |
---|
| 521 | |
---|
[39] | 522 | |
---|
[16] | 523 | sub form { |
---|
| 524 | my $self = shift; |
---|
| 525 | |
---|
| 526 | # automatically call build if needed to |
---|
| 527 | # allow the new->parse->write shortcut |
---|
| 528 | $self->build unless $self->{built}; |
---|
[1] | 529 | |
---|
[16] | 530 | return $self->{form}; |
---|
| 531 | } |
---|
| 532 | |
---|
[12] | 533 | sub _form_template { |
---|
[33] | 534 | my $self = shift; |
---|
[34] | 535 | my $msg_required = $self->{build_options}{messages}{text_required}; |
---|
| 536 | my $msg_invalid = $self->{build_options}{messages}{text_invalid}; |
---|
| 537 | return q{<% $description ? qq[<p id="description">$description</p>] : '' %> |
---|
| 538 | <% (grep { $_->{required} } @fields) ? qq[<p id="instructions">} . $msg_required . q{</p>] : '' %> |
---|
[12] | 539 | <% $start %> |
---|
[23] | 540 | <% |
---|
| 541 | # drop in the hidden fields here |
---|
| 542 | $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); |
---|
[34] | 543 | %>} . |
---|
| 544 | q[ |
---|
[29] | 545 | <% |
---|
| 546 | SECTION: while (my $section = shift @sections) { |
---|
[30] | 547 | $OUT .= qq[<table id="] . ($$section{id} || '_default') . qq[">\n]; |
---|
| 548 | $OUT .= qq[ <caption><h2 class="sectionhead">$$section{head}</h2></caption>] if $$section{head}; |
---|
[29] | 549 | TABLE_LINE: for my $line (@{ $$section{lines} }) { |
---|
| 550 | if ($$line[0] eq 'head') { |
---|
[30] | 551 | $OUT .= qq[ <tr><th class="subhead" colspan="2"><h3>$$line[1]</h3></th></tr>\n] |
---|
[50] | 552 | } elsif ($$line[0] eq 'note') { |
---|
| 553 | $OUT .= qq[ <tr><td class="note" colspan="2">$$line[1]</td></tr>\n] |
---|
[29] | 554 | } elsif ($$line[0] eq 'field') { |
---|
[42] | 555 | local $_ = $field{$$line[1]}; |
---|
[34] | 556 | |
---|
[29] | 557 | # skip hidden fields in the table |
---|
| 558 | next TABLE_LINE if $$_{type} eq 'hidden'; |
---|
| 559 | |
---|
| 560 | $OUT .= $$_{invalid} ? qq[ <tr class="invalid">] : qq[ <tr>]; |
---|
[30] | 561 | |
---|
| 562 | # special case single value checkboxes |
---|
| 563 | if ($$_{type} eq 'checkbox' && @{ $$_{options} } == 1) { |
---|
| 564 | $OUT .= qq[<th></th>]; |
---|
| 565 | } else { |
---|
| 566 | $OUT .= '<th class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}:</strong>] : "$$_{label}:") . '</th>'; |
---|
| 567 | } |
---|
[33] | 568 | |
---|
| 569 | # mark invalid fields |
---|
[29] | 570 | if ($$_{invalid}) { |
---|
[34] | 571 | $OUT .= "<td>$$_{field} $$_{comment} ] . $msg_invalid . q[</td>"; |
---|
[29] | 572 | } else { |
---|
[30] | 573 | $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; |
---|
[29] | 574 | } |
---|
[33] | 575 | |
---|
[30] | 576 | $OUT .= qq[</tr>\n]; |
---|
| 577 | |
---|
[29] | 578 | } elsif ($$line[0] eq 'group') { |
---|
[42] | 579 | my @group_fields = map { $field{$_} } map { $$_{name} } @{ $$line[1]{group} }; |
---|
[29] | 580 | $OUT .= (grep { $$_{invalid} } @group_fields) ? qq[ <tr class="invalid">\n] : qq[ <tr>\n]; |
---|
| 581 | |
---|
| 582 | $OUT .= ' <th class="label">'; |
---|
| 583 | $OUT .= (grep { $$_{required} } @group_fields) ? qq[<strong class="required">$$line[1]{label}:</strong>] : "$$line[1]{label}:"; |
---|
| 584 | $OUT .= qq[</th>\n]; |
---|
| 585 | |
---|
| 586 | $OUT .= qq[ <td>]; |
---|
| 587 | $OUT .= join(' ', map { qq[<small class="sublabel">$$_{label}</small> $$_{field} $$_{comment}] } @group_fields); |
---|
[42] | 588 | $OUT .= " $msg_invalid" if $$_{invalid}; |
---|
| 589 | |
---|
[29] | 590 | $OUT .= qq[ </td>\n]; |
---|
| 591 | $OUT .= qq[ </tr>\n]; |
---|
| 592 | } |
---|
[21] | 593 | } |
---|
[29] | 594 | # close the table if there are sections remaining |
---|
| 595 | # but leave the last one open for the submit button |
---|
| 596 | $OUT .= qq[</table>\n] if @sections; |
---|
| 597 | } |
---|
| 598 | %> |
---|
[12] | 599 | <tr><th></th><td style="padding-top: 1em;"><% $submit %></td></tr> |
---|
| 600 | </table> |
---|
| 601 | <% $end %> |
---|
| 602 | ]; |
---|
| 603 | } |
---|
| 604 | |
---|
[34] | 605 | # usage: $self->_pre_template($css, $charset) |
---|
[33] | 606 | sub _pre_template { |
---|
[12] | 607 | my $self = shift; |
---|
[30] | 608 | my $css = shift || $DEFAULT_CSS; |
---|
[34] | 609 | my $charset = shift || $DEFAULT_CHARSET; |
---|
| 610 | my $msg_author = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_author}) . '", $author)'; |
---|
[33] | 611 | return |
---|
[12] | 612 | q[<html> |
---|
[1] | 613 | <head> |
---|
[34] | 614 | <meta http-equiv="Content-Type" content="text/html; charset=] . $charset . q[" /> |
---|
[1] | 615 | <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> |
---|
[33] | 616 | <style type="text/css"> |
---|
[42] | 617 | ] . $css . q[ </style> |
---|
[33] | 618 | <% $jshead %> |
---|
[1] | 619 | </head> |
---|
| 620 | <body> |
---|
| 621 | |
---|
| 622 | <h1><% $title %></h1> |
---|
[34] | 623 | <% $author ? qq[<p id="author">] . ] . $msg_author . q{ . q[</p>] : '' %> |
---|
| 624 | }; |
---|
[33] | 625 | } |
---|
| 626 | |
---|
| 627 | sub _post_template { |
---|
[34] | 628 | my $self = shift; |
---|
| 629 | my $msg_madewith = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_madewith}) . |
---|
| 630 | '", q[<a href="http://formbuilder.org/">CGI::FormBuilder</a>], CGI::FormBuilder->VERSION)'; |
---|
| 631 | |
---|
| 632 | return qq[<hr /> |
---|
[1] | 633 | <div id="footer"> |
---|
[34] | 634 | <p id="creator"><% $msg_madewith %></p> |
---|
[1] | 635 | </div> |
---|
| 636 | </body> |
---|
| 637 | </html> |
---|
| 638 | ]; |
---|
| 639 | } |
---|
| 640 | |
---|
[42] | 641 | # usage: $self->_template($css, $charset) |
---|
[33] | 642 | sub _template { |
---|
| 643 | my $self = shift; |
---|
[34] | 644 | return $self->_pre_template(@_) . $self->_form_template . $self->_post_template; |
---|
[33] | 645 | } |
---|
| 646 | |
---|
[7] | 647 | sub dump { |
---|
| 648 | eval "use YAML;"; |
---|
| 649 | unless ($@) { |
---|
| 650 | print YAML::Dump(shift->{form_spec}); |
---|
| 651 | } else { |
---|
| 652 | warn "Can't dump form spec structure: $@"; |
---|
| 653 | } |
---|
| 654 | } |
---|
[1] | 655 | |
---|
| 656 | |
---|
| 657 | # module return |
---|
| 658 | 1; |
---|
| 659 | |
---|
| 660 | =head1 NAME |
---|
| 661 | |
---|
[21] | 662 | Text::FormBuilder - Create CGI::FormBuilder objects from simple text descriptions |
---|
[1] | 663 | |
---|
| 664 | =head1 SYNOPSIS |
---|
| 665 | |
---|
[16] | 666 | use Text::FormBuilder; |
---|
| 667 | |
---|
[1] | 668 | my $parser = Text::FormBuilder->new; |
---|
| 669 | $parser->parse($src_file); |
---|
| 670 | |
---|
[16] | 671 | # returns a new CGI::FormBuilder object with |
---|
| 672 | # the fields from the input form spec |
---|
[7] | 673 | my $form = $parser->form; |
---|
[19] | 674 | |
---|
| 675 | # write a My::Form module to Form.pm |
---|
| 676 | $parser->write_module('My::Form'); |
---|
[1] | 677 | |
---|
[23] | 678 | =head1 REQUIRES |
---|
| 679 | |
---|
| 680 | L<Parse::RecDescent>, L<CGI::FormBuilder>, L<Text::Template> |
---|
| 681 | |
---|
[1] | 682 | =head1 DESCRIPTION |
---|
| 683 | |
---|
[38] | 684 | This module is intended to extend the idea of making it easy to create |
---|
| 685 | web forms by allowing you to describe them with a simple langauge. These |
---|
| 686 | I<formspecs> are then passed through this module's parser and converted |
---|
| 687 | into L<CGI::FormBuilder> objects that you can easily use in your CGI |
---|
| 688 | scripts. In addition, this module can generate code for standalone modules |
---|
| 689 | which allow you to separate your form design from your script code. |
---|
| 690 | |
---|
| 691 | A simple formspec looks like this: |
---|
| 692 | |
---|
| 693 | name//VALUE |
---|
| 694 | email//EMAIL |
---|
| 695 | langauge:select{English,Spanish,French,German} |
---|
| 696 | moreinfo|Send me more information:checkbox |
---|
| 697 | interests:checkbox{Perl,karate,bass guitar} |
---|
| 698 | |
---|
| 699 | This will produce a required C<name> test field, a required C<email> text |
---|
| 700 | field that must look like an email address, an optional select dropdown |
---|
| 701 | field C<langauge> with the choices English, Spanish, French, and German, |
---|
| 702 | an optional C<moreinfo> checkbox labeled ``Send me more information'', and |
---|
| 703 | finally a set of checkboxes named C<interests> with the choices Perl, |
---|
| 704 | karate, and bass guitar. |
---|
| 705 | |
---|
[46] | 706 | =head1 METHODS |
---|
| 707 | |
---|
[1] | 708 | =head2 new |
---|
| 709 | |
---|
[38] | 710 | my $parser = Text::FormBuilder->new; |
---|
| 711 | |
---|
[1] | 712 | =head2 parse |
---|
| 713 | |
---|
[42] | 714 | # parse a file (regular scalar) |
---|
[19] | 715 | $parser->parse($filename); |
---|
[7] | 716 | |
---|
[19] | 717 | # or pass a scalar ref for parse a literal string |
---|
| 718 | $parser->parse(\$string); |
---|
[42] | 719 | |
---|
| 720 | # or an array ref to parse lines |
---|
| 721 | $parser->parse(\@lines); |
---|
[19] | 722 | |
---|
[42] | 723 | Parse the file or string. Returns the parser object. This method, |
---|
| 724 | along with all of its C<parse_*> siblings, may be called as a class |
---|
| 725 | method to construct a new object. |
---|
[19] | 726 | |
---|
| 727 | =head2 parse_file |
---|
| 728 | |
---|
| 729 | $parser->parse_file($src_file); |
---|
| 730 | |
---|
[7] | 731 | # or as a class method |
---|
[16] | 732 | my $parser = Text::FormBuilder->parse($src_file); |
---|
[7] | 733 | |
---|
| 734 | =head2 parse_text |
---|
| 735 | |
---|
[16] | 736 | $parser->parse_text($src); |
---|
| 737 | |
---|
[19] | 738 | Parse the given C<$src> text. Returns the parser object. |
---|
[16] | 739 | |
---|
[42] | 740 | =head2 parse_array |
---|
| 741 | |
---|
| 742 | $parser->parse_array(@lines); |
---|
| 743 | |
---|
| 744 | Concatenates and parses C<@lines>. Returns the parser object. |
---|
| 745 | |
---|
[1] | 746 | =head2 build |
---|
| 747 | |
---|
[12] | 748 | $parser->build(%options); |
---|
[7] | 749 | |
---|
[12] | 750 | Builds the CGI::FormBuilder object. Options directly used by C<build> are: |
---|
| 751 | |
---|
| 752 | =over |
---|
| 753 | |
---|
[19] | 754 | =item C<form_only> |
---|
[12] | 755 | |
---|
| 756 | Only uses the form portion of the template, and omits the surrounding html, |
---|
[19] | 757 | title, author, and the standard footer. This does, however, include the |
---|
| 758 | description as specified with the C<!description> directive. |
---|
[12] | 759 | |
---|
[30] | 760 | =item C<css>, C<extra_css> |
---|
| 761 | |
---|
| 762 | These options allow you to tell Text::FormBuilder to use different |
---|
| 763 | CSS styles for the built in template. A value given a C<css> will |
---|
| 764 | replace the existing CSS, and a value given as C<extra_css> will be |
---|
| 765 | appended to the CSS. If both options are given, then the CSS that is |
---|
| 766 | used will be C<css> concatenated with C<extra_css>. |
---|
| 767 | |
---|
[38] | 768 | If you want to use an external stylesheet, a quick way to get this is |
---|
| 769 | to set the C<css> parameter to import your file: |
---|
| 770 | |
---|
| 771 | css => '@import(my_external_stylesheet.css);' |
---|
| 772 | |
---|
[34] | 773 | =item C<messages> |
---|
| 774 | |
---|
| 775 | This works the same way as the C<messages> parameter to |
---|
| 776 | C<< CGI::FormBuilder->new >>; you can provide either a hashref of messages |
---|
| 777 | or a filename. |
---|
| 778 | |
---|
| 779 | The default messages used by Text::FormBuilder are: |
---|
| 780 | |
---|
| 781 | text_author Created by %s |
---|
| 782 | text_madewith Made with %s version %s |
---|
| 783 | text_required (Required fields are marked in <strong>bold</strong>.) |
---|
| 784 | text_invalid Missing or invalid value. |
---|
| 785 | |
---|
| 786 | Any messages you set here get passed on to CGI::FormBuilder, which means |
---|
| 787 | that you should be able to put all of your customization messages in one |
---|
| 788 | big file. |
---|
| 789 | |
---|
| 790 | =item C<charset> |
---|
| 791 | |
---|
| 792 | Sets the character encoding for the generated page. The default is ISO-8859-1. |
---|
| 793 | |
---|
[12] | 794 | =back |
---|
| 795 | |
---|
| 796 | All other options given to C<build> are passed on verbatim to the |
---|
| 797 | L<CGI::FormBuilder> constructor. Any options given here override the |
---|
| 798 | defaults that this module uses. |
---|
| 799 | |
---|
[16] | 800 | The C<form>, C<write>, and C<write_module> methods will all call |
---|
| 801 | C<build> with no options for you if you do not do so explicitly. |
---|
| 802 | This allows you to say things like this: |
---|
| 803 | |
---|
| 804 | my $form = Text::FormBuilder->new->parse('formspec.txt')->form; |
---|
| 805 | |
---|
| 806 | However, if you need to specify options to C<build>, you must call it |
---|
| 807 | explictly after C<parse>. |
---|
| 808 | |
---|
[7] | 809 | =head2 form |
---|
| 810 | |
---|
| 811 | my $form = $parser->form; |
---|
| 812 | |
---|
[16] | 813 | Returns the L<CGI::FormBuilder> object. Remember that you can modify |
---|
| 814 | this object directly, in order to (for example) dynamically populate |
---|
| 815 | dropdown lists or change input types at runtime. |
---|
[7] | 816 | |
---|
[1] | 817 | =head2 write |
---|
| 818 | |
---|
[7] | 819 | $parser->write($out_file); |
---|
| 820 | # or just print to STDOUT |
---|
| 821 | $parser->write; |
---|
| 822 | |
---|
[29] | 823 | Calls C<render> on the FormBuilder form, and either writes the resulting |
---|
| 824 | HTML to a file, or to STDOUT if no filename is given. |
---|
[7] | 825 | |
---|
[12] | 826 | =head2 write_module |
---|
| 827 | |
---|
[16] | 828 | $parser->write_module($package, $use_tidy); |
---|
[12] | 829 | |
---|
| 830 | Takes a package name, and writes out a new module that can be used by your |
---|
| 831 | CGI script to render the form. This way, you only need CGI::FormBuilder on |
---|
| 832 | your server, and you don't have to parse the form spec each time you want |
---|
[16] | 833 | to display your form. The generated module has one function (not exported) |
---|
| 834 | called C<get_form>, that takes a CGI object as its only argument, and returns |
---|
| 835 | a CGI::FormBuilder object. |
---|
[12] | 836 | |
---|
[16] | 837 | First, you parse the formspec and write the module, which you can do as a one-liner: |
---|
| 838 | |
---|
[19] | 839 | $ perl -MText::FormBuilder -e"Text::FormBuilder->parse('formspec.txt')->write_module('My::Form')" |
---|
[16] | 840 | |
---|
| 841 | And then, in your CGI script, use the new module: |
---|
| 842 | |
---|
[12] | 843 | #!/usr/bin/perl -w |
---|
| 844 | use strict; |
---|
| 845 | |
---|
| 846 | use CGI; |
---|
[19] | 847 | use My::Form; |
---|
[12] | 848 | |
---|
| 849 | my $q = CGI->new; |
---|
[19] | 850 | my $form = My::Form::get_form($q); |
---|
[12] | 851 | |
---|
| 852 | # do the standard CGI::FormBuilder stuff |
---|
| 853 | if ($form->submitted && $form->validate) { |
---|
| 854 | # process results |
---|
| 855 | } else { |
---|
| 856 | print $q->header; |
---|
| 857 | print $form->render; |
---|
| 858 | } |
---|
| 859 | |
---|
[16] | 860 | If you pass a true value as the second argument to C<write_module>, the parser |
---|
| 861 | will run L<Perl::Tidy> on the generated code before writing the module file. |
---|
| 862 | |
---|
[19] | 863 | # write tidier code |
---|
| 864 | $parser->write_module('My::Form', 1); |
---|
| 865 | |
---|
[39] | 866 | =head2 write_script |
---|
| 867 | |
---|
| 868 | $parser->write_script($filename, $use_tidy); |
---|
| 869 | |
---|
| 870 | If you don't need the reuseability of a separate module, you can have |
---|
| 871 | Text::FormBuilder write the form object to a script for you, along with |
---|
| 872 | the simplest framework for using it, to which you can add your actual |
---|
| 873 | form processing code. |
---|
| 874 | |
---|
| 875 | The generated script looks like this: |
---|
| 876 | |
---|
| 877 | #!/usr/bin/perl |
---|
| 878 | use strict; |
---|
| 879 | use warnings; |
---|
| 880 | |
---|
| 881 | use CGI; |
---|
| 882 | use CGI::FormBuilder; |
---|
| 883 | |
---|
| 884 | my $q = CGI->new; |
---|
| 885 | |
---|
| 886 | my $form = CGI::FormBuilder->new( |
---|
| 887 | params => $q, |
---|
| 888 | # ... lots of other stuff to set up the form ... |
---|
| 889 | ); |
---|
| 890 | |
---|
| 891 | $form->field( name => 'month' ); |
---|
| 892 | $form->field( name => 'day' ); |
---|
| 893 | |
---|
| 894 | unless ( $form->submitted && $form->validate ) { |
---|
| 895 | print $form->render; |
---|
| 896 | } else { |
---|
| 897 | # do something with the entered data ... |
---|
| 898 | # this is where your form processing code should go |
---|
| 899 | } |
---|
| 900 | |
---|
| 901 | Like C<write_module>, you can optionally pass a true value as the second |
---|
| 902 | argument to have Perl::Tidy make the generated code look nicer. |
---|
| 903 | |
---|
[7] | 904 | =head2 dump |
---|
| 905 | |
---|
[16] | 906 | Uses L<YAML> to print out a human-readable representation of the parsed |
---|
[7] | 907 | form spec. |
---|
| 908 | |
---|
[46] | 909 | =head1 EXPORTS |
---|
| 910 | |
---|
| 911 | There is one exported function, C<create_form>, that is intended to ``do the |
---|
| 912 | right thing'' in simple cases. |
---|
| 913 | |
---|
| 914 | =head2 create_form |
---|
| 915 | |
---|
| 916 | # get a CGI::FormBuilder object |
---|
| 917 | my $form = create_form($source, $options, $destination); |
---|
| 918 | |
---|
| 919 | # or just write the form immediately |
---|
| 920 | create_form($source, $options, $destination); |
---|
| 921 | |
---|
| 922 | C<$source> accepts any of the types of arguments that C<parse> does. C<$options> |
---|
| 923 | is a hashref of options that should be passed to C<build>. Finally, C<$destination> |
---|
| 924 | is a simple scalar that determines where and what type of output C<create_form> |
---|
| 925 | should generate. |
---|
| 926 | |
---|
| 927 | /\.pm$/ ->write_module($destination) |
---|
| 928 | /\.(cgi|pl)$/ ->write_script($destination) |
---|
| 929 | everything else ->write($destination) |
---|
| 930 | |
---|
| 931 | For anything more than simple, one-off cases, you are usually better off using the |
---|
| 932 | object-oriented interface, since that gives you more control over things. |
---|
| 933 | |
---|
[33] | 934 | =head1 DEFAULTS |
---|
| 935 | |
---|
| 936 | These are the default settings that are passed to C<< CGI::FormBuilder->new >>: |
---|
| 937 | |
---|
| 938 | method => 'GET' |
---|
| 939 | javascript => 0 |
---|
| 940 | keepextras => 1 |
---|
| 941 | |
---|
| 942 | Any of these can be overriden by the C<build> method: |
---|
| 943 | |
---|
| 944 | # use POST instead |
---|
| 945 | $parser->build(method => 'POST')->write; |
---|
| 946 | |
---|
[1] | 947 | =head1 LANGUAGE |
---|
| 948 | |
---|
[19] | 949 | field_name[size]|descriptive label[hint]:type=default{option1[display string],...}//validate |
---|
[1] | 950 | |
---|
| 951 | !title ... |
---|
| 952 | |
---|
[12] | 953 | !author ... |
---|
| 954 | |
---|
[16] | 955 | !description { |
---|
| 956 | ... |
---|
| 957 | } |
---|
| 958 | |
---|
[42] | 959 | !pattern NAME /regular expression/ |
---|
[16] | 960 | |
---|
[42] | 961 | !list NAME { |
---|
[7] | 962 | option1[display string], |
---|
| 963 | option2[display string], |
---|
[1] | 964 | ... |
---|
| 965 | } |
---|
[12] | 966 | |
---|
[42] | 967 | !list NAME &{ CODE } |
---|
[12] | 968 | |
---|
[42] | 969 | !group NAME { |
---|
| 970 | field1 |
---|
| 971 | field2 |
---|
| 972 | ... |
---|
| 973 | } |
---|
| 974 | |
---|
[29] | 975 | !section id heading |
---|
| 976 | |
---|
[12] | 977 | !head ... |
---|
[50] | 978 | |
---|
| 979 | !note { |
---|
| 980 | ... |
---|
| 981 | } |
---|
[1] | 982 | |
---|
| 983 | =head2 Directives |
---|
| 984 | |
---|
| 985 | =over |
---|
| 986 | |
---|
| 987 | =item C<!pattern> |
---|
| 988 | |
---|
[12] | 989 | Defines a validation pattern. |
---|
| 990 | |
---|
[1] | 991 | =item C<!list> |
---|
| 992 | |
---|
[12] | 993 | Defines a list for use in a C<radio>, C<checkbox>, or C<select> field. |
---|
| 994 | |
---|
[42] | 995 | =item C<!group> |
---|
| 996 | |
---|
| 997 | Define a named group of fields that are displayed all on one line. Use with |
---|
| 998 | the C<!field> directive. |
---|
| 999 | |
---|
| 1000 | =item C<!field> |
---|
| 1001 | |
---|
| 1002 | Include a named instance of a group defined with C<!group>. |
---|
| 1003 | |
---|
[1] | 1004 | =item C<!title> |
---|
| 1005 | |
---|
[50] | 1006 | Title of the form. |
---|
| 1007 | |
---|
[7] | 1008 | =item C<!author> |
---|
| 1009 | |
---|
[50] | 1010 | Author of the form. |
---|
| 1011 | |
---|
[16] | 1012 | =item C<!description> |
---|
| 1013 | |
---|
[19] | 1014 | A brief description of the form. Suitable for special instructions on how to |
---|
| 1015 | fill out the form. |
---|
| 1016 | |
---|
[29] | 1017 | =item C<!section> |
---|
| 1018 | |
---|
| 1019 | Starts a new section. Each section has its own heading and id, which are |
---|
| 1020 | written by default into spearate tables. |
---|
| 1021 | |
---|
[12] | 1022 | =item C<!head> |
---|
| 1023 | |
---|
| 1024 | Inserts a heading between two fields. There can only be one heading between |
---|
| 1025 | any two fields; the parser will warn you if you try to put two headings right |
---|
| 1026 | next to each other. |
---|
| 1027 | |
---|
[50] | 1028 | =item C<!note> |
---|
| 1029 | |
---|
| 1030 | A text note that can be inserted as a row in the form. This is useful for |
---|
| 1031 | special instructions at specific points in a long form. |
---|
| 1032 | |
---|
[1] | 1033 | =back |
---|
| 1034 | |
---|
[50] | 1035 | B<Known BUG:> If you include an odd number of C<'> or C<"> characters in a |
---|
| 1036 | C<!description> or C<!note>, then that directive will mistakenly be skipped. |
---|
| 1037 | This is a bug casued by me taking a shortcut in the parser C<:-/> |
---|
| 1038 | |
---|
[1] | 1039 | =head2 Fields |
---|
| 1040 | |
---|
[24] | 1041 | First, a note about multiword strings in the fields. Anywhere where it says |
---|
| 1042 | that you may use a multiword string, this means that you can do one of two |
---|
| 1043 | things. For strings that consist solely of alphanumeric characters (i.e. |
---|
| 1044 | C<\w+>) and spaces, the string will be recognized as is: |
---|
[1] | 1045 | |
---|
[24] | 1046 | field_1|A longer label |
---|
| 1047 | |
---|
| 1048 | If you want to include non-alphanumerics (e.g. punctuation), you must |
---|
| 1049 | single-quote the string: |
---|
| 1050 | |
---|
| 1051 | field_2|'Dept./Org.' |
---|
| 1052 | |
---|
| 1053 | To include a literal single-quote in a single-quoted string, escape it with |
---|
| 1054 | a backslash: |
---|
| 1055 | |
---|
| 1056 | field_3|'\'Official\' title' |
---|
| 1057 | |
---|
[34] | 1058 | Now, back to the beginning. Form fields are each described on a single line. |
---|
[24] | 1059 | The simplest field is just a name (which cannot contain any whitespace): |
---|
| 1060 | |
---|
[19] | 1061 | color |
---|
| 1062 | |
---|
| 1063 | This yields a form with one text input field of the default size named `color'. |
---|
[34] | 1064 | The generated label for this field would be ``Color''. To add a longer or more\ |
---|
| 1065 | descriptive label, use: |
---|
[19] | 1066 | |
---|
| 1067 | color|Favorite color |
---|
| 1068 | |
---|
[34] | 1069 | The descriptive label can be a multiword string, as described above. So if you |
---|
| 1070 | want punctuation in the label, you should single quote it: |
---|
[19] | 1071 | |
---|
[34] | 1072 | color|'Fav. color' |
---|
| 1073 | |
---|
[19] | 1074 | To use a different input type: |
---|
| 1075 | |
---|
| 1076 | color|Favorite color:select{red,blue,green} |
---|
| 1077 | |
---|
| 1078 | Recognized input types are the same as those used by CGI::FormBuilder: |
---|
| 1079 | |
---|
| 1080 | text # the default |
---|
| 1081 | textarea |
---|
[23] | 1082 | password |
---|
| 1083 | file |
---|
| 1084 | checkbox |
---|
| 1085 | radio |
---|
[19] | 1086 | select |
---|
[23] | 1087 | hidden |
---|
[19] | 1088 | static |
---|
| 1089 | |
---|
[21] | 1090 | To change the size of the input field, add a bracketed subscript after the |
---|
| 1091 | field name (but before the descriptive label): |
---|
[19] | 1092 | |
---|
[21] | 1093 | # for a single line field, sets size="40" |
---|
| 1094 | title[40]:text |
---|
| 1095 | |
---|
| 1096 | # for a multiline field, sets rows="4" and cols="30" |
---|
| 1097 | description[4,30]:textarea |
---|
| 1098 | |
---|
| 1099 | For the input types that can have options (C<select>, C<radio>, and |
---|
| 1100 | C<checkbox>), here's how you do it: |
---|
| 1101 | |
---|
| 1102 | color|Favorite color:select{red,blue,green} |
---|
| 1103 | |
---|
[24] | 1104 | Values are in a comma-separated list of single words or multiword strings |
---|
| 1105 | inside curly braces. Whitespace between values is irrelevant. |
---|
[21] | 1106 | |
---|
[26] | 1107 | To add more descriptive display text to a value in a list, add a square-bracketed |
---|
[19] | 1108 | ``subscript,'' as in: |
---|
| 1109 | |
---|
| 1110 | ...:select{red[Scarlet],blue[Azure],green[Olive Drab]} |
---|
| 1111 | |
---|
[1] | 1112 | If you have a list of options that is too long to fit comfortably on one line, |
---|
[26] | 1113 | you should use the C<!list> directive: |
---|
[1] | 1114 | |
---|
[19] | 1115 | !list MONTHS { |
---|
| 1116 | 1[January], |
---|
| 1117 | 2[February], |
---|
| 1118 | 3[March], |
---|
| 1119 | # and so on... |
---|
| 1120 | } |
---|
| 1121 | |
---|
| 1122 | month:select@MONTHS |
---|
| 1123 | |
---|
| 1124 | There is another form of the C<!list> directive: the dynamic list: |
---|
| 1125 | |
---|
| 1126 | !list RANDOM &{ map { rand } (0..5) } |
---|
| 1127 | |
---|
| 1128 | The code inside the C<&{ ... }> is C<eval>ed by C<build>, and the results |
---|
| 1129 | are stuffed into the list. The C<eval>ed code can either return a simple |
---|
[21] | 1130 | list, as the example does, or the fancier C<< ( { value1 => 'Description 1'}, |
---|
| 1131 | { value2 => 'Description 2}, ... ) >> form. |
---|
[19] | 1132 | |
---|
[24] | 1133 | I<B<NOTE:> This feature of the language may go away unless I find a compelling |
---|
[19] | 1134 | reason for it in the next few versions. What I really wanted was lists that |
---|
| 1135 | were filled in at run-time (e.g. from a database), and that can be done easily |
---|
[24] | 1136 | enough with the CGI::FormBuilder object directly.> |
---|
[19] | 1137 | |
---|
[26] | 1138 | If you want to have a single checkbox (e.g. for a field that says ``I want to |
---|
| 1139 | recieve more information''), you can just specify the type as checkbox without |
---|
| 1140 | supplying any options: |
---|
| 1141 | |
---|
| 1142 | moreinfo|I want to recieve more information:checkbox |
---|
| 1143 | |
---|
[30] | 1144 | In this case, the label ``I want to recieve more information'' will be |
---|
| 1145 | printed to the right of the checkbox. |
---|
[26] | 1146 | |
---|
[19] | 1147 | You can also supply a default value to the field. To get a default value of |
---|
| 1148 | C<green> for the color field: |
---|
| 1149 | |
---|
| 1150 | color|Favorite color:select=green{red,blue,green} |
---|
| 1151 | |
---|
[24] | 1152 | Default values can also be either single words or multiword strings. |
---|
| 1153 | |
---|
[19] | 1154 | To validate a field, include a validation type at the end of the field line: |
---|
| 1155 | |
---|
| 1156 | email|Email address//EMAIL |
---|
| 1157 | |
---|
[21] | 1158 | Valid validation types include any of the builtin defaults from L<CGI::FormBuilder>, |
---|
[19] | 1159 | or the name of a pattern that you define with the C<!pattern> directive elsewhere |
---|
| 1160 | in your form spec: |
---|
| 1161 | |
---|
| 1162 | !pattern DAY /^([1-3][0-9])|[1-9]$/ |
---|
| 1163 | |
---|
| 1164 | last_day//DAY |
---|
| 1165 | |
---|
| 1166 | If you just want a required value, use the builtin validation type C<VALUE>: |
---|
| 1167 | |
---|
| 1168 | title//VALUE |
---|
| 1169 | |
---|
[24] | 1170 | By default, adding a validation type to a field makes that field required. To |
---|
| 1171 | change this, add a C<?> to the end of the validation type: |
---|
| 1172 | |
---|
| 1173 | contact//EMAIL? |
---|
| 1174 | |
---|
| 1175 | In this case, you would get a C<contact> field that was optional, but if it |
---|
| 1176 | were filled in, would have to validate as an C<EMAIL>. |
---|
| 1177 | |
---|
[42] | 1178 | =head2 Field Groups |
---|
| 1179 | |
---|
| 1180 | You can define groups of fields using the C<!group> directive: |
---|
| 1181 | |
---|
| 1182 | !group DATE { |
---|
| 1183 | month:select@MONTHS//INT |
---|
| 1184 | day[2]//INT |
---|
| 1185 | year[4]//INT |
---|
| 1186 | } |
---|
| 1187 | |
---|
| 1188 | You can then include instances of this group using the C<!field> directive: |
---|
| 1189 | |
---|
| 1190 | !field %DATE birthday |
---|
| 1191 | |
---|
| 1192 | This will create a line in the form labeled ``Birthday'' which contains |
---|
| 1193 | a month dropdown, and day and year text entry fields. The actual input field |
---|
| 1194 | names are formed by concatenating the C<!field> name (e.g. C<birthday>) with |
---|
| 1195 | the name of the subfield defined in the group (e.g. C<month>, C<day>, C<year>). |
---|
| 1196 | Thus in this example, you would end up with the form fields C<birthday_month>, |
---|
| 1197 | C<birthday_day>, and C<birthday_year>. |
---|
| 1198 | |
---|
[1] | 1199 | =head2 Comments |
---|
| 1200 | |
---|
| 1201 | # comment ... |
---|
| 1202 | |
---|
| 1203 | Any line beginning with a C<#> is considered a comment. |
---|
| 1204 | |
---|
[7] | 1205 | =head1 TODO |
---|
| 1206 | |
---|
[52] | 1207 | Improve the commmand line tools |
---|
| 1208 | |
---|
[42] | 1209 | Allow renaming of the submit button; allow renaming and inclusion of a |
---|
| 1210 | reset button |
---|
| 1211 | |
---|
[52] | 1212 | Allow groups to be used in normal field lines something like this: |
---|
| 1213 | |
---|
| 1214 | !group DATE { |
---|
| 1215 | month |
---|
| 1216 | day |
---|
| 1217 | year |
---|
| 1218 | } |
---|
| 1219 | |
---|
| 1220 | dob|Your birthday:DATE |
---|
| 1221 | |
---|
| 1222 | Pieces that wouldn't make sense in a group field: size, row/col, options, |
---|
| 1223 | validate. These should cause C<build> to emit a warning before ignoring them. |
---|
| 1224 | |
---|
| 1225 | Make the generated modules into subclasses of CGI::FormBuilder |
---|
| 1226 | |
---|
[31] | 1227 | Allow for custom wrappers around the C<form_template> |
---|
| 1228 | |
---|
[30] | 1229 | Maybe use HTML::Template instead of Text::Template for the built in template |
---|
[28] | 1230 | (since CGI::FormBuilder users may be more likely to already have HTML::Template) |
---|
| 1231 | |
---|
[16] | 1232 | C<!include> directive to include external formspec files |
---|
[7] | 1233 | |
---|
[19] | 1234 | Better tests! |
---|
[16] | 1235 | |
---|
[23] | 1236 | =head1 BUGS |
---|
| 1237 | |
---|
[50] | 1238 | Having a single C<'> or C<"> in a C<!description> or C<!note> directive causes that |
---|
| 1239 | directive to get skipped. This is an issue with the C<perl_codeblock> shortcut in |
---|
| 1240 | Parse::RecDescent. |
---|
| 1241 | |
---|
[42] | 1242 | Creating two $parsers in the same script causes the second one to get the data |
---|
| 1243 | from the first one. |
---|
[26] | 1244 | |
---|
[42] | 1245 | I'm sure there are more in there, I just haven't tripped over any new ones lately. :-) |
---|
| 1246 | |
---|
| 1247 | Suggestions on how to improve the (currently tiny) test suite would be appreciated. |
---|
| 1248 | |
---|
[1] | 1249 | =head1 SEE ALSO |
---|
| 1250 | |
---|
[50] | 1251 | L<http://textformbuilder.berlios.de> |
---|
[1] | 1252 | |
---|
[50] | 1253 | L<CGI::FormBuilder>, L<http://formbuilder.org> |
---|
| 1254 | |
---|
[23] | 1255 | =head1 THANKS |
---|
| 1256 | |
---|
[26] | 1257 | Thanks to eszpee for pointing out some bugs in the default value parsing, |
---|
| 1258 | as well as some suggestions for i18n/l10n and splitting up long forms into |
---|
[29] | 1259 | sections. |
---|
[23] | 1260 | |
---|
[16] | 1261 | =head1 AUTHOR |
---|
| 1262 | |
---|
[34] | 1263 | Peter Eichman C<< <peichman@cpan.org> >> |
---|
[16] | 1264 | |
---|
| 1265 | =head1 COPYRIGHT AND LICENSE |
---|
| 1266 | |
---|
| 1267 | Copyright E<copy>2004 by Peter Eichman. |
---|
| 1268 | |
---|
| 1269 | This program is free software; you can redistribute it and/or |
---|
| 1270 | modify it under the same terms as Perl itself. |
---|
| 1271 | |
---|
[1] | 1272 | =cut |
---|