Changeset 23 in text-formbuilder for trunk/lib/Text


Ignore:
Timestamp:
11/15/04 16:58:41 (20 years ago)
Author:
peter
Message:

marked Parse::RecDescent as an explicit dependency
added a single-quoted string to the grammar that can be
used in the labels and default values to include characters in [\w\t ]

generated code leaves out overwrriten options

allow option lists to have simple multiword and quoted string values

Location:
trunk/lib/Text
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Text/FormBuilder.pm

    r21 r23  
    1111use Text::FormBuilder::Parser; 
    1212use CGI::FormBuilder; 
     13 
     14# the default options passed to CGI::FormBuilder->new 
     15my %DEFAULT_OPTIONS = ( 
     16    method => 'GET', 
     17    javascript => 0, 
     18    keepextras => 1, 
     19); 
    1320 
    1421sub new { 
     
    133140     
    134141     
    135      
    136  
    137      
    138     # TODO: use lines instead of fields 
    139     # TODO: change template to do groups 
    140      
    141142    # TODO: configurable threshold for this 
    142143    foreach (@{ $self->{form_spec}{fields} }) { 
     
    144145    } 
    145146     
     147 
     148 
     149     
    146150    $self->{form} = CGI::FormBuilder->new( 
    147         method => 'GET', 
    148         javascript => 0, 
    149         keepextras => 1, 
     151        %DEFAULT_OPTIONS, 
    150152        title => $self->{form_spec}{title}, 
    151         #fields => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], 
    152153        template => { 
    153154            type => 'Text', 
     
    171172    $self->{built} = 1; 
    172173     
    173     # TEMP: dump @lines structure 
    174     use YAML; 
    175     warn YAML::Dump($self->{form_spec}->{lines}), "\n"; 
    176      
    177174    return $self; 
    178175} 
     
    215212     
    216213    my $headings    = Data::Dumper->Dump([$self->{form_spec}{headings}],['headings']); 
     214    my $lines       = Data::Dumper->Dump([$self->{form_spec}{lines}],['lines']); 
    217215    my $fields      = Data::Dumper->Dump([ [ map { $$_{name} } @{ $self->{form_spec}{fields} } ] ],['fields']); 
    218216     
    219     my %options = %{ $self->{build_options} }; 
     217    my %options = ( 
     218        %DEFAULT_OPTIONS, 
     219        title => $self->{form_spec}{title}, 
     220        template => { 
     221            type => 'Text', 
     222            engine => { 
     223                TYPE       => 'STRING', 
     224                SOURCE     => $self->{build_options}{form_only} ? $self->_form_template : $self->_template, 
     225                DELIMITERS => [ qw(<% %>) ], 
     226            }, 
     227            data => { 
     228                lines       => $self->{form_spec}{lines}, 
     229                headings    => $self->{form_spec}{headings}, 
     230                author      => $self->{form_spec}{author}, 
     231                description => $self->{form_spec}{description}, 
     232            }, 
     233        },  
     234        %{ $self->{build_options} }, 
     235    ); 
     236     
    220237    my $source = $options{form_only} ? $self->_form_template : $self->_template; 
    221238     
    222239    delete $options{form_only}; 
    223240     
    224     my $form_options = keys %options > 0 ? Data::Dumper->Dump([$self->{build_options}],['*options']) : ''; 
     241    my $form_options = keys %options > 0 ? Data::Dumper->Dump([\%options],['*options']) : ''; 
    225242     
    226243    my $field_setup = join( 
     
    239256    my \$cgi = shift; 
    240257    my \$cgi_form = CGI::FormBuilder->new( 
    241         method => 'GET', 
    242258        params => \$cgi, 
    243         javascript => 0, 
    244         keepextras => 1, 
    245         title => q[$title], 
    246         fields => $fields, 
    247         template => { 
    248             type => 'Text', 
    249             engine => { 
    250                 TYPE       => 'STRING', 
    251                 SOURCE     => q[$source], 
    252                 DELIMITERS => [ qw(<% %>) ], 
    253             }, 
    254             data => { 
    255                 headings => $headings, 
    256                 author   => q[$author], 
    257                 description => q[$description], 
    258             }, 
    259         }, 
    260259        $form_options 
    261260    ); 
     
    2692681; 
    270269END 
    271      
     270 
    272271    my $outfile = (split(/::/, $package))[-1] . '.pm'; 
    273272     
     
    299298<% (grep { $_->{required} } @fields) ? qq[<p id="instructions">(Required fields are marked in <strong>bold</strong>.)</p>] : '' %> 
    300299<% $start %> 
     300<% 
     301    # drop in the hidden fields here 
     302    $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); 
     303%> 
     304 
    301305<table> 
    302306 
    303 <% for my $line (@lines) { 
     307<% TABLE_LINE: for my $line (@lines) { 
    304308 
    305309    if ($$line[0] eq 'head') { 
     
    308312        #TODO: we only need the field names, not the full field spec in the lines strucutre 
    309313        local $_ = $field{$$line[1]{name}}; 
     314        # skip hidden fields in the table 
     315        next TABLE_LINE if $$_{type} eq 'hidden'; 
     316         
    310317        $OUT .= $$_{invalid} ? qq[  <tr class="invalid">] : qq[  <tr>]; 
    311318        $OUT .= '<th class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}:</strong>] : "$$_{label}:") . '</th>'; 
     
    402409    $parser->write_module('My::Form'); 
    403410 
     411=head1 REQUIRES 
     412 
     413L<Parse::RecDescent>, L<CGI::FormBuilder>, L<Text::Template> 
     414 
    404415=head1 DESCRIPTION 
    405416 
     
    596607    text        # the default 
    597608    textarea 
     609    password 
     610    file 
     611    checkbox 
     612    radio 
    598613    select 
    599     radio 
    600     checkbox 
     614    hidden 
    601615    static 
    602616 
     
    681695=head1 TODO 
    682696 
     697DWIM for single valued checkboxes (e.g. C<moreinfo|Send me more info:checkbox>) 
     698 
     699Use the custom message file format for messages in the built in template 
     700 
     701C<!section> directive to split up the table into multiple tables, each 
     702with their own id and (optional) heading 
     703 
     704Optional validated fields; marked like C<//EMAIL?> 
     705 
     706Better examples in the docs (maybe a standalone or two as well) 
     707 
     708Document the defaults that are passed to CGI::FormBuilder 
     709 
    683710C<!include> directive to include external formspec files 
    684711 
    685 Field groups all on one line in the generated form 
    686  
    687712Better tests! 
    688713 
     714=head1 BUGS 
     715 
    689716=head1 SEE ALSO 
    690717 
    691718L<CGI::FormBuilder> 
     719 
     720=head1 THANKS 
     721 
     722Thanks to eszpee for pointing out some bugs in the default value parsing. 
    692723 
    693724=head1 AUTHOR 
  • trunk/lib/Text/FormBuilder/grammar

    r22 r23  
    164164list_var: /@[A-Z_]+/ { $list_var = $item[1] } 
    165165 
    166 option: value display_text(?) 
    167     { push @options, { $item{value} => $item{'display_text(?)'}[0] } } 
     166option: (simple_multiword | quoted_string) display_text(?) 
     167    { push @options, { $item[1] => $item{'display_text(?)'}[0] } } 
    168168 
    169169value: identifier 
Note: See TracChangeset for help on using the changeset viewer.