lib/CGI/FormBuilder/Field.pm

filter: Statementstmt 87.9% 358 / 407 Branchbran 69.2% 187 / 270 Conditioncond 55.7% 73 / 131 MC/DCmcdc 34.6% 27 / 78 Subroutinesub 95.4% 42 / 44 Podpod 34.3% 11 / 32 totaltotal 72.5% 698 / 962 CC 186 SCAR 31.0
CC
186
Cov
76%
validate
69.0
type
37.3
jsfield
27.1
CRAP 22.3
« lib/CGI/FormBuilder/Field/checkbox.pm ↑ Summary lib/CGI/FormBuilder/Field/radio.pm »
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 package CGI::FormBuilder::Field;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Field - Base class for FormBuilder fields
12  
13 =head1 SYNOPSIS
14  
15 use CGI::FormBuilder::Field;
16  
17 # delegated straight from FormBuilder
18 my $f = CGI::FormBuilder::Field->new($form, name => 'whatever');
19  
20 # attribute functions
21 my $n = $f->name; # name of field
22 my $n = "$f"; # stringify to $f->name
23  
24 my $t = $f->type; # auto-type
25 my @v = $f->value; # auto-stickiness
26 my @o = $f->options; # options, aligned and sorted
27  
28 my $l = $f->label; # auto-label
29 my $h = $f->tag; # field XHTML tag (name/type/value)
30 my $s = $f->script; # per-field JS validation script
31  
32 my $m = $f->message; # error message if invalid
33 my $m = $f->jsmessage; # JavaScript error message
34  
35 my $r = $f->required; # required?
36 my $k = $f->validate; # run validation check
37  
38 my $v = $f->tag_value; # value in tag (stickiness handling)
39 my $v = $f->cgi_value; # CGI value if any
40 my $v = $f->def_value; # manually-specified value
41  
42 $f->field(opt => 'val'); # FormBuilder field() call
43  
44 =cut
45  
46 10 ▶use Carp; # confess used manually in this pkg
Subroutine BEGIN: called CC=1 SCAR=0.0
47 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 10 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 10 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 10 our $VERSION = '3.20';
54 10 our $AUTOLOAD;
55  
56 # what to generate for tag
57 10 our @TAGATTR = qw(name type multiple jsclick);
58  
59 # Catches for special validation patterns
60 # These are semi-Perl patterns; they must be usable by JavaScript
61 # as well so they do not take advantage of features JS can't use
62 # If the value is an arrayref, then the second arg is a tag to
63 # spit out at the person after the field label to help with format
64  
65 10 our %VALIDATE = (
66 WORD => '/^\w+$/',
67 NAME => '/^[a-zA-Z]+$/',
68 NUM => '/^-?\s*[0-9]+\.?[0-9]*$|^-?\s*\.[0-9]+$/', # 1, 1.25, .25
69 INT => '/^-?\s*[0-9]+$/',
70 FLOAT => '/^-?\s*[0-9]+\.[0-9]+$/',
71 PHONE => '/^\d{3}\-\d{3}\-\d{4}$|^\(\d{3}\)\s+\d{3}\-\d{4}$/',
72 INTPHONE => '/^\+\d+[\s\-][\d\-\s]+$/',
73 EMAIL => '/^[\w\-\+\._]+\@[a-zA-Z0-9][-a-zA-Z0-9\.]*\.[a-zA-Z]+$/',
74 CARD => '/^\d{4}[\- ]?\d{4}[\- ]?\d{4}[\- ]?\d{4}$|^\d{4}[\- ]?\d{6}[\- ]?\d{5}$/',
75 MMYY => '/^(0?[1-9]|1[0-2])\/?[0-9]{2}$/',
76 MMYYYY => '/^(0?[1-9]|1[0-2])\/?[0-9]{4}$/',
77 DATE => '/^(0?[1-9]|1[0-2])\/?(0?[1-9]|[1-2][0-9]|3[0-1])\/?[0-9]{4}$/',
78 EUDATE => '/^(0?[1-9]|[1-2][0-9]|3[0-1])\/?(0?[1-9]|1[0-2])\/?[0-9]{4}$/',
79 TIME => '/^[0-9]{1,2}:[0-9]{2}$/',
80 AMPM => '/^[0-9]{1,2}:[0-9]{2}\s*([aA]|[pP])[mM]$/',
81 ZIPCODE => '/^\d{5}$|^\d{5}\-\d{4}$/',
82 STATE => '/^[a-zA-Z]{2}$/',
83 COUNTRY => '/^[a-zA-Z]{2}$/',
84 IPV4 => '/^([0-1]??\d{1,2}|2[0-4]\d|25[0-5])\.([0-1]??\d{1,2}|2[0-4]\d|25[0-5])\.([0-1]??\d{1,2}|2[0-4]\d|25[0-5])\.([0-1]??\d{1,2}|2[0-4]\d|25[0-5])$/',
85 NETMASK => '/^([0-1]??\d{1,2}|2[0-4]\d|25[0-5])\.([0-1]??\d{1,2}|2[0-4]\d|25[0-5])\.([0-1]??\d{1,2}|2[0-4]\d|25[0-5])\.([0-1]??\d{1,2}|2[0-4]\d|25[0-5])$/',
86 FILE => '/^[\/\w\.\-_]+$/',
87 WINFILE => '/^[a-zA-Z]:\\[\\\w\s\.\-]+$/',
88 MACFILE => '/^[:\w\.\-_]+$/',
89 USER => '/^[-a-zA-Z0-9_]{4,8}$/',
90 HOST => '/^[a-zA-Z0-9][-a-zA-Z0-9]*$/',
91 DOMAIN => '/^[a-zA-Z0-9][-a-zA-Z0-9\.]*\.[a-zA-Z]+$/',
92 ETHER => '/^[\da-f]{1,2}[\.:]?[\da-f]{1,2}[\.:]?[\da-f]{1,2}[\.:]?[\da-f]{1,2}[\.:]?[\da-f]{1,2}[\.:]?[\da-f]{1,2}$/i',
93 # Many thanks to Mark Belanger for these additions
94 FNAME => '/^[a-zA-Z]+[- ]?[a-zA-Z]*$/',
95 LNAME => '/^[a-zA-Z]+[- ]?[a-zA-Z]+\s*,?([a-zA-Z]+|[a-zA-Z]+\.)?$/',
96 CCMM => '/^0[1-9]|1[012]$/',
97 CCYY => '/^[1-9]{2}$/',
98 );
99  
100 # stringify to name
101 6468 ▶use overload '""' => sub { $_[0]->name },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
102 #'.' => sub { $_[0]->name },
103 0 ▶ '0+' => sub { $_[0]->name },
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 106 ▶ 'bool' => sub { $_[0]->name },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 10 ▶ 'eq' => sub { $_[0]->name eq $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
106  
107 340 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
108 340 ▶ puke "Not enough arguments for Field->new()" unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 340 340
109 340 my $self = shift;
110  
111 340 my $form = shift; # need for top-level attr
112 340 my $opt = arghash(@_);
113 340 $opt->{_form} = $form; # parental ptr
114 puke "Missing name for field() in Field->new()"
115 340 ▶ unless $opt->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"name"} 0 340 340
116  
117 340 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
03400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
118 340 my $f = bless $opt, $class;
119  
120 # Note that at this point, the object is a generic field
121 # without a type. Not until it's called via $f->type does
122 # it get a type, which affects its HTML representation.
123 # Everything else is inherited from this module.
124  
125 340 return $f;
126 }
127  
128 181 ▶sub field {
Subroutine field: called CC=5 SCAR=16.2
Pod documented
129 181 my $self = shift;
130  
131 181 ▶ if (ref $_[0] || @_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] or @_ > 1) 152 29 181
Condition66%2/3
ref $_[0] or @_ > 1
l!l&&r!l&&!r
015229
MC/DC50%1/2
ref $_[0] or @_ > 1
ref $_[0] @_ > 1
Truth table66%2/3
A or B
A= ref $_[0]B= @_ > 1
ABresult
1X1
011
000
132 152 my $opt = arghash(@_);
133 152 ▶ while (my($k,$v) = each %$opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$opt) 215 152 367
134 215 ▶ next if $k eq 'name'; # segfault??
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "name" 0 215 215
135 215 $self->{$k} = $v;
136 }
137 }
138 181 return $self->value; # needed for @v = $form->field('name')
139 }
140  
141 10 *override = \&force; # CGI.pm
142 1245 ▶sub force {
Subroutine force: called CC=3 SCAR=11.0
Pod undocumented
143 1245 my $self = shift;
144 1245 ▶ $self->{force} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1245 1245
145 1245 ▶ return $self->{force} || $self->{override};
Condition100%3/3
$self->{'force'} || $self->{'override'}
l!l&&r!l&&!r
2031222
MC/DC100%2/2
$self->{'force'} || $self->{'override'}
$self->{'force'} $self->{'override'}
Truth table100%3/3
A || B
A= $self->{'force'}B= $self->{'override'}
ABresult
1X1
011
000
146 }
147  
148 # grab the field_other field if other => 1 specified
149 2349 ▶sub other {
Subroutine other: called CC=5 SCAR=16.5
Pod undocumented
150 2349 my $self = shift;
151 2349 ▶ $self->{other} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2349 2349
152 2349 ▶ return unless $self->{other};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"other"} 2254 95 2349
153 95 ▶ $self->{other} = {} unless ref $self->{other};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"other"} 6 89 95
154 95 $self->{other}{name} = $self->othername;
155 0 ▶ return wantarray ? %{$self->{other}} : $self->{other};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 95 95
156 }
157  
158 329 ▶sub othername {
Subroutine othername: called CC=1 SCAR=0.0
Pod undocumented
159 329 my $self = shift;
160 329 return $self->{_form}->othername . '_' . $self->name;
161 }
162  
163 7 ▶sub othertag {
Subroutine othertag: called CC=7 SCAR=19.5
Pod undocumented
164 7 my $self = shift;
165 7 ▶ return '' unless $self->other;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->other 0 7 7
166  
167 # add an additional tag for our _other field
168 7 my $oa = $self->other; # other attr
169  
170 # default settings
171 7 ▶ $oa->{type} ||= 'text';
Condition100%2/2
$oa->{"type"} ||= "text"
l!l
25
MC/DC100%1/1
$oa->{"type"} ||= "text"
$oa->{"type"}
Truth table100%2/2
A
A= $oa->{"type"}
Aresult
11
00
172 7 my $v = $self->{_form}->cgi_param($self->othername);
173 #$v = $self->tag_value unless defined $v;
174 7 ▶ if ($self->sticky and defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sticky and defined $v) 1 6 7
Condition66%2/3
$self->sticky and defined $v
!ll&&!rl&&r
061
MC/DC50%1/2
$self->sticky and defined $v
$self->sticky defined $v
Truth table66%2/3
A and B
A= $self->stickyB= defined $v
ABresult
0X0
100
111
175 1 $oa->{value} = $v;
176 }
177  
178 7 ▶ $oa->{disabled} = 'disabled' if $self->javascript && ! defined $v; # fanciness
Branch: 1 branch
BranchTrueFalseTotal
if $self->javascript and not defined $v 4 3 7
Condition100%3/3
$self->javascript and not defined $v
!ll&&!rl&&r
214
MC/DC100%2/2
$self->javascript and not defined $v
$self->javascript not defined $v
Truth table100%3/3
A and not(B)
A= $self->javascriptB= not defined $v
ABresult
0X0
100
111
179 7 return htmltag('input', $oa);
180 }
181  
182 2 ▶sub growname {
Subroutine growname: called CC=1 SCAR=0.0
Pod undocumented
183 2 my $self = shift;
184 2 return $self->{_form}->growname . '_' . $self->name;
185 }
186  
187 1222 ▶sub cgi_value {
Subroutine cgi_value: called CC=8 SCAR=21.1
Pod documented
188 1222 my $self = shift;
189 1222 debug 2, "$self->{name}: called \$field->cgi_value";
190 1222 ▶ puke "Cannot set \$field->cgi_value manually" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1222 1222
191 1222 ▶ if (my @v = $self->{_form}{params}->can('multi_param') ? $self->{_form}{params}->multi_param($self->name) : $self->{_form}{params}->param($self->name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@v) = $self->{"_form"}{"params"}->can("multi_param") ? $self->{"_form"}{"params"}->multi_param($self->name) : $self->{"_form"}{"params"}->param($self->name)) 173 1049 1222
$self->{'_form'}{'params'}->can('multi_param') ? : 1222 0 1222
192 173 for my $v (@v) {
193 179 ▶ if ($self->other && $v eq $self->othername) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other and $v eq $self->othername) 6 173 179
Condition66%2/3
$self->other and $v eq $self->othername
!ll&&!rl&&r
17306
MC/DC50%1/2
$self->other and $v eq $self->othername
$self->other $v eq $self->othername
Truth table66%2/3
A and B
A= $self->otherB= $v eq $self->othername
ABresult
0X0
100
111
194 6 debug 1, "$self->{name}: redoing value from _other field";
195 6 $v = $self->{_form}{params}->param($self->othername);
196 }
197 }
198 173 local $" = ',';
199 173 debug 2, "$self->{name}: cgi value = (@v)";
200 173 ▶ return wantarray ? @v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 173 0 173
201 }
202 1049 return;
203 }
204  
205 1181 ▶sub def_value {
Subroutine def_value: called CC=3 SCAR=11.3
Pod documented
206 1181 my $self = shift;
207 1181 debug 2, "$self->{name}: called \$field->def_value";
208 1181 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1181 1181
209 0 $self->{value} = arglist(@_); # manually set
210 0 delete $self->{_cache}{type}; # clear auto-type
211 }
212 1181 my @v = autodata $self->{value};
213 1181 local $" = ',';
214 1181 debug 2, "$self->{name}: def value = (@v)";
215 1181 $self->inflate_value(\@v);
216 1181 ▶ return wantarray ? @v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 999 182 1181
217 }
218  
219 1248 ▶sub inflate_value {
Subroutine inflate_value: called CC=9 SCAR=22.2
Pod undocumented
220 1248 my ($self, $v_aref) = @_;
221  
222 1248 debug 2, "$self->{name}: called \$field->inflate_value";
223  
224 # trying to inflate?
225 1248 ▶ return unless exists $self->{inflate};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"inflate"} 1246 2 1248
226 2 debug 2, "$self->{name}: inflate routine exists";
227  
228 # must return real values to the validate() routine:
229 2 ▶ return if grep { ((caller($_))[3] eq 'CGI::FormBuilder::Field::validate') }
Branch: 1 branch
BranchTrueFalseTotal
if grep {(caller $_)[3] eq "CGI::FormBuilder::Field::validate";} (1, 2) 0 2 2
230 1..2;
231 2 debug 2, "$self->{name}: made sure inflate not called via validate";
232  
233 # must be valid:
234 #return unless exists $self->{invalid} && ! $self->{invalid};
235 2 ▶ return if $self->invalid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->invalid 0 2 2
236 2 debug 2, "$self->{name}: valid field, inflate proceeding";
237  
238 2 my $cache = $self->{inflated_values};
239  
240 2 ▶ if ($cache && ref $cache eq 'ARRAY' && @{$cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and ref $cache eq 'ARRAY' and @{$cache;}) { } 1 1 2
Condition66%4/6
$cache and ref $cache eq "ARRAY" and @{$cache;}
!ll&&!rl&&r
101
$cache and ref $cache eq "ARRAY"
!ll&&!rl&&r
101
MC/DC33%1/3
$cache and ref $cache eq "ARRAY" and @{$cache;}
$cache ref $cache eq "ARRAY" @{$cache;}
Truth table50%2/4
A and B and C
A= $cacheB= ref $cache eq "ARRAY"C= @{$cache;}
ABCresult
0XX0
10X0
1100
1111
241 # could have been cached by validate() check
242 1 @{ $v_aref } = @{ $self->{inflated_values} };
243 1 debug 2, "$self->{name}: using cached inflate "
244 . "value from validate()";
245 }
246 else {
247 1 debug 2, "$self->{name}: new inflate";
248  
249 puke("Field $self->{name}: inflate must be a reference to a \\&sub")
250 1 ▶ if ref $self->{inflate} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"inflate"} ne "CODE" 0 1 1
251  
252 1 eval { @{ $v_aref } = map $self->{inflate}->($_), @{ $v_aref } };
253  
254 # no choice but to die hard if didn't validate() first
255 1 ▶ puke("Field $self->{name}: inflate failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
256  
257 # cache the result:
258 1 @{ $self->{inflated_values} } = @{ $v_aref };
259 }
260 2 return;
261 }
262  
263 # CGI.pm happiness
264 10 *default = \&value;
265 10 *defaults = \&value;
266 10 *values = \&value;
267 277 ▶sub value {
Subroutine value: called CC=5 SCAR=16.2
Pod documented
268 277 my $self = shift;
269 277 debug 2, "$self->{name}: called \$field->value(@_)";
270 277 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 277 277
271 0 $self->{value} = arglist(@_); # manually set
272 0 delete $self->{_cache}{type}; # clear auto-type
273 }
274 277 ▶ unless ($self->force) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->force) 265 12 277
275 # CGI wins if stickiness is set
276 265 debug 2, "$self->{name}: sticky && ! force";
277 265 ▶ if (my @v = $self->cgi_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@v) = $self->cgi_value) 67 198 265
278 67 local $" = ',';
279 67 debug 1, "$self->{name}: returning value (@v)";
280 67 $self->inflate_value(\@v);
281 67 ▶ return wantarray ? @v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 49 67
282 }
283 }
284 210 debug 2, "no cgi found, returning def_value";
285 # no CGI value, or value was forced, or not sticky
286 210 return $self->def_value;
287 }
288  
289 # The value in the <tag> may be different than in code (sticky)
290 1041 ▶sub tag_value {
Subroutine tag_value: called CC=7 SCAR=19.9
Pod documented
291 1041 my $self = shift;
292 1041 debug 2, "$self->{name}: called \$field->tag_value";
293 1041 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1041 1041
294 # setting the tag_value manually is odd...
295 0 $self->{tag_value} = arglist(@_);
296 0 delete $self->{_cache}{type};
297 }
298 1041 ▶ return $self->{tag_value} if $self->{tag_value};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tag_value"} 0 1041 1041
299  
300 1041 ▶ if ($self->sticky && ! $self->force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sticky and not $self->force) 957 84 1041
Condition100%3/3
$self->sticky and not $self->force
!ll&&!rl&&r
7311957
MC/DC100%2/2
$self->sticky and not $self->force
$self->sticky not $self->force
Truth table100%3/3
A and not(B)
A= $self->stickyB= not $self->force
ABresult
0X0
100
111
301 # CGI wins if stickiness is set
302 957 debug 2, "$self->{name}: sticky && ! force";
303 957 ▶ if (my @v = $self->cgi_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@v) = $self->cgi_value) 106 851 957
304 106 local $" = ',';
305 106 debug 1, "$self->{name}: returning value (@v)";
306 106 ▶ return wantarray ? @v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 106 0 106
307 }
308 }
309 935 debug 2, "no cgi found, returning def_value";
310 # no CGI value, or value was forced, or not sticky
311 935 return $self->def_value;
312 }
313  
314 # Handle "b:select" and "b:option"
315 0 ▶sub tag_name {
Subroutine tag_name: not called CC=4 SCAR=30.0
Pod documented
316 0 my $self = shift;
317 0 ▶ $self->{tag_name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
318 0 ▶ return $self->{tag_name} if $self->{tag_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tag_name"} 0 0 0
319 # Try to guess
320 0 my($tag) = ref($self) =~ /^CGI::FormBuilder::Field::(.+)/;
321 0 ▶ puke "Can't resolve tag for untyped field '$self->{name}'"
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 0 0 0
322 unless $tag;
323 0 return $tag;
324 }
325  
326 1334 ▶sub type {
Subroutine type: called CC=21 SCAR=36.2
Pod documented
327 1334 local $^W = 0; # -w sucks
328 1334 my $self = shift;
329 1334 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1334 1334
330 0 $self->{type} = lc shift;
331 0 delete $self->{_cache}{type}; # forces rebless
332 0 debug 2, "setting field type to '$self->{type}'";
333 }
334  
335 #
336 # catch for new way of saying static => 1
337 #
338 # confirm() will set ->static but not touch $self->{type},
339 # so make sure it's not a field the user hid themselves
340 #
341 1334 ▶ if ($self->static && $self->{type} ne 'hidden') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->static and $self->{"type"} ne "hidden") 36 1298 1334
Condition66%2/3
$self->static and $self->{"type"} ne "hidden"
!ll&&!rl&&r
1298036
MC/DC50%1/2
$self->static and $self->{"type"} ne "hidden"
$self->static $self->{"type"} ne "hidden"
Truth table66%2/3
A and B
A= $self->staticB= $self->{"type"} ne "hidden"
ABresult
0X0
100
111
342 36 $self->{type} = 'static';
343 36 delete $self->{_cache}{type}; # forces rebless
344 36 debug 2, "setting field type to '$self->{type}'";
345 }
346  
347 # manually set
348 1334 debug 2, "$self->{name}: called \$field->type (manual = '$self->{type}')";
349  
350 # The $field->type method is called so often that it really slows
351 # things down. As such, we cache the type and use it *unless* the
352 # value has been updated manually (we assume one CGI instance).
353 # See value() for its deletion of this cache
354 1334 ▶ return $self->{_cache}{type} if $self->{_cache}{type};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_cache"}{"type"} 1100 234 1334
355  
356 234 my $name = $self->{name};
357 234 my $type;
358 234 ▶ unless ($type = lc $self->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type = lc $self->{"type"}) 166 68 234
359 #
360 # Unless the type has been set explicitly, we make a guess
361 # based on how many items there are to display, which is
362 # basically, how many options we have. Our 'jsclick' option
363 # is now changed down in the javascript section, fixing a bug
364 #
365 166 ▶ if ($self->{_form}->smartness) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_form"}->smartness) 166 0 166
366 166 debug 1, "$name: input type not set, checking for options";
367 166 ▶ if (my $n = $self->options) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $n = $self->options) { } 51 115 166
elsif ($self->{'_form'}->smartness > 1) { } 0 115 115
368 51 debug 2, "$name: has options, so setting to select|radio|checkbox";
369 51 ▶ if ($n >= $self->selectnum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->selectnum) { } 15 36 51
370 15 debug 2, "$name: has more than selectnum (", $self->selectnum,
371 ") options, setting to 'select'";
372 15 $type = 'select';
373 } else {
374 # Something is a checkbox if it is a multi-valued box.
375 # However, it is *also* a checkbox if only single-valued options,
376 # otherwise you can't unselect it.
377 36 my @v = $self->def_value; # only on manual, not dubious CGI
378 36 ▶ if ($self->multiple || @v > 1 || $n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->multiple or @v > 1 or $n == 1) { } 8 28 36
Condition83%5/6
$self->multiple or @v > 1 or $n == 1
l!l&&r!l&&!r
7128
$self->multiple or @v > 1
l!l&&r!l&&!r
7029
MC/DC66%2/3
$self->multiple or @v > 1 or $n == 1
$self->multiple @v > 1 $n == 1
Truth table75%3/4
A or B or C
A= $self->multipleB= @v > 1C= $n == 1
ABCresult
1XX1
01X1
0011
0000
379 8 debug 2, "$name: has multiple select < selectnum, setting to 'checkbox'";
380 8 $type = 'checkbox';
381 } else {
382 28 debug 2, "$name: has singular select < selectnum, setting to 'radio'";
383 28 $type = 'radio';
384 }
385 }
386 } elsif ($self->{_form}->smartness > 1) {
387 0 debug 2, "$name: smartness > 1, auto-inferring type based on value";
388 # only autoinfer field types based on values with high smartness
389 0 my @v = $self->def_value; # only on manual, not dubious CGI
390 0 ▶ if ($name =~ /passw(or)?d/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name =~ /passw(or)?d/i) { } 0 0 0
elsif ($name =~ /(?:details?|comments?)$/i or grep /\n|\r/, @v || $self->cols || $self->rows) { } 0 0 0
elsif ($name =~ /\bfile/i) { } 0 0 0
391 0 $type = 'password';
392 ▶ } elsif ($name =~ /(?:details?|comments?)$/i
Condition0%0/9
$name =~ /(?:details?|comments?)$/i or grep /\n|\r/, @v || $self->cols || $self->rows
l!l&&r!l&&!r
000
@v || $self->cols || $self->rows
l!l&&r!l&&!r
000
@v or $self->cols
l!l&&r!l&&!r
000
MC/DC0%0/2
$name =~ /(?:details?|comments?)$/i or grep /\n|\r/, @v || $self->cols || $self->rows
$name =~ /(?:details?|comments?)$/i grep /\n|\r/, @v || $self->cols || $self->rows
MC/DC0%0/3
@v || $self->cols || $self->rows
@v $self->cols $self->rows
Truth table0%0/3
A or B
A= $name =~ /(?:details?|comments?)$/iB= grep /\n|\r/, @v || $self->cols || $self->rows
ABresult
1X1
011
000
Truth table0%0/4
A or B || C
A= @vB= $self->colsC= $self->rows
ABCresult
1XX1
01X1
0011
0000
393 || grep /\n|\r/, @v || $self->cols || $self->rows) {
394 0 $type = 'textarea';
395 } elsif ($name =~ /\bfile/i) {
396 0 $type = 'file';
397 }
398 } else {
399 115 debug 2, "no options found";
400 }
401 }
402 166 ▶ $type ||= 'text'; # default if no fancy settings matched or no smartness
Condition100%2/2
$type ||= "text"
l!l
51115
MC/DC100%1/1
$type ||= "text"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
403 }
404 234 debug 1, "$name: field set to type '$type' (reblessing)";
405  
406 # Store type in cache for speediness
407 234 $self->{_cache}{type} = $type;
408  
409 # Re-bless into the appropriate package
410 234 my $pkg = __PACKAGE__ . '::' . $type;
411 234 $pkg =~ s/\-/_/g; # handle HTML5 type names ala 'datetime-local'
412 234 eval "require $pkg";
413 234 ▶ puke "Can't load $pkg for field '$name' (type '$type'): $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 234 234
414 234 bless $self, $pkg;
415  
416 234 return $type;
417 }
418  
419 774 ▶sub label {
Subroutine label: called CC=3 SCAR=11.0
Pod documented
420 774 my $self = shift;
421 774 ▶ $self->{label} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 774 774
422 774 ▶ return $self->{label} if defined $self->{label}; # manually set
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"label"} 82 692 774
423 692 return toname($self->name);
424 }
425  
426 383 ▶sub attr {
Subroutine attr: called CC=15 SCAR=28.0
Pod undocumented
427 383 my $self = shift;
428 383 ▶ if (my $k = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $k = shift()) { } 0 383 383
429 0 ▶ $self->{$k} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
430 0 ▶ return exists $self->{$k} ? $self->{$k} : $self->{_form}->$k;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$k} ? : 0 0 0
431 } else {
432 # exhaustive expansion, but don't invoke validate().
433 383 my %ret;
434 383 for my $k (@TAGATTR, keys %$self) {
435 5082 my $v;
436 5082 ▶ next if $k =~ /^_/ || $k eq 'validate'; # don't invoke validate
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^_/ or $k eq "validate" 874 4208 5082
Condition100%3/3
$k =~ /^_/ or $k eq "validate"
l!l&&r!l&&!r
7661084208
MC/DC100%2/2
$k =~ /^_/ or $k eq "validate"
$k =~ /^_/ $k eq "validate"
Truth table100%3/3
A or B
A= $k =~ /^_/B= $k eq "validate"
ABresult
1X1
011
000
437 4208 ▶ if ($k eq 'jsclick') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'jsclick') { } 18 4190 4208
elsif (exists $self->{$k}) { } 3134 1056 4190
438 # always has to be a special fucking case
439 18 $v = $self->{$k};
440 18 $k = $self->jstype;
441 } elsif (exists $self->{$k}) {
442 # flat val
443 3134 $v = $self->{$k};
444 3134 ▶ $v = lc $v if $k eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "type" 162 2972 3134
445 } else {
446 1056 $v = $self->$k;
447 }
448 4208 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 767 3441 4208
449  
450 3441 debug 3, "$self->{name}: \$attr{$k} = '$v'";
451 3441 $ret{$k} = $v;
452 }
453  
454 # More special cases
455 # 1. disabled field/form
456 $self->disabled ? $ret{disabled} = 'disabled'
457 383 ▶ : delete $ret{disabled};
Branch: 1 branch
BranchTrueFalseTotal
$self->disabled ? : 8 375 383
458  
459 # 2. setup class for stylesheets and JS vars
460 $ret{class} ||= $self->{_form}->class('_'.
461 ($ret{type} eq 'text' ? 'input' : $ret{type})
462 383 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$ret{'type'} eq 'text' ? : 232 151 383
Condition66%2/3
$ret{"class"} ||= $self->{"_form"}->class('_' . ($ret{"type"} eq "text" ? "input" : $ret{"type"}))
l!l&&r!l&&!r
090293
MC/DC50%1/2
$ret{"class"} ||= $self->{"_form"}->class('_' . ($ret{"type"} eq "text" ? "input" : $ret{"type"}))
$ret{"class"} $self->{"_form"}->class('_' . ($ret{"type"} eq "text" ? "input" : $ret{"type"}))
Truth table66%2/3
A ||= B
A= $ret{"class"}B= $self->{"_form"}->class('_' . ($ret{"type"} eq "text" ? "input" : $ret{"type"}))
ABresult
1X1
011
000
463  
464 # 3. useless in all tags
465 383 delete $ret{value};
466  
467 383 ▶ return wantarray ? %ret : \%ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 383 383
468 }
469 }
470  
471 460 ▶sub multiple {
Subroutine multiple: called CC=4 SCAR=14.2
Pod undocumented
472 460 my $self = shift;
473 460 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 460 460
474 0 $self->{multiple} = shift; # manually set
475 0 delete $self->{_cache}{type}; # clear auto-type
476 }
477 460 ▶ return 'multiple' if $self->{multiple}; # manually set
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 3 457 460
478 457 my @v = $self->tag_value;
479 457 ▶ return 'multiple' if @v > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @v > 1 44 413 457
480 413 return;
481 }
482  
483 760 ▶sub options {
Subroutine options: called CC=5 SCAR=16.4
Pod undocumented
484 760 my $self = shift;
485 760 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 760 760
486 0 $self->{options} = shift; # manually set
487 0 delete $self->{_cache}{type}; # clear auto-type
488 }
489 760 ▶ return unless $self->{options};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"options"} 522 238 760
490  
491 # align options per internal settings
492 238 my @opt = optalign($self->{options});
493  
494 # scalar is just counting length, so skip sort
495 238 ▶ return @opt unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 67 171 238
496  
497 # sort if requested
498 171 ▶ @opt = optsort($self->sortopts, @opt) if $self->sortopts;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sortopts 18 153 171
499  
500 171 return @opt;
501 }
502  
503 # per-field messages
504 4 ▶sub message {
Subroutine message: called CC=9 SCAR=25.4
Pod undocumented
505 4 my $self = shift;
506 4 ▶ $self->{message} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
507 4 my $mess = $self->{message};
508 4 ▶ unless ($mess) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mess) 4 0 4
509 4 ▶ my $type = shift || $self->type;
Condition33%1/3
shift() || $self->type
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || $self->type
shift() $self->type
Truth table33%1/3
A || B
A= shift()B= $self->type
ABresult
1X1
011
000
510 4 ▶ my $et = 'form_invalid_' . ($type eq 'text' ? 'input' : $type);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'text' ? : 0 4 4
511 4 ▶ $et = 'form_invalid_input' if $self->other; # other fields assume text
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 0 4 4
512 $mess = $self->{_form}{messages}->$et
513 4 ▶ || $self->{_form}{messages}->form_invalid_default;
Condition33%1/3
$self->{'_form'}{'messages'}->$et || $self->{'_form'}{'messages'}->form_invalid_default
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{'_form'}{'messages'}->$et || $self->{'_form'}{'messages'}->form_invalid_default
$self->{'_form'}{'messages'}->$et $self->{'_form'}{'messages'}->form_invalid_default
Truth table33%1/3
A || B
A= $self->{'_form'}{'messages'}->$etB= $self->{'_form'}{'messages'}->form_invalid_default
ABresult
1X1
011
000
514 4 ▶ $mess = sprintf($mess, $self->label) if $mess =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if $mess =~ /%/ 0 4 4
515 }
516 return $self->{_form}{stylesheet}
517 4 ▶ ? qq(<span class="$self->{_form}{styleclass}_message">$mess</span>)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_form'}{'stylesheet'} ? : 4 0 4
518 : $mess;
519 }
520  
521 400 ▶sub jsmessage {
Subroutine jsmessage: called CC=8 SCAR=22.9
Pod undocumented
522 400 my $self = shift;
523 400 ▶ $self->{jsmessage} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 400 400
524 400 ▶ my $mess = $self->{jsmessage} || $self->{message};
Condition33%1/3
$self->{'jsmessage'} || $self->{'message'}
l!l&&r!l&&!r
00400
MC/DC0%0/2
$self->{'jsmessage'} || $self->{'message'}
$self->{'jsmessage'} $self->{'message'}
Truth table33%1/3
A || B
A= $self->{'jsmessage'}B= $self->{'message'}
ABresult
1X1
011
000
525 400 ▶ unless ($mess) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mess) 400 0 400
526 400 ▶ my $type = shift || $self->type;
Condition33%1/3
shift() || $self->type
l!l&&r!l&&!r
04000
MC/DC0%0/2
shift() || $self->type
shift() $self->type
Truth table33%1/3
A || B
A= shift()B= $self->type
ABresult
1X1
011
000
527 400 ▶ my $et = 'js_invalid_' . ($type eq 'text' ? 'input' : $type);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'text' ? : 275 125 400
528 400 ▶ $et = 'js_invalid_input' if $self->other; # other fields assume text
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 7 393 400
529 $mess = sprintf(($self->{_form}{messages}->$et
530 400 ▶ || $self->{_form}{messages}->js_invalid_default),
Condition33%1/3
$self->{'_form'}{'messages'}->$et || $self->{'_form'}{'messages'}->js_invalid_default
l!l&&r!l&&!r
40000
MC/DC0%0/2
$self->{'_form'}{'messages'}->$et || $self->{'_form'}{'messages'}->js_invalid_default
$self->{'_form'}{'messages'}->$et $self->{'_form'}{'messages'}->js_invalid_default
Truth table33%1/3
A || B
A= $self->{'_form'}{'messages'}->$etB= $self->{'_form'}{'messages'}->js_invalid_default
ABresult
1X1
011
000
531 $self->label);
532 }
533 400 return $mess
534 }
535  
536 383 ▶sub comment {
Subroutine comment: called CC=4 SCAR=13.9
Pod undocumented
537 383 my $self = shift;
538 383 ▶ $self->{comment} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 383 383
539 383 ▶ my $mess = $self->{comment} || return '';
Condition100%2/2
$self->{'comment'} || (return '')
l!l
37346
MC/DC100%1/1
$self->{'comment'} || (return '')
$self->{'comment'}
Truth table100%2/2
A
A= $self->{'comment'}
Aresult
11
00
540 return $self->{_form}{stylesheet}
541 37 ▶ ? qq(<span class="$self->{_form}{styleclass}_comment">$mess</span>)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_form'}{'stylesheet'} ? : 12 25 37
542 : $mess;
543 }
544  
545 # simple error wrapper (why wasn't this here?)
546 201 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod undocumented
547 201 my $self = shift;
548 201 ▶ return $self->invalid ? $self->message : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->invalid ? : 2 199 201
549 }
550  
551 18 ▶sub jstype {
Subroutine jstype: called CC=4 SCAR=14.6
Pod undocumented
552 18 my $self = shift;
553 18 ▶ my $type = shift || $self->type;
Condition33%1/3
shift() || $self->type
l!l&&r!l&&!r
0180
MC/DC0%0/2
shift() || $self->type
shift() $self->type
Truth table33%1/3
A || B
A= shift()B= $self->type
ABresult
1X1
011
000
554 18 ▶ return ($type eq 'radio' || $type eq 'checkbox') ? 'onclick' : 'onchange';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'radio' || $type eq 'checkbox' ? : 5 13 18
Condition66%2/3
$type eq "radio" or $type eq "checkbox"
l!l&&r!l&&!r
5013
MC/DC50%1/2
$type eq "radio" or $type eq "checkbox"
$type eq "radio" $type eq "checkbox"
Truth table66%2/3
A or B
A= $type eq "radio"B= $type eq "checkbox"
ABresult
1X1
011
000
555 }
556  
557 3 ▶sub script {
Subroutine script: called CC=1 SCAR=0.0
Pod undocumented
558 3 my $self = shift;
559 #
560 # An unfortunate hack. Sometimes (often?) we don't know the field
561 # type until render(), in which Javascript is generated first. So,
562 # the grandfather (this) of all script() methods just sets the type
563 # by calling $self->type in a void context (which reblesses the object)
564 # and then calling $self->script again. I think this sucks, but then
565 # again this code shouldn't be called that often. Maybe.
566 #
567 3 $self->type;
568 3 $self->script;
569 }
570  
571 323 ▶sub jsfield {
Subroutine jsfield: called CC=18 SCAR=33.0
Pod undocumented
572 323 my $self = shift;
573 323 my $name = $self->name;
574 323 my $pattern = $self->{validate};
575 323 debug 2, "return '' unless ".$self->javascript." && ($pattern || ".$self->required.")";
576 323 ▶ return '' unless $self->javascript && ($pattern || $self->required);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->javascript and $pattern || $self->required 246 77 323
Condition83%5/6
$self->javascript and $pattern || $self->required
!ll&&!rl&&r
024677
$pattern or $self->required
l!l&&r!l&&!r
4136246
MC/DC66%2/3
$self->javascript and $pattern || $self->required
$self->javascript $pattern $self->required
Truth table75%3/4
A and B or C
A= $self->javascriptB= $patternC= $self->required
ABCresult
0XX0
1000
11X1
1011
577  
578 # First arg is the script that our children should've included
579 77 my($jsfunc, $close_brace, $in) = @_;
580 77 ▶ unless ($jsfunc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jsfunc) 0 77 77
581 0 belch "Missing generated \$jsfunc string for $name->jsfield";
582 0 return '';
583 }
584  
585 77 debug 1, "$name: generating JavaScript validation code";
586  
587 # Special catch, since many would assume this would work
588 77 ▶ if (ref $pattern eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "Regexp") 0 77 77
589 0 puke "To use a regex in a 'validate' option you must specify ".
590 "it in single quotes, like '/^\\w+\$/' - failed on '$name' field";
591 }
592  
593 # hashref is a grouping per-language
594 77 ▶ if (ref $pattern eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "HASH") 0 77 77
595 0 ▶ $pattern = $pattern->{javascript} || return '';
Condition0%0/2
$pattern->{'javascript'} || (return '')
l!l
00
MC/DC0%0/1
$pattern->{'javascript'} || (return '')
$pattern->{'javascript'}
Truth table0%0/2
A
A= $pattern->{'javascript'}
Aresult
11
00
596 }
597  
598 # Check our hash to see if it's a special pattern
599 77 ▶ $pattern = $VALIDATE{$pattern} if $VALIDATE{$pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Field::VALIDATE{$pattern} 33 44 77
600  
601 # Make field name JS-safe
602 77 my $jsfield = tovar($name);
603  
604 # Note we have to use form.elements['name'] instead of just form.name
605 # as the JAPH using this module may have defined fields like "u.type"
606 77 my $alertstr = escapejs($self->jsmessage); # handle embedded '
607 77 $alertstr .= '\n';
608  
609 # Our fields are only required if the required option is set
610 # So, if not set, add a not-null check to the if below
611 77 ▶ my $notnull = $self->required
Branch: 1 branch
BranchTrueFalseTotal
$self->required ? : 71 6 77
612 ? qq[$jsfield == null ||] # must have or error
613 : qq[$jsfield != null && $jsfield != "" &&]; # only care if filled in
614  
615 77 ▶ if ($pattern =~ m#^m?(\S)(.*)\1([gimsu]+)?$#) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pattern =~ /^m?(\S)(.*)\1([gimsu]+)?$/) { } 39 38 77
elsif (ref $pattern eq 'ARRAY') { } 2 36 38
elsif (ref $pattern eq 'CODE' or $pattern eq 'VALUE' or $self->required and not $pattern) { } 36 0 36
616 # JavaScript regexp
617 39 ($pattern = $2) =~ s/\\\//\//g;
618 39 $pattern =~ s/\//\\\//g;
619 39 $jsfunc .= qq[${in}if ($notnull ! $jsfield.match(/$pattern/$3)) {\n];
620 }
621 elsif (ref $pattern eq 'ARRAY') {
622 # Must be w/i this set of values
623 # escape single-quotes in option values
624 2 my @options = @{$pattern}; # clone values, don't modify original
625 2 s{'}{\\'}g for @options;
626 # the third argument to jsfield,
627 # then check it is not null or empty string,
628 # then make sure it is one of the provided options
629 2 $jsfunc .= qq[${in}if ($notnull ($jsfield != ']
630 . join("' && $jsfield != '", @options) . "')) {\n";
631 }
632 ▶ elsif (ref $pattern eq 'CODE' || $pattern eq 'VALUE' || ($self->required && ! $pattern)) {
Condition33%3/9
ref $pattern eq "CODE" or $pattern eq "VALUE" or $self->required and not $pattern
l!l&&r!l&&!r
0360
ref $pattern eq "CODE" or $pattern eq "VALUE"
l!l&&r!l&&!r
0036
$self->required and not $pattern
!ll&&!rl&&r
0036
MC/DC0%0/4
ref $pattern eq "CODE" or $pattern eq "VALUE" or $self->required and not $pattern
ref $pattern eq "CODE" $pattern eq "VALUE" $self->required not $pattern
Truth table20%1/5
A or B or C and not(D)
A= ref $pattern eq "CODE"B= $pattern eq "VALUE"C= $self->requiredD= not $pattern
ABCDresult
1XXX1
01XX1
00111
000X0
00100
633 # Not null (for required sub refs, just check for a value)
634 36 $jsfunc .= qq[${in}if ($notnull $jsfield === "") {\n];
635 }
636 else {
637 # Literal string is literal code to execute, but provide
638 # a warning just in case
639 0 ▶ belch "Validation string '$pattern' may be a typo of a builtin pattern"
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /^[A-Z]+$/ 0 0 0
640 if $pattern =~ /^[A-Z]+$/;
641 0 $jsfunc .= qq[${in}if ($notnull $jsfield $pattern) {\n];
642 }
643  
644 # add on our alert message, but only if it's required
645 77 $jsfunc .= <<EOJS;
646 $in alertstr += '$alertstr';
647 $in invalid++;
648 $in invalid_fields.push('$jsfield');
649 $in}$close_brace
650 EOJS
651  
652 77 return $jsfunc;
653 }
654  
655 10 *render = \&tag;
656 207 ▶sub tag {
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
657 207 my $self = shift;
658 207 $self->type;
659 207 return $self->tag(@_);
660 }
661  
662 41 ▶sub validate () {
Subroutine validate: called CC=40 SCAR=42.3
Pod documented
663  
664 # This function does all the validation on the Perl side.
665 # It doesn't generate JavaScript; see render() for that...
666  
667 41 my $self = shift;
668 41 my $form = $self->{_form}; # alias for examples (paint-by-numbers)
669 41 local $^W = 0; # -w sucks
670  
671 41 ▶ my $pattern = shift || $self->{validate};
Condition66%2/3
shift() || $self->{'validate'}
l!l&&r!l&&!r
02318
MC/DC50%1/2
shift() || $self->{'validate'}
shift() $self->{'validate'}
Truth table66%2/3
A || B
A= shift()B= $self->{'validate'}
ABresult
1X1
011
000
672 41 my $field = $self->name;
673  
674 # inflation subref?
675 41 ▶ my $inflate = (exists $self->{inflate}) ? $self->{inflate} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'inflate'} ? : 0 41 41
676 41 ▶ puke("$field: inflate attribute must be subroutine reference")
Branch: 1 branch
BranchTrueFalseTotal
if defined $inflate and ref $inflate ne "CODE" 0 41 41
Condition33%1/3
defined $inflate and ref $inflate ne "CODE"
!ll&&!rl&&r
4100
MC/DC0%0/2
defined $inflate and ref $inflate ne "CODE"
defined $inflate ref $inflate ne "CODE"
Truth table33%1/3
A and B
A= defined $inflateB= ref $inflate ne "CODE"
ABresult
0X0
100
111
677 if defined $inflate && ref $inflate ne 'CODE';
678 41 ▶ puke("$field: inflate requires a validation pattern")
Branch: 1 branch
BranchTrueFalseTotal
if defined $inflate and not defined $pattern 0 41 41
Condition33%1/3
defined $inflate and not defined $pattern
!ll&&!rl&&r
4100
MC/DC0%0/2
defined $inflate and not defined $pattern
defined $inflate not defined $pattern
Truth table33%1/3
A and not(B)
A= defined $inflateB= not defined $pattern
ABresult
0X0
100
111
679 if defined $inflate && !defined $pattern;
680 41 ▶ $self->{inflated_values} = [ ] if $inflate;
Branch: 1 branch
BranchTrueFalseTotal
if $inflate 0 41 41
681  
682 41 debug 1, "$self->{name}: called \$field->validate(@_) for field '$field'";
683  
684 # Check our hash to see if it's a special pattern
685 41 ▶ ($pattern) = autodata($VALIDATE{$pattern}) if $VALIDATE{$pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Field::VALIDATE{$pattern} 14 27 41
686  
687 # Hashref is a grouping per-language
688 41 ▶ if (ref $pattern eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "HASH") 2 39 41
689 2 ▶ $pattern = $pattern->{perl} || return 1;
Condition50%1/2
$pattern->{'perl'} || (return 1)
l!l
20
MC/DC0%0/1
$pattern->{'perl'} || (return 1)
$pattern->{'perl'}
Truth table50%1/2
A
A= $pattern->{'perl'}
Aresult
11
00
690 }
691  
692 # Counter for fail or success
693 41 my $bad = 0;
694  
695 # Loop thru, and if something isn't valid, we tag it
696 41 my $atleastone = 0;
697 41 ▶ $self->{invalid} ||= 0;
Condition50%1/2
$self->{"invalid"} ||= 0
l!l
041
MC/DC0%0/1
$self->{"invalid"} ||= 0
$self->{"invalid"}
Truth table50%1/2
A
A= $self->{"invalid"}
Aresult
11
00
698 41 for my $value ($self->value) {
699 60 my $thisfail = 0;
700  
701 # only continue if field is required or filled in
702 60 ▶ if ($self->required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required) { } 48 12 60
703 48 debug 1, "$field: is required per 'required' param";
704 } else {
705 12 debug 1, "$field: is optional per 'required' param";
706 12 ▶ next unless length($value) && defined($pattern);
Branch: 1 branch
BranchTrueFalseTotal
unless length $value and defined $pattern 10 2 12
Condition100%3/3
length $value and defined $pattern
!ll&&!rl&&r
462
MC/DC100%2/2
length $value and defined $pattern
length $value defined $pattern
Truth table100%3/3
A and B
A= length $valueB= defined $pattern
ABresult
0X0
100
111
707 2 debug 1, "$field: ...but is defined, so still checking";
708 }
709  
710 50 $atleastone++;
711 50 debug 1, "$field: validating ($value) against pattern '$pattern'";
712  
713 50 ▶ if ($pattern =~ m#^m(\S)(.*)\1$# || $pattern =~ m#^(/)(.*)\1$#) {
Branch: 5 branches
BranchTrueFalseTotal
if ($pattern =~ /^m(\S)(.*)\1$/ or $pattern =~ m[^(/)(.*)\1$]) { } 18 32 50
elsif (ref $pattern eq 'ARRAY') { } 8 24 32
elsif (ref $pattern eq 'CODE') { } 10 14 24
elsif ($pattern eq 'VALUE') { } 1 13 14
elsif (not defined $pattern) { } 9 4 13
Condition66%2/3
$pattern =~ /^m(\S)(.*)\1$/ or $pattern =~ m[^(/)(.*)\1$]
l!l&&r!l&&!r
01832
MC/DC50%1/2
$pattern =~ /^m(\S)(.*)\1$/ or $pattern =~ m[^(/)(.*)\1$]
$pattern =~ /^m(\S)(.*)\1$/ $pattern =~ m[^(/)(.*)\1$]
Truth table66%2/3
A or B
A= $pattern =~ /^m(\S)(.*)\1$/B= $pattern =~ m[^(/)(.*)\1$]
ABresult
1X1
011
000
714 # it be a regexp, handle / escaping
715 18 (my $tpat = $2) =~ s#\\/#/#g;
716 18 $tpat =~ s#/#\\/#g;
717 18 debug 2, "$field: does '$value' =~ /$tpat/ ?";
718 18 ▶ unless ($value =~ /$tpat/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$tpat/) 3 15 18
719 3 $thisfail = ++$bad;
720 }
721 } elsif (ref $pattern eq 'ARRAY') {
722 # must be w/i this set of values
723 8 debug 2, "$field: is '$value' in (@{$pattern}) ?";
724 8 ▶ unless (ismember($value, @{$pattern})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ismember($value, @{$pattern;})) 5 3 8
725 5 $thisfail = ++$bad;
726 }
727 } elsif (ref $pattern eq 'CODE') {
728 # eval that mofo, which gives them $form
729 10 ▶ my $extra = $form->{c} || $form;
Condition33%1/3
$form->{'c'} || $form
l!l&&r!l&&!r
0100
MC/DC0%0/2
$form->{'c'} || $form
$form->{'c'} $form
Truth table33%1/3
A || B
A= $form->{'c'}B= $form
ABresult
1X1
011
000
730 10 debug 2, "$field: does $pattern($value, $extra) ret true ?";
731 10 ▶ unless (&{$pattern}($value, $extra)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&{$pattern;}($value, $extra)) 0 10 10
732 0 $thisfail = ++$bad;
733 }
734 } elsif ($pattern eq 'VALUE') {
735 # Not null
736 1 debug 2, "$field: length '$value' > 0 ?";
737 1 ▶ unless (defined($value) && length($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and length $value) 0 1 1
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
738 0 $thisfail = ++$bad;
739 }
740 } elsif (! defined $pattern) {
741 9 debug 2, "$field: length('$value') > 0";
742 9 ▶ $thisfail = ++$bad unless length($value) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value > 0 2 7 9
743 } else {
744 # literal string is a literal comparison, but warn of typos...
745 4 ▶ belch "Validation string '$pattern' may be a typo of a builtin pattern"
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /^[A-Z]+$/ 0 4 4
746 if ($pattern =~ /^[A-Z]+$/);
747 # must reference to prevent serious problem if $value = "'; system 'rm -f /'; '"
748 4 debug 2, "$field: '$value' $pattern ? 1 : 0";
749 4 ▶ unless (eval qq(\$value $pattern ? 1 : 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "\$value $pattern ? 1 : 0") 0 4 4
750 0 $thisfail = ++$bad;
751 }
752 4 ▶ belch "Literal code eval error in validate: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
753 }
754  
755 # Just for debugging's sake
756 50 ▶ $thisfail ? debug 2, "$field: pattern FAILED"
Branch: 1 branch
BranchTrueFalseTotal
$thisfail ? : 10 40 50
757 : debug 2, "$field: pattern passed";
758
759 # run inflation subref if defined, trap errors and warn
760 50 ▶ if (defined $inflate) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inflate) 0 50 50
761 0 debug 1, "trying to inflate value '$value'";
762 0 my $inflated_value = eval { $inflate->($value) };
763 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
764 0 belch "Field $field: inflate failed on value '$value' due to '$@'";
765 0 $thisfail = ++$bad;
766 }
767 # cache for value():
768 0 push @{$self->{inflated_values}}, $inflated_value;
769  
770 # debugging:
771 0 ▶ $thisfail ? debug 2, "$field: inflate FAILED"
Branch: 1 branch
BranchTrueFalseTotal
$thisfail ? : 0 0 0
772 : debug 2, "$field: inflate passed";
773 }
774 }
775  
776 # If not $atleastone and they asked for validation, then we
777 # know that we have an error since this means no values
778 41 ▶ if ($bad || (! $atleastone && $self->required)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad or not $atleastone and $self->required) { } 6 35 41
Condition66%4/6
$bad or not $atleastone and $self->required
l!l&&r!l&&!r
6035
not $atleastone and $self->required
!ll&&!rl&&r
24110
MC/DC33%1/3
$bad or not $atleastone and $self->required
$bad not $atleastone $self->required
Truth table75%3/4
A or not(B) and C
A= $badB= not $atleastoneC= $self->required
ABCresult
1XX1
0111
00X0
0100
779 6 debug 1, "$field: validation FAILED";
780 6 ▶ $self->{invalid} = $bad || 1;
Condition50%1/2
$bad || 1
l!l
60
MC/DC0%0/1
$bad || 1
$bad
Truth table50%1/2
A
A= $bad
Aresult
11
00
781 6 $self->{missing} = $atleastone;
782 6 return;
783 } else {
784 35 debug 1, "$field: validation passed";
785 35 delete $self->{invalid}; # in case of previous run
786 35 delete $self->{missing}; # ditto
787 35 return 1;
788 }
789 }
790  
791 1551 ▶sub static () {
Subroutine static: called CC=3 SCAR=11.0
Pod undocumented
792 1551 my $self = shift;
793 1551 ▶ $self->{static} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1551 1551
794 1551 ▶ return $self->{static} if exists $self->{static};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"static"} 8 1543 1551
795 # check parent for this as well
796 1543 return $self->{_form}{static};
797 }
798  
799 383 ▶sub disabled () {
Subroutine disabled: called CC=4 SCAR=14.2
Pod undocumented
800 383 my $self = shift;
801 383 ▶ $self->{disabled} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 383 383
802 return ($self->{disabled} ? 'disabled' : undef)
803 383 ▶ if exists $self->{disabled};
Branch: 2 branches
BranchTrueFalseTotal
if exists $self->{"disabled"} 8 375 383
$self->{'disabled'} ? : 8 0 8
804 # check parent for this as well
805 375 return $self->{_form}->disabled;
806 }
807  
808 688 ▶sub javascript () {
Subroutine javascript: called CC=3 SCAR=11.4
Pod undocumented
809 688 my $self = shift;
810 688 ▶ $self->{javascript} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 688 688
811 688 ▶ return $self->{javascript} if exists $self->{javascript};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"javascript"} 0 688 688
812 # check parent for this as well
813 688 return $self->{_form}{javascript};
814 }
815  
816 1541 ▶sub growable () {
Subroutine growable: called CC=5 SCAR=18.4
Pod undocumented
817 1541 my $self = shift;
818 1541 ▶ $self->{growable} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1541 1541
819 1541 ▶ return unless $self->{growable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"growable"} 1515 26 1541
820 # check to make sure we're only a text or file type
821 26 ▶ unless ($self->type eq 'text' || $self->type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->type eq "text" or $self->type eq "file") 0 26 26
Condition33%1/3
$self->type eq "text" or $self->type eq "file"
l!l&&r!l&&!r
2600
MC/DC0%0/2
$self->type eq "text" or $self->type eq "file"
$self->type eq "text" $self->type eq "file"
Truth table33%1/3
A or B
A= $self->type eq "text"B= $self->type eq "file"
ABresult
1X1
011
000
822 0 belch "The 'growable' option only works with 'text' or 'file' fields";
823 0 return;
824 }
825 26 return $self->{growable};
826 }
827  
828 9509 ▶sub name () {
Subroutine name: called CC=3 SCAR=11.4
Pod undocumented
829 9509 my $self = shift;
830 9509 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9509 9509
831 confess "[".__PACKAGE__."::name] Fatal: Attempt to manipulate unnamed field"
832 9509 ▶ unless exists $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"name"} 0 9509 9509
833 9509 return $self->{name};
834 }
835  
836 327 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
837  
838 10351 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
839 # This allows direct addressing by name, for quicker usage
840 10351 my $self = shift;
841 10351 my($name) = $AUTOLOAD =~ /.*::(.+)/;
842  
843 10351 debug 3, "-> dispatch to \$field->{$name} = @_";
844 10351 ▶ croak "self not ref in AUTOLOAD" unless ref $self; # nta
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 10351 10351
845  
846 10351 ▶ $self->{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 10345 10351
847 10351 return $self->{$name};
848 }
849  
850 10 1;
851 __END__
« lib/CGI/FormBuilder/Field/checkbox.pm ↑ Summary lib/CGI/FormBuilder/Field/radio.pm »