blib/lib/CGI/FormBuilder/Field/number.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 148 SCAR 58.3
CC
18
Cov
0%
tag
272.0
script
12.0
BEGIN
2.0
CRAP 342.0
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 # The majority of this module's methods (including new) are
8 # inherited directly from ::base, since they involve things
9 # which are common, such as parameter parsing. The only methods
10 # that are individual to different fields are those that affect
11 # the rendering, such as script() and tag()
12  
13 package CGI::FormBuilder::Field::number;
14  
15 use strict;
16 use warnings;
17 no warnings 'uninitialized';
18  
19 use CGI::FormBuilder::Util;
20 use CGI::FormBuilder::Field;
21 use base 'CGI::FormBuilder::Field';
22  
23  
24 our $VERSION = '3.10';
25  
26 sub script {
27 my $self = shift;
28 my $name = $self->name;
29  
30 # The way script() works is slightly backwards: First the
31 # type-specific JS DOM code is generated, then this is
32 # passed as a string to Field->jsfield, which wraps this
33 # in the generic handling.
34  
35 # Holders for different parts of JS code
36 my $jsfunc = '';
37 my $jsfield = tovar($name);
38 my $close_brace = '';
39 my $in = indent(my $idt = 1); # indent
40  
41 my $alertstr = escapejs($self->jsmessage); # handle embedded '
42 $alertstr .= '\n';
43  
44 if ($self->growable) {
45 # special handling for growable, have to dynamically
46 # find out how many have been created
47 $jsfunc .= <<EOJS;
48 // $name: growable text or file box
49 var $jsfield = null;
50 var entered_$jsfield = 0;
51 var i = 0;
52 while (1) {
53 var growel = document.getElementById('$jsfield'+'_'+i);
54 if (growel == null) break; // last element
55 $jsfield = growel.value;
56 entered_$jsfield++;
57 i++;
58 EOJS
59  
60 $close_brace = <<EOJS;
61  
62 } // while $name
63 EOJS
64  
65 # required?
66 $close_brace .= <<EOJS if $self->required;
67 if (! entered_$jsfield) {
68 alertstr += '$alertstr';
69 invalid++;
70 }
71 EOJS
72 # indent the very last if/else tests so they're in the while loop
73 $in = indent($idt += 1);
74  
75 } else {
76  
77 # get value from text or other straight input
78 # at least this part makes some sense
79 $jsfunc .= <<EOJS;
80 // $name: standard text, hidden, password, or textarea box
81 var $jsfield = form.elements['$name'].value;
82 EOJS
83  
84 }
85  
86 return $self->jsfield($jsfunc, $close_brace, $in);
87 }
88  
89 *render = \&tag;
90 sub tag {
91 local $^W = 0; # -w sucks
92 my $self = shift;
93 my $attr = $self->attr;
94  
95 my $jspre = $self->{_form}->jsprefix;
96  
97 my $tag = '';
98 my @value = $self->tag_value; # sticky is different in <tag>
99 my @opt = $self->options;
100 debug 2, "my(@opt) = \$field->options";
101  
102 # Add in our "Other:" option if applicable
103 push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
104 if $self->other;
105  
106 debug 2, "$self->{name}: generating $attr->{type} input type";
107  
108 # We iterate over each value - this is the only reliable
109 # way to handle multiple form values of the same name
110 # (i.e., multiple <input> or <hidden> fields)
111 @value = (undef) unless @value; # this creates a single-element array
112  
113 # growable handling
114 my $count = 0; # for tracking the size of growable fields
115 my $limit; # for providing (optional) limits to growable fields
116 my $at_limit; # have we reached the limit of a growable field?
117 if ($self->growable && $self->growable ne 1) {
118 $limit = $self->growable;
119 }
120  
121 for my $value (@value) {
122 if ($limit && $count == $limit) {
123 belch "Number of supplied values (" . @value . ")"
124 . " for '$attr->{name}' exceeds growable limit $limit - discarding excess";
125 $at_limit = 1;
126 last;
127 }
128
129 # setup the value
130 $attr->{value} = $value; # override
131 delete $attr->{value} unless defined $value;
132  
133 if ($self->growable && $self->javascript) {
134 # the inputs in growable fields need a unique id for fb_grow()
135 $attr->{id} = tovar("$attr->{name}_$count");
136 $count++;
137 }
138  
139 # render the tag
140 $tag .= htmltag('input', $attr);
141  
142 #
143 # If we have options, lookup the label instead of the true value.
144 # This code is reached if a field is marked 'static', since
145 # that is still rendered from ::text (here) but as a 'hidden'
146 # field. These options would be leftover from a former select
147 # or radio group that is now shown on a confirm() screen. Got it?
148 #
149 for (@opt) {
150 my($o,$n) = optval($_);
151 if ($o eq $value) {
152 $n ||= $attr->{labels}{$o} || ($self->nameopts ? toname($o) : $o);
153 $value = $n;
154 last;
155 }
156 }
157  
158 if ($self->growable && $self->javascript) {
159 # put linebreaks between the input tags in growable fields
160 # this puts the "Additonal [label]" button on the same line
161 # as the last input tag
162 $tag .= '<br />' unless $count == @value;
163 } else {
164 $tag .= '<br />' if $self->linebreaks;
165 }
166 }
167 # check to see if we just hit the limit
168 $at_limit = 1 if $limit && $count == $limit;
169  
170 # add the "Additional [label]" button
171 if ($self->growable && $self->javascript) {
172 $tag .= ' ' . htmltag('input',
173 id => $self->growname,
174 type => 'button',
175 onclick => "${jspre}grow('$attr->{name}')",
176 value => sprintf($self->{_form}{messages}->form_grow_default, $self->label),
177 ( $at_limit ? ( disabled => 'disabled') : () ),
178 );
179 }
180  
181 # add an additional tag for our _other field
182 $tag .= ' ' . $self->othertag if $self->other;
183  
184 debug 2, "$self->{name}: generated tag = $tag";
185 return $tag; # always return scalar tag
186 }
187  
188 1;
189  
190 __END__