Changeset 33 in text-formbuilder


Ignore:
Timestamp:
11/18/04 14:26:18 (20 years ago)
Author:
peter
Message:

split _template into _pre_template and _post_template
BUGFIX: moved !pattern expansion to later
beginning work on custom messages

File:
1 edited

Legend:

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

    r32 r33  
    66use vars qw($VERSION); 
    77 
    8 $VERSION = '0.06_02'; 
     8$VERSION = '0.06_03'; 
    99 
    1010use Carp; 
     
    3232END 
    3333 
     34my %DEFAULT_MESSAGES = ( 
     35    text_formbuilder_created => 'Created by %s', 
     36    text_formbuilder_madewith => 'Made with %s', 
     37    text_formbuilder_required => 'Required fields are marked in <strong>bold</strong>.', 
     38); 
     39 
    3440sub new { 
    3541    my $invocant = shift; 
     
    8187} 
    8288 
     89# this is where a lot of the magic happens 
    8390sub build { 
    8491    my ($self, %options) = @_; 
    85  
    86     # save the build options so they can be used from write_module 
    87     $self->{build_options} = { %options }; 
    8892     
    8993    # our custom %options: 
    9094    # form_only: use only the form part of the template 
    9195    my $form_only = $options{form_only}; 
     96     
     97    # css, extra_css: allow for custom inline stylesheets 
     98    #   neat trick: extra_css => '@import(my_external_stylesheet.css);' 
     99    #   will let you use an external stylesheet 
    92100    my $css; 
    93101    $css = $options{css} || $DEFAULT_CSS; 
    94102    $css .= $options{extra_css} if $options{extra_css}; 
    95103     
    96     # remove our custom options 
    97     delete $options{$_} foreach qw(form_only css extra_css); 
    98      
    99     # substitute in custom pattern definitions for field validation 
    100     if (my %patterns = %{ $self->{form_spec}{patterns} || {} }) { 
    101         foreach (@{ $self->{form_spec}{fields} }) { 
    102             if ($$_{validate} and exists $patterns{$$_{validate}}) { 
    103                 $$_{validate} = $patterns{$$_{validate}}; 
     104    # messages 
     105    if ($options{messages}) { 
     106        # if its a hashref, we'll just pass it on to CGI::FormBuilder 
     107         
     108        if (my $ref = ref $options{messages}) { 
     109            # hashref pass on to CGI::FormBuilder 
     110            croak "[Text::FormBuilder] Argument to 'messages' option must be a filename or hashref" unless $ref eq 'HASH'; 
     111            while (my ($key,$value) = each %DEFAULT_MESSAGES) { 
     112                $options{messages}{$key} ||= $DEFAULT_MESSAGES{$key}; 
     113            } 
     114        } else { 
     115            # filename, just *warn* on missing, and use defaults 
     116            if (-f $options{messages} && -r _ && open(MESSAGES, "< $options{messages}")) { 
     117                $options{messages} = {}; 
     118                while(<MESSAGES>) { 
     119                    next if /^\s*#/ || /^\s*$/; 
     120                    chomp; 
     121                    my($key,$value) = split ' ', $_, 2; 
     122                    ($options{messages}{$key} = $value) =~ s/\s+$//; 
     123                } 
     124                close MESSAGES; 
     125            } else { 
     126                carp "Could not read messages file $options{messages}: $!"; 
    104127            } 
    105128        } 
    106129    } 
     130     
     131    # save the build options so they can be used from write_module 
     132    $self->{build_options} = { %options }; 
     133     
     134    # remove our custom options before we hand off to CGI::FormBuilder 
     135    delete $options{$_} foreach qw(form_only css extra_css); 
    107136     
    108137    # expand groups 
     
    124153    } 
    125154     
     155    # the actual fields that are given to CGI::FormBuilder 
    126156    $self->{form_spec}{fields} = []; 
    127157     
    128158    for my $section (@{ $self->{form_spec}{sections} || [] }) { 
    129         #for my $line (@{ $self->{form_spec}{lines} || [] }) { 
    130159        for my $line (@{ $$section{lines} }) { 
    131160            if ($$line[0] eq 'group') { 
     
    133162            } elsif ($$line[0] eq 'field') { 
    134163                push @{ $self->{form_spec}{fields} }, $$line[1]; 
     164            } 
     165        } 
     166    } 
     167     
     168    # substitute in custom pattern definitions for field validation 
     169    if (my %patterns = %{ $self->{form_spec}{patterns} || {} }) { 
     170        foreach (@{ $self->{form_spec}{fields} }) { 
     171            if ($$_{validate} and exists $patterns{$$_{validate}}) { 
     172                $$_{validate} = $patterns{$$_{validate}}; 
    135173            } 
    136174        } 
     
    175213    } 
    176214     
    177     # because this messes up things at the CGI::FormBuilder::field level 
    178     # it seems to be marking required based on the existance of a 'required' 
    179     # param, not whether it is true or defined 
     215    # remove false $$_{required} params because this messes up things at 
     216    # the CGI::FormBuilder::field level; it seems to be marking required 
     217    # based on the existance of a 'required' param, not whether it is 
     218    # true or defined 
    180219    $$_{required} or delete $$_{required} foreach @{ $self->{form_spec}{fields} }; 
    181220 
    182     # need to explicity set the fields so that simple text fields get picked up 
    183221    $self->{form} = CGI::FormBuilder->new( 
    184222        %DEFAULT_OPTIONS, 
     223        # need to explicity set the fields so that simple text fields get picked up 
    185224        fields   => [ map { $$_{name} } @{ $self->{form_spec}{fields} } ], 
    186225        required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], 
     
    239278    die "Can't write module; need Data::Dumper. $@" if $@; 
    240279     
    241     # don't dump $VARn names 
    242     $Data::Dumper::Terse = 1; 
     280    $Data::Dumper::Terse = 1;           # don't dump $VARn names 
     281    $Data::Dumper::Quotekeys = 0;       # don't quote simple string keys 
    243282     
    244283    my $css; 
     
    268307    ); 
    269308     
    270     my $source = $options{form_only} ? $self->_form_template : $self->_template; 
    271      
    272309    # remove our custom options 
    273310    delete $options{$_} foreach qw(form_only css extra_css); 
     
    309346        eval 'use Perl::Tidy'; 
    310347        die "Can't tidy the code: $@" if $@; 
    311         Perl::Tidy::perltidy(source => \$module, destination => $outfile); 
     348        Perl::Tidy::perltidy(source => \$module, destination => $outfile, argv => '-nolq -ci=4'); 
    312349    } else { 
    313350        # otherwise, just print as is 
     
    329366 
    330367sub _form_template { 
    331 q[<% $description ? qq[<p id="description">$description</p>] : '' %> 
     368    my $self = shift; 
     369    #warn keys %{ $self->{build_options}{messages} }; 
     370    my $msg_required = $self->{build_options}{messages}{text_formbuilder_required}; 
     371    return q[<% $description ? qq[<p id="description">$description</p>] : '' %> 
    332372<% (grep { $_->{required} } @fields) ? qq[<p id="instructions">(Required fields are marked in <strong>bold</strong>.)</p>] : '' %> 
    333373<% $start %> 
     
    358398                    $OUT .= '<th class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}:</strong>] : "$$_{label}:") . '</th>'; 
    359399                } 
     400                 
     401                # mark invalid fields 
    360402                if ($$_{invalid}) { 
    361403                    $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td>]; 
     
    363405                    $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; 
    364406                } 
     407                 
    365408                $OUT .= qq[</tr>\n]; 
    366409                 
     
    391434} 
    392435 
    393 sub _template { 
     436sub _pre_template { 
    394437    my $self = shift; 
    395438    my $css = shift || $DEFAULT_CSS; 
     439    return  
    396440q[<html> 
    397441<head> 
    398442  <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> 
    399   <style type="text/css">] . 
    400 $css . q[ 
    401   </style> 
     443  <style type="text/css"> 
     444] . 
     445$css . 
     446q[  </style> 
     447  <% $jshead %> 
    402448</head> 
    403449<body> 
     
    405451<h1><% $title %></h1> 
    406452<% $author ? qq[<p id="author">Created by $author</p>] : '' %> 
    407 ] . $self->_form_template . q[ 
    408 <hr /> 
     453]; 
     454} 
     455 
     456sub _post_template { 
     457q[<hr /> 
    409458<div id="footer"> 
    410459  <p id="creator">Made with <a href="http://formbuilder.org/">CGI::FormBuilder</a> version <% CGI::FormBuilder->VERSION %>.</p> 
     
    415464} 
    416465 
     466sub _template { 
     467    my $self = shift; 
     468    my $css = shift || $DEFAULT_CSS; 
     469    return $self->_pre_template($css) . $self->_form_template . $self->_post_template; 
     470} 
     471 
    417472sub dump {  
    418473    eval "use YAML;"; 
     
    578633Uses L<YAML> to print out a human-readable representation of the parsed 
    579634form spec. 
     635 
     636=head1 DEFAULTS 
     637 
     638These are the default settings that are passed to C<< CGI::FormBuilder->new >>: 
     639 
     640    method => 'GET' 
     641    javascript => 0 
     642    keepextras => 1 
     643 
     644Any of these can be overriden by the C<build> method: 
     645 
     646    # use POST instead 
     647    $parser->build(method => 'POST')->write; 
    580648 
    581649=head1 LANGUAGE 
     
    785853Allow for custom wrappers around the C<form_template> 
    786854 
    787 Use the custom message file format for messages in the built in template 
     855Use the custom message file format for messages in the built in template (i18n/l10n) 
    788856 
    789857Maybe use HTML::Template instead of Text::Template for the built in template 
Note: See TracChangeset for help on using the changeset viewer.