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   8 use strict;
  2         2  
  2         54  
14 2     2   6 use warnings;
  2         2  
  2         55  
15 2     2   6 no warnings 'uninitialized';
  2         2  
  2         58  
16              
17 2     2   7 use CGI::FormBuilder::Util;
  2         2  
  2         203  
18 2     2   8 use CGI::FormBuilder::Field;
  2         2  
  2         34  
19 2     2   6 use base 'CGI::FormBuilder::Field';
  2         2  
  2         803  
20              
21              
22             our $VERSION = '3.10';
23              
24             sub script {
25 26     26 0 35 return ''; # static fields get no messages
26             }
27              
28             *render = \&tag;
29             sub tag {
30 26     26 1 50 local $^W = 0; # -w sucks
31 26         21 my $self = shift;
32 26         53 my $attr = $self->attr;
33              
34 26         106 my $jspre = $self->{_form}->jsprefix;
35              
36 26         24 my @tag;
37 26         46 my @value = $self->tag_value; # sticky is different in
38 26         57 my @opt = $self->options;
39 26         61 debug 2, "my(@opt) = \$field->options";
40              
41             # Add in our "Other:" option if applicable
42 26 50       45 push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
43             if $self->other;
44              
45 26         58 debug 2, "$self->{name}: generating $attr->{type} input type";
46              
47             # static fields are actually hidden
48 26         21 $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       51 @value = (undef) unless @value; # this creates a single-element array
54              
55 26         36 for my $value (@value) {
56 26         17 my $tmp = '';
57            
58             # setup the value
59 26         43 $attr->{value} = $value; # override
60 26 100       48 delete $attr->{value} unless defined $value;
61              
62             # render the tag
63 26         60 $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         44 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       107 $tmp .= $self->cleanopts ? escapehtml($value) : $value;
81 26         49 push @tag, $tmp;
82             }
83              
84 26         64 debug 2, "$self->{name}: generated tag = @tag";
85 26         132 return join ' ', @tag; # always return scalar tag
86             }
87              
88             1;
89              
90             __END__