Changeset 30 in text-formbuilder


Ignore:
Timestamp:
11/17/04 11:16:57 (20 years ago)
Author:
peter
Message:

added CSS customization
special-case for single-option checkboxes

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Changes

    r24 r30  
    1010      string values 
    1111    * allow for validated but not required fields 
     12    * added a !section directive that splits up a form into 
     13      sub-tables with their own ids and captions 
    1214     
    13150.05 -  9 Nov 2004 
  • trunk/lib/Text/FormBuilder.pm

    r29 r30  
    1818    keepextras => 1, 
    1919); 
     20 
     21# the built in CSS for the template 
     22my $DEFAULT_CSS = <<END; 
     23table { padding: 1em; } 
     24#author, #footer { font-style: italic; } 
     25caption h2 { padding: .125em .5em; background: #ccc; text-align: left; } 
     26th { text-align: left; } 
     27th h3 { padding: .125em .5em; background: #eee; } 
     28th.label { font-weight: normal; text-align: right; vertical-align: top; } 
     29td ul { list-style: none; padding-left: 0; margin-left: 0; } 
     30.sublabel { color: #999; } 
     31.invalid { background: red; } 
     32END 
    2033 
    2134sub new { 
     
    7487    # form_only: use only the form part of the template 
    7588    my $form_only = $options{form_only}; 
    76     delete $options{form_only}; 
     89    my $css; 
     90    $css = $options{css} || $DEFAULT_CSS; 
     91    $css .= $options{extra_css} if $options{extra_css}; 
     92     
     93    # remove our custom options 
     94    delete $options{$_} foreach qw(form_only css extra_css); 
    7795     
    7896    # substitute in custom pattern definitions for field validation 
     
    139157    } continue { 
    140158        delete $$_{list}; 
    141     }     
     159    } 
     160     
     161    # special case single-value checkboxes 
     162    foreach (grep { $$_{type} && $$_{type} eq 'checkbox' } @{ $self->{form_spec}{fields} }) { 
     163        unless ($$_{options}) { 
     164            $$_{options} = [ { $$_{name} => $$_{label} || ucfirst join(' ',split(/_/,$$_{name})) } ]; 
     165        } 
     166    } 
    142167     
    143168    # TODO: configurable threshold for this 
     
    167192            engine => { 
    168193                TYPE       => 'STRING', 
    169                 SOURCE     => $form_only ? $self->_form_template : $self->_template, 
     194                SOURCE     => $form_only ? $self->_form_template : $self->_template($css), 
    170195                DELIMITERS => [ qw(<% %>) ], 
    171196            }, 
     
    218243    $Data::Dumper::Terse = 1; 
    219244     
     245    my $css; 
     246    $css = $self->{build_options}{css} || $DEFAULT_CSS; 
     247    $css .= $self->{build_options}{extra_css} if $self->{build_options}{extra_css}; 
     248     
    220249    my %options = ( 
    221250        %DEFAULT_OPTIONS, 
     
    228257            engine => { 
    229258                TYPE       => 'STRING', 
    230                 SOURCE     => $self->{build_options}{form_only} ? $self->_form_template : $self->_template, 
     259                SOURCE     => $self->{build_options}{form_only} ? $self->_form_template : $self->_template($css), 
    231260                DELIMITERS => [ qw(<% %>) ], 
    232261            }, 
     
    242271    my $source = $options{form_only} ? $self->_form_template : $self->_template; 
    243272     
    244     delete $options{form_only}; 
     273    # remove our custom options 
     274    delete $options{$_} foreach qw(form_only css extra_css); 
    245275     
    246276    my $form_options = keys %options > 0 ? Data::Dumper->Dump([\%options],['*options']) : ''; 
     
    310340<% 
    311341    SECTION: while (my $section = shift @sections) { 
    312         $OUT .= qq[<table id="$$section{id}">\n]; 
    313         $OUT .= qq[  <caption><h2>$$section{head}</h2></caption>] if $$section{head}; 
     342        $OUT .= qq[<table id="] . ($$section{id} || '_default') . qq[">\n]; 
     343        $OUT .= qq[  <caption><h2 class="sectionhead">$$section{head}</h2></caption>] if $$section{head}; 
    314344        TABLE_LINE: for my $line (@{ $$section{lines} }) { 
    315345            if ($$line[0] eq 'head') { 
    316                 $OUT .= qq[  <tr><th class="sectionhead" colspan="2"><h3>$$line[1]</h3></th></tr>\n] 
     346                $OUT .= qq[  <tr><th class="subhead" colspan="2"><h3>$$line[1]</h3></th></tr>\n] 
    317347            } elsif ($$line[0] eq 'field') { 
    318348                #TODO: we only need the field names, not the full field spec in the lines strucutre 
     
    322352                 
    323353                $OUT .= $$_{invalid} ? qq[  <tr class="invalid">] : qq[  <tr>]; 
    324                 $OUT .= '<th class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}:</strong>] : "$$_{label}:") . '</th>'; 
     354                 
     355                # special case single value checkboxes 
     356                if ($$_{type} eq 'checkbox' && @{ $$_{options} } == 1) { 
     357                    $OUT .= qq[<th></th>]; 
     358                } else { 
     359                    $OUT .= '<th class="label">' . ($$_{required} ? qq[<strong class="required">$$_{label}:</strong>] : "$$_{label}:") . '</th>'; 
     360                } 
    325361                if ($$_{invalid}) { 
    326                     $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td></tr>\n]; 
     362                    $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td>]; 
    327363                } else { 
    328                     $OUT .= qq[<td>$$_{field} $$_{comment}</td></tr>\n]; 
     364                    $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; 
    329365                } 
     366                $OUT .= qq[</tr>\n]; 
     367                 
    330368            } elsif ($$line[0] eq 'group') { 
    331369                my @field_names = map { $$_{name} } @{ $$line[1]{group} }; 
     
    356394sub _template { 
    357395    my $self = shift; 
     396    my $css = shift || $DEFAULT_CSS; 
    358397q[<html> 
    359398<head> 
    360399  <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> 
    361   <style type="text/css"> 
    362     table { margin: .5em 1em; } 
    363     #author, #footer { font-style: italic; } 
    364     caption h2 { padding: .125em .5em; background: #ddd; text-align: left; } 
    365     th { text-align: left; } 
    366     th h3 { padding: .125em .5em; background: #eee; } 
    367     th.label { font-weight: normal; text-align: right; vertical-align: top; } 
    368     td ul { list-style: none; padding-left: 0; margin-left: 0; } 
    369     .sublabel { color: #999; } 
    370     .invalid { background: red; } 
     400  <style type="text/css">] . 
     401$css . q[ 
    371402  </style> 
    372403</head> 
     
    461492description as specified with the C<!description> directive. 
    462493 
     494=item C<css>, C<extra_css> 
     495 
     496These options allow you to tell Text::FormBuilder to use different 
     497CSS styles for the built in template. A value given a C<css> will 
     498replace the existing CSS, and a value given as C<extra_css> will be 
     499appended to the CSS. If both options are given, then the CSS that is 
     500used will be C<css> concatenated with C<extra_css>. 
     501 
    463502=back 
    464503 
     
    703742    moreinfo|I want to recieve more information:checkbox 
    704743 
    705 The one drawback to this is that the label to the checkbox will still appear 
    706 to the left of the field. I am leaving it this way for now, but if enough 
    707 people would like this to change, I may make single-option checkboxes a special 
    708 case and put the label on the right. 
     744In this case, the label ``I want to recieve more information'' will be 
     745printed to the right of the checkbox. 
    709746 
    710747You can also supply a default value to the field. To get a default value of 
     
    749786Use the custom message file format for messages in the built in template 
    750787 
    751 Custom CSS, both in addition to, and replacing the built in. 
    752  
    753 Use HTML::Template instead of Text::Template for the built in template 
     788Maybe use HTML::Template instead of Text::Template for the built in template 
    754789(since CGI::FormBuilder users may be more likely to already have HTML::Template) 
    755790 
     
    764799=head1 BUGS 
    765800 
    766 For now, checkboxes with a single value still display their labels on 
    767 the left. 
     801I'm sure they're in there, I just haven't tripped over any new ones lately. :-) 
    768802 
    769803=head1 SEE ALSO 
Note: See TracChangeset for help on using the changeset viewer.