Changeset 39 in text-formbuilder for trunk/lib/Text
- Timestamp:
- 12/07/04 16:18:02 (20 years ago)
- Location:
- trunk/lib/Text
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Text/FormBuilder.pm
r38 r39 41 41 42 42 my $DEFAULT_CHARSET = 'iso-8859-1'; 43 44 # options to clean up the code with Perl::Tidy 45 my $TIDY_OPTIONS = '-nolq -ci=4 -ce'; 43 46 44 47 sub new { … … 177 180 } 178 181 179 # substitute in custom pattern definitions for field validation 180 if (my %patterns = %{ $self->{form_spec}{patterns} || {} }) { 181 foreach (@{ $self->{form_spec}{fields} }) { 182 if ($$_{validate} and exists $patterns{$$_{validate}}) { 182 # substitute in custom validation subs and pattern definitions for field validation 183 my %patterns = %{ $self->{form_spec}{patterns} || {} }; 184 my %subs = %{ $self->{form_spec}{subs} || {} }; 185 186 foreach (@{ $self->{form_spec}{fields} }) { 187 if ($$_{validate}) { 188 if (exists $patterns{$$_{validate}}) { 183 189 $$_{validate} = $patterns{$$_{validate}}; 190 # TODO: need the Data::Dumper code to work for this 191 # for now, we just warn that it doesn't work 192 } elsif (exists $subs{$$_{validate}}) { 193 warn "[Text::FormBuilder] validate coderefs don't work yet"; 194 delete $$_{validate}; 195 ## $$_{validate} = eval "sub $subs{$$_{validate}}"; 184 196 } 185 197 } … … 229 241 $$_{required} or delete $$_{required} foreach @{ $self->{form_spec}{fields} }; 230 242 243 foreach (@{ $self->{form_spec}{sections} }) { 244 for my $line (grep { $$_[0] eq 'field' } @{ $$_{lines} }) { 245 $_ eq 'name' or delete $$line[1]{$_} foreach keys %{ $$line[1] }; 246 } 247 } 248 231 249 $self->{form} = CGI::FormBuilder->new( 232 250 %DEFAULT_OPTIONS, … … 275 293 } 276 294 277 sub write_module { 278 my ($self, $package, $use_tidy) = @_; 279 280 croak '[Text::FormBuilder::write_module] Expecting a package name' unless $package; 281 295 sub _form_code { 296 my $self = shift; 282 297 # automatically call build if needed to 283 298 # allow the new->parse->write shortcut … … 322 337 delete $options{$_} foreach qw(form_only css extra_css); 323 338 324 my $form_options = keys %options > 0 ? Data::Dumper->Dump([\%options],['*options']) : ''; 339 my %module_subs; 340 my $d = Data::Dumper->new([ \%options ], [ '*options' ]); 341 342 #TODO: need a workaround/better solution since Data::Dumper doesn't like dumping coderefs 343 ## foreach (@{ $self->{form_spec}{fields} }) { 344 ## if (ref $$_{validate} eq 'CODE') { 345 ## $d->Seen({ "*_validate_$$_{name}" => $$_{validate} }); 346 ## $module_subs{$$_{name}} = "sub _validate_$$_{name} $$_{validate}"; 347 ## } 348 ## } 349 ## 350 ## my $sub_code = join("\n", each %module_subs); 351 my $form_options = keys %options > 0 ? $d->Dump : ''; 325 352 326 353 my $field_setup = join( 327 354 "\n", 328 map { '$ cgi_form->field' . Data::Dumper->Dump([$_],['*field']) . ';' } @{ $self->{form_spec}{fields} }355 map { '$form->field' . Data::Dumper->Dump([$_],['*field']) . ';' } @{ $self->{form_spec}{fields} } 329 356 ); 357 358 return <<END; 359 my \$form = CGI::FormBuilder->new( 360 params => \$q, 361 $form_options 362 ); 363 364 $field_setup 365 END 366 } 367 368 sub write_module { 369 my ($self, $package, $use_tidy) = @_; 370 371 croak '[Text::FormBuilder::write_module] Expecting a package name' unless $package; 372 373 my $form_code = $self->_form_code; 330 374 331 375 my $module = <<END; … … 337 381 338 382 sub get_form { 339 my \$cgi = shift; 340 my \$cgi_form = CGI::FormBuilder->new( 341 params => \$cgi, 342 $form_options 343 ); 344 345 $field_setup 346 347 return \$cgi_form; 383 my \$q = shift; 384 385 $form_code 386 387 return \$form; 348 388 } 349 389 … … 354 394 my $outfile = (split(/::/, $package))[-1] . '.pm'; 355 395 396 _write_output_file($module, $outfile, $use_tidy); 397 return $self; 398 } 399 400 sub write_script { 401 my ($self, $script_name, $use_tidy) = @_; 402 403 croak '[Text::FormBuilder::write_script] Expecting a script name' unless $script_name; 404 405 my $form_code = $self->_form_code; 406 407 my $script = <<END; 408 #!/usr/bin/perl 409 use strict; 410 use warnings; 411 412 use CGI; 413 use CGI::FormBuilder; 414 415 my \$q = CGI->new; 416 417 $form_code 418 419 unless (\$form->submitted && \$form->validate) { 420 print \$form->render; 421 } else { 422 # do something with the entered data 423 } 424 END 425 426 _write_output_file($script, $script_name, $use_tidy); 427 return $self; 428 } 429 430 sub _write_output_file { 431 my ($source_code, $outfile, $use_tidy) = @_; 356 432 if ($use_tidy) { 357 433 # clean up the generated code, if asked 358 434 eval 'use Perl::Tidy'; 359 435 die "Can't tidy the code: $@" if $@; 360 Perl::Tidy::perltidy(source => \$ module, destination => $outfile, argv => '-nolq -ci=4');436 Perl::Tidy::perltidy(source => \$source_code, destination => $outfile, argv => $TIDY_OPTIONS); 361 437 } else { 362 438 # otherwise, just print as is 363 open FORM, "> $outfile"; 364 print FORM $module; 365 close FORM; 366 } 367 } 439 open OUT, "> $outfile" or die $!; 440 print OUT $source_code; 441 close OUT; 442 } 443 } 444 368 445 369 446 sub form { … … 696 773 $parser->write_module('My::Form', 1); 697 774 775 =head2 write_script 776 777 $parser->write_script($filename, $use_tidy); 778 779 If you don't need the reuseability of a separate module, you can have 780 Text::FormBuilder write the form object to a script for you, along with 781 the simplest framework for using it, to which you can add your actual 782 form processing code. 783 784 The generated script looks like this: 785 786 #!/usr/bin/perl 787 use strict; 788 use warnings; 789 790 use CGI; 791 use CGI::FormBuilder; 792 793 my $q = CGI->new; 794 795 my $form = CGI::FormBuilder->new( 796 params => $q, 797 # ... lots of other stuff to set up the form ... 798 ); 799 800 $form->field( name => 'month' ); 801 $form->field( name => 'day' ); 802 803 unless ( $form->submitted && $form->validate ) { 804 print $form->render; 805 } else { 806 # do something with the entered data ... 807 # this is where your form processing code should go 808 } 809 810 Like C<write_module>, you can optionally pass a true value as the second 811 argument to have Perl::Tidy make the generated code look nicer. 812 698 813 =head2 dump 699 814 -
trunk/lib/Text/FormBuilder/grammar
r29 r39 11 11 %lists, 12 12 %patterns, 13 %subs, # validation subs 13 14 @group, # current group 14 15 %groups, # stored groups of fields … … 24 25 } 25 26 26 form_spec: (list_def | description_def | group_def | line)(s)27 form_spec: (list_def | description_def | validate_def | group_def | line)(s) 27 28 { 28 29 # grab the last section, if there is any … … 45 46 lists => \%lists, 46 47 patterns => \%patterns, 48 subs => \%subs, 47 49 groups => \%groups, 48 50 sections => \@sections, … … 71 73 $description =~ s/^{\s*|\s*}$//g; 72 74 } 75 76 validate_def: '!validate' var_name <perl_codeblock> 77 { $subs{$item{var_name}} = $item[3] } 73 78 74 79 group_def: '!group' { $context = 'group' } var_name '{' field_line(s) '}' { $context = 'line' }
Note: See TracChangeset
for help on using the changeset viewer.