File Coverage

lib/CGI/FormBuilder/Field/static.pm
Criterion Covered Total %
statement 41 46 89.1
branch 6 10 60.0
condition 0 6 0.0
subroutine 8 8 100.0
pod 1 2 50.0
total 56 72 77.7


line stmt bran cond sub pod time code
1              
2             ###########################################################################
3             # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4             # Please visit http://formbuilder.org for tutorials, support, and examples.
5             ###########################################################################
6              
7             # static fields are a special FormBuilder type that turns any
8             # normal field into a hidden field with the value printed.
9             # As such, the code has to basically handle all field types.
10              
11             package CGI::FormBuilder::Field::static;
12              
13 2     2   13 use strict;
  2         5  
  2         77  
14 2     2   12 use warnings;
  2         2  
  2         72  
15 2     2   10 no warnings 'uninitialized';
  2         4  
  2         75  
16              
17 2     2   22 use CGI::FormBuilder::Util;
  2         3  
  2         355  
18 2     2   12 use CGI::FormBuilder::Field;
  2         3  
  2         52  
19 2     2   10 use base 'CGI::FormBuilder::Field';
  2         5  
  2         1091  
20              
21              
22             our $VERSION = '3.09';
23              
24             sub script {
25 26     26 0 71 return ''; # static fields get no messages
26             }
27              
28             *render = \&tag;
29             sub tag {
30 26     26 1 85 local $^W = 0; # -w sucks
31 26         42 my $self = shift;
32 26         102 my $attr = $self->attr;
33              
34 26         160 my $jspre = $self->{_form}->jsprefix;
35              
36 26         43 my @tag;
37 26         81 my @value = $self->tag_value; # sticky is different in
38 26         101 my @opt = $self->options;
39 26         109 debug 2, "my(@opt) = \$field->options";
40              
41             # Add in our "Other:" option if applicable
42 26 50       95 push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
43             if $self->other;
44              
45 26         197 debug 2, "$self->{name}: generating $attr->{type} input type";
46              
47             # static fields are actually hidden
48 26         545 $attr->{type} = 'hidden';
49              
50             # We iterate over each value - this is the only reliable
51             # way to handle multiple form values of the same name
52             # (i.e., multiple or fields)
53 26 100       80 @value = (undef) unless @value; # this creates a single-element array
54              
55 26         55 for my $value (@value) {
56 26         43 my $tmp = '';
57            
58             # setup the value
59 26         75 $attr->{value} = $value; # override
60 26 100       77 delete $attr->{value} unless defined $value;
61              
62             # render the tag
63 26         88 $tmp .= htmltag('input', $attr);
64              
65             #
66             # If we have options, lookup the label instead of the true value
67             # to print next to the field. This will happen when radio/select
68             # lists are converted to 'static'.
69             #
70 26         68 for (@opt) {
71 0         0 my($o,$n) = optval($_);
72 0 0       0 if ($o eq $value) {
73 0   0     0 $n ||= $attr->{labels}{$o} || ($self->nameopts ? toname($o) : $o);
      0        
74 0         0 $value = $n;
75 0         0 last;
76             }
77             }
78              
79             # print the value out too when in a static context
80 26 50       153 $tmp .= $self->cleanopts ? escapehtml($value) : $value;
81 26         91 push @tag, $tmp;
82             }
83              
84 26         129 debug 2, "$self->{name}: generated tag = @tag";
85 26         228 return join ' ', @tag; # always return scalar tag
86             }
87              
88             1;
89              
90             __END__