Changeset 19 in text-formbuilder for trunk/lib/Text
- Timestamp:
- 11/09/04 12:47:12 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Text/FormBuilder.pm
r16 r19 4 4 use warnings; 5 5 6 our $VERSION = '0.04'; 6 use vars qw($VERSION); 7 8 $VERSION = '0.05'; 7 9 8 10 use Carp; … … 20 22 21 23 sub parse { 24 my ($self, $source) = @_; 25 if (ref $source && ref $source eq 'SCALAR') { 26 $self->parse_text($$source); 27 } else { 28 $self->parse_file($source); 29 } 30 } 31 32 sub parse_file { 22 33 my ($self, $filename) = @_; 23 34 … … 59 70 60 71 # substitute in custom pattern definitions for field validation 61 if (my %patterns = %{ $self->{form_spec}{patterns} }) {72 if (my %patterns = %{ $self->{form_spec}{patterns} || {} }) { 62 73 foreach (@{ $self->{form_spec}{fields} }) { 63 74 if ($$_{validate} and exists $patterns{$$_{validate}}) { … … 78 89 79 90 # substitute in list names 80 if (my %lists = %{ $self->{form_spec}{lists} }) {91 if (my %lists = %{ $self->{form_spec}{lists} || {} }) { 81 92 foreach (@{ $self->{form_spec}{fields} }) { 82 93 next unless $$_{list}; … … 322 333 # the fields from the input form spec 323 334 my $form = $parser->form; 335 336 # write a My::Form module to Form.pm 337 $parser->write_module('My::Form'); 324 338 325 339 =head1 DESCRIPTION … … 329 343 =head2 parse 330 344 331 $parser->parse($src_file); 345 # parse a file 346 $parser->parse($filename); 347 348 # or pass a scalar ref for parse a literal string 349 $parser->parse(\$string); 350 351 Parse the file or string. Returns the parser object. 352 353 =head2 parse_file 354 355 $parser->parse_file($src_file); 332 356 333 357 # or as a class method … … 338 362 $parser->parse_text($src); 339 363 340 Parse the given C<$src> text. Returns the parse object.364 Parse the given C<$src> text. Returns the parser object. 341 365 342 366 =head2 build … … 348 372 =over 349 373 350 =item form_only374 =item C<form_only> 351 375 352 376 Only uses the form portion of the template, and omits the surrounding html, 353 title, author, and the standard footer. 377 title, author, and the standard footer. This does, however, include the 378 description as specified with the C<!description> directive. 354 379 355 380 =back … … 398 423 First, you parse the formspec and write the module, which you can do as a one-liner: 399 424 400 $ perl -MText::FormBuilder -e"Text::FormBuilder->parse('formspec.txt')->write_module('My Form')"425 $ perl -MText::FormBuilder -e"Text::FormBuilder->parse('formspec.txt')->write_module('My::Form')" 401 426 402 427 And then, in your CGI script, use the new module: … … 406 431 407 432 use CGI; 408 use My Form;433 use My::Form; 409 434 410 435 my $q = CGI->new; 411 my $form = My Form::get_form($q);436 my $form = My::Form::get_form($q); 412 437 413 438 # do the standard CGI::FormBuilder stuff … … 422 447 will run L<Perl::Tidy> on the generated code before writing the module file. 423 448 449 # write tidier code 450 $parser->write_module('My::Form', 1); 451 424 452 =head2 dump 425 453 … … 429 457 =head1 LANGUAGE 430 458 431 field_name[size]|descriptive label[hint]:type=default{option1[display string], option2[display string],...}//validate459 field_name[size]|descriptive label[hint]:type=default{option1[display string],...}//validate 432 460 433 461 !title ... … … 469 497 =item C<!description> 470 498 499 A brief description of the form. Suitable for special instructions on how to 500 fill out the form. 501 471 502 =item C<!head> 472 503 … … 479 510 =head2 Fields 480 511 481 Form fields are each described on a single line. 512 Form fields are each described on a single line. The simplest field is just a 513 name: 514 515 color 516 517 This yields a form with one text input field of the default size named `color'. 518 The label for this field as generated by CGI::FormBuilder would be ``Color''. 519 To add a longer or more descriptive label, use: 520 521 color|Favorite color 522 523 Field names cannot contain whitespace, but the descriptive label can. 524 525 To use a different input type: 526 527 color|Favorite color:select{red,blue,green} 528 529 Recognized input types are the same as those used by CGI::FormBuilder: 530 531 text # the default 532 textarea 533 select 534 radio 535 checkbox 536 static 537 538 This example also shows how you can list multiple values for the input types 539 that take multiple values (C<select>, C<radio>, and C<checkbox>). Values are 540 in a comma-separated list inside curly braces. Whitespace between values is 541 irrelevant, although there cannot be any whitespace within a value. 542 543 To add more descriptive display text to a vlaue in a list, add a square-bracketed 544 ``subscript,'' as in: 545 546 ...:select{red[Scarlet],blue[Azure],green[Olive Drab]} 547 548 As you can see, spaces I<are> allowed within the display text for a value. 482 549 483 550 If you have a list of options that is too long to fit comfortably on one line, 484 consider using the C<!list> directive. 551 consider using the C<!list> directive: 552 553 !list MONTHS { 554 1[January], 555 2[February], 556 3[March], 557 # and so on... 558 } 559 560 month:select@MONTHS 561 562 There is another form of the C<!list> directive: the dynamic list: 563 564 !list RANDOM &{ map { rand } (0..5) } 565 566 The code inside the C<&{ ... }> is C<eval>ed by C<build>, and the results 567 are stuffed into the list. The C<eval>ed code can either return a simple 568 list, as the example does, or the fancier C<( { value1 => 'Description 1'}, 569 { value2 => 'Description 2}, ...)> form. 570 571 B<NOTE:> This feature of the language may go away unless I find a compelling 572 reason for it in the next few versions. What I really wanted was lists that 573 were filled in at run-time (e.g. from a database), and that can be done easily 574 enough with the CGI::FormBuilder object directly. 575 576 You can also supply a default value to the field. To get a default value of 577 C<green> for the color field: 578 579 color|Favorite color:select=green{red,blue,green} 580 581 To validate a field, include a validation type at the end of the field line: 582 583 email|Email address//EMAIL 584 585 Valid validation types include any of the builtin defaults from CGI::FormBuilder, 586 or the name of a pattern that you define with the C<!pattern> directive elsewhere 587 in your form spec: 588 589 !pattern DAY /^([1-3][0-9])|[1-9]$/ 590 591 last_day//DAY 592 593 If you just want a required value, use the builtin validation type C<VALUE>: 594 595 title//VALUE 485 596 486 597 =head2 Comments … … 496 607 Field groups all on one line in the generated form 497 608 498 Tests!609 Better tests! 499 610 500 611 =head1 SEE ALSO
Note: See TracChangeset
for help on using the changeset viewer.