lib/CGI/FormBuilder/Field.pm

filter: Statementstmt 87.8% 355 / 404 Branchbran 69.4% 186 / 268 Conditioncond 55.7% 73 / 131 MC/DCmcdc 33.3% 26 / 78 Subroutinesub 95.4% 42 / 44 Podpod 34.3% 11 / 32 totaltotal 72.4% 693 / 957 SCAR 64.4
CC
184
Cov
76%
validate
69.0
type
37.3
jsfield
25.9
CRAP 625.4
« blib/lib/CGI/FormBuilder/Field/checkbox.pm ↑ Summary blib/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.10';
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 __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 226 ▶ 'bool' => sub { $_[0]->name },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 10 ▶ 'eq' => sub { $_[0]->name eq $_[1] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
106  
107 sub new {
108 340 ▶ puke "Not enough arguments for Field->new()" unless @_ > 1;
Subroutine new: called CC=4 SCAR=14.5
Pod documented
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 sub field {
129 181 ▶ my $self = shift;
Subroutine field: called CC=5 SCAR=16.2
Pod documented
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 sub force {
143 1245 ▶ my $self = shift;
Subroutine force: called CC=3 SCAR=11.0
Pod undocumented
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 sub other {
150 2349 ▶ my $self = shift;
Subroutine other: called CC=5 SCAR=16.5
Pod undocumented
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 95 ▶ return wantarray ? %{$self->{other}} : $self->{other};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 95 95
156 }
157  
158 sub othername {
159 329 ▶ my $self = shift;
Subroutine othername: called CC=1 SCAR=0.0
Pod undocumented
160 329 return $self->{_form}->othername . '_' . $self->name;
161 }
162  
163 sub othertag {
164 7 ▶ my $self = shift;
Subroutine othertag: called CC=7 SCAR=19.5
Pod undocumented
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 sub growname {
183 2 ▶ my $self = shift;
Subroutine growname: called CC=1 SCAR=0.0
Pod undocumented
184 2 return $self->{_form}->growname . '_' . $self->name;
185 }
186  
187 sub cgi_value {
188 1222 ▶ my $self = shift;
Subroutine cgi_value: called CC=8 SCAR=21.1
Pod documented
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 sub def_value {
206 1181 ▶ my $self = shift;
Subroutine def_value: called CC=3 SCAR=11.3
Pod documented
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 sub inflate_value {
220 1248 ▶ my ($self, $v_aref) = @_;
Subroutine inflate_value: called CC=9 SCAR=22.2
Pod undocumented
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 sub value {
268 277 ▶ my $self = shift;
Subroutine value: called CC=5 SCAR=16.2
Pod documented
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 sub tag_value {
291 1041 ▶ my $self = shift;
Subroutine tag_value: called CC=7 SCAR=19.9
Pod documented
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 sub tag_name {
316 0 ▶ my $self = shift;
Subroutine tag_name: not called CC=4 SCAR=30.0
Pod documented
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 sub type {
327 1334 ▶ local $^W = 0; # -w sucks
Subroutine type: called CC=21 SCAR=36.2
Pod documented
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 sub label {
420 778 ▶ my $self = shift;
Subroutine label: called CC=3 SCAR=11.0
Pod documented
421 778 ▶ $self->{label} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 778 778
422 778 ▶ return $self->{label} if defined $self->{label}; # manually set
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"label"} 86 692 778
423 692 return toname($self->name);
424 }
425  
426 sub attr {
427 383 ▶ my $self = shift;
Subroutine attr: called CC=15 SCAR=28.0
Pod undocumented
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 sub multiple {
472 460 ▶ my $self = shift;
Subroutine multiple: called CC=4 SCAR=14.2
Pod undocumented
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 sub options {
484 760 ▶ my $self = shift;
Subroutine options: called CC=5 SCAR=16.4
Pod undocumented
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 sub message {
505 4 ▶ my $self = shift;
Subroutine message: called CC=8 SCAR=24.0
Pod undocumented
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 = sprintf(($self->{_form}{messages}->$et
513 4 ▶ || $self->{_form}{messages}->form_invalid_default), $self->label);
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 }
515 return $self->{_form}{stylesheet}
516 4 ▶ ? qq(<span class="$self->{_form}{styleclass}_message">$mess</span>)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_form'}{'stylesheet'} ? : 4 0 4
517 : $mess;
518 }
519  
520 sub jsmessage {
521 400 ▶ my $self = shift;
Subroutine jsmessage: called CC=8 SCAR=22.9
Pod undocumented
522 400 ▶ $self->{jsmessage} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 400 400
523 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
524 400 ▶ unless ($mess) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mess) 400 0 400
525 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
526 400 ▶ my $et = 'js_invalid_' . ($type eq 'text' ? 'input' : $type);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'text' ? : 275 125 400
527 400 ▶ $et = 'js_invalid_input' if $self->other; # other fields assume text
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 7 393 400
528 $mess = sprintf(($self->{_form}{messages}->$et
529 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
530 $self->label);
531 }
532 400 return $mess
533 }
534  
535 sub comment {
536 383 ▶ my $self = shift;
Subroutine comment: called CC=4 SCAR=13.9
Pod undocumented
537 383 ▶ $self->{comment} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 383 383
538 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
539 return $self->{_form}{stylesheet}
540 37 ▶ ? qq(<span class="$self->{_form}{styleclass}_comment">$mess</span>)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_form'}{'stylesheet'} ? : 12 25 37
541 : $mess;
542 }
543  
544 # simple error wrapper (why wasn't this here?)
545 sub error {
546 201 ▶ my $self = shift;
Subroutine error: called CC=2 SCAR=6.9
Pod undocumented
547 201 ▶ return $self->invalid ? $self->message : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->invalid ? : 2 199 201
548 }
549  
550 sub jstype {
551 18 ▶ my $self = shift;
Subroutine jstype: called CC=4 SCAR=14.6
Pod undocumented
552 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
553 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
554 }
555  
556 sub script {
557 3 ▶ my $self = shift;
Subroutine script: called CC=1 SCAR=0.0
Pod undocumented
558 #
559 # An unfortunate hack. Sometimes (often?) we don't know the field
560 # type until render(), in which Javascript is generated first. So,
561 # the grandfather (this) of all script() methods just sets the type
562 # by calling $self->type in a void context (which reblesses the object)
563 # and then calling $self->script again. I think this sucks, but then
564 # again this code shouldn't be called that often. Maybe.
565 #
566 3 $self->type;
567 3 $self->script;
568 }
569  
570 sub jsfield {
571 323 ▶ my $self = shift;
Subroutine jsfield: called CC=17 SCAR=32.5
Pod undocumented
572 323 my $name = $self->name;
573 323 my $pattern = $self->{validate};
574 323 debug 2, "return '' unless ".$self->javascript." && ($pattern || ".$self->required.")";
575 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
027350
$pattern or $self->required
l!l&&r!l&&!r
4136246
MC/DC33%1/3
$self->javascript and $pattern || $self->required
$self->javascript $pattern $self->required
Truth table50%2/4
A and B or C
A= $self->javascriptB= $patternC= $self->required
ABCresult
0XX0
1000
11X1
1011
576  
577 # First arg is the script that our children should've included
578 77 my($jsfunc, $close_brace, $in) = @_;
579 77 ▶ unless ($jsfunc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jsfunc) 0 77 77
580 0 belch "Missing generated \$jsfunc string for $name->jsfield";
581 0 return '';
582 }
583  
584 77 debug 1, "$name: generating JavaScript validation code";
585  
586 # Special catch, since many would assume this would work
587 77 ▶ if (ref $pattern eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "Regexp") 0 77 77
588 0 puke "To use a regex in a 'validate' option you must specify ".
589 "it in single quotes, like '/^\\w+\$/' - failed on '$name' field";
590 }
591  
592 # hashref is a grouping per-language
593 77 ▶ if (ref $pattern eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "HASH") 0 77 77
594 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
595 }
596  
597 # Check our hash to see if it's a special pattern
598 77 ▶ $pattern = $VALIDATE{$pattern} if $VALIDATE{$pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Field::VALIDATE{$pattern} 33 44 77
599  
600 # Make field name JS-safe
601 77 my $jsfield = tovar($name);
602  
603 # Note we have to use form.elements['name'] instead of just form.name
604 # as the JAPH using this module may have defined fields like "u.type"
605 77 my $alertstr = escapejs($self->jsmessage); # handle embedded '
606 77 $alertstr .= '\n';
607  
608 # Our fields are only required if the required option is set
609 # So, if not set, add a not-null check to the if below
610 77 ▶ my $notnull = $self->required
Branch: 1 branch
BranchTrueFalseTotal
$self->required ? : 71 6 77
611 ? qq[$jsfield == null ||] # must have or error
612 : qq[$jsfield != null && $jsfield != "" &&]; # only care if filled in
613  
614 77 ▶ if ($pattern =~ m#^m?(\S)(.*)\1$#) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pattern =~ /^m?(\S)(.*)\1$/) { } 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
615 # JavaScript regexp
616 39 ($pattern = $2) =~ s/\\\//\//g;
617 39 $pattern =~ s/\//\\\//g;
618 39 $jsfunc .= qq[${in}if ($notnull ! $jsfield.match(/$pattern/)) {\n];
619 }
620 elsif (ref $pattern eq 'ARRAY') {
621 # Must be w/i this set of values
622 # Can you figure out how this piece of Perl works? No, seriously, I forgot.
623 $jsfunc .= qq[${in}if ($notnull ($jsfield != ']
624 2 . join("' && $jsfield != '", @{$pattern}) . "')) {\n";
625 }
626 ▶ 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
627 # Not null (for required sub refs, just check for a value)
628 36 $jsfunc .= qq[${in}if ($notnull $jsfield === "") {\n];
629 }
630 else {
631 # Literal string is literal code to execute, but provide
632 # a warning just in case
633 0 ▶ belch "Validation string '$pattern' may be a typo of a builtin pattern"
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /^[A-Z]+$/ 0 0 0
634 if $pattern =~ /^[A-Z]+$/;
635 0 $jsfunc .= qq[${in}if ($notnull $jsfield $pattern) {\n];
636 }
637  
638 # add on our alert message, but only if it's required
639 77 $jsfunc .= <<EOJS;
640 $in alertstr += '$alertstr';
641 $in invalid++;
642 $in invalid_fields.push('$jsfield');
643 $in}$close_brace
644 EOJS
645  
646 77 return $jsfunc;
647 }
648  
649 10 *render = \&tag;
650 sub tag {
651 207 ▶ my $self = shift;
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
652 207 $self->type;
653 207 return $self->tag(@_);
654 }
655  
656 sub validate () {
657  
658 # This function does all the validation on the Perl side.
659 # It doesn't generate JavaScript; see render() for that...
660  
661 41 ▶ my $self = shift;
Subroutine validate: called CC=40 SCAR=42.3
Pod documented
662 41 my $form = $self->{_form}; # alias for examples (paint-by-numbers)
663 41 local $^W = 0; # -w sucks
664  
665 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
666 41 my $field = $self->name;
667  
668 # inflation subref?
669 41 ▶ my $inflate = (exists $self->{inflate}) ? $self->{inflate} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'inflate'} ? : 0 41 41
670 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
671 if defined $inflate && ref $inflate ne 'CODE';
672 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
673 if defined $inflate && !defined $pattern;
674 41 ▶ $self->{inflated_values} = [ ] if $inflate;
Branch: 1 branch
BranchTrueFalseTotal
if $inflate 0 41 41
675  
676 41 debug 1, "$self->{name}: called \$field->validate(@_) for field '$field'";
677  
678 # Check our hash to see if it's a special pattern
679 41 ▶ ($pattern) = autodata($VALIDATE{$pattern}) if $VALIDATE{$pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Field::VALIDATE{$pattern} 14 27 41
680  
681 # Hashref is a grouping per-language
682 41 ▶ if (ref $pattern eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "HASH") 2 39 41
683 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
684 }
685  
686 # Counter for fail or success
687 41 my $bad = 0;
688  
689 # Loop thru, and if something isn't valid, we tag it
690 41 my $atleastone = 0;
691 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
692 41 for my $value ($self->value) {
693 60 my $thisfail = 0;
694  
695 # only continue if field is required or filled in
696 60 ▶ if ($self->required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required) { } 48 12 60
697 48 debug 1, "$field: is required per 'required' param";
698 } else {
699 12 debug 1, "$field: is optional per 'required' param";
700 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
701 2 debug 1, "$field: ...but is defined, so still checking";
702 }
703  
704 50 $atleastone++;
705 50 debug 1, "$field: validating ($value) against pattern '$pattern'";
706  
707 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
708 # it be a regexp, handle / escaping
709 18 (my $tpat = $2) =~ s#\\/#/#g;
710 18 $tpat =~ s#/#\\/#g;
711 18 debug 2, "$field: does '$value' =~ /$tpat/ ?";
712 18 ▶ unless ($value =~ /$tpat/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$tpat/) 3 15 18
713 3 $thisfail = ++$bad;
714 }
715 } elsif (ref $pattern eq 'ARRAY') {
716 # must be w/i this set of values
717 8 debug 2, "$field: is '$value' in (@{$pattern}) ?";
718 8 ▶ unless (ismember($value, @{$pattern})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ismember($value, @{$pattern;})) 5 3 8
719 5 $thisfail = ++$bad;
720 }
721 } elsif (ref $pattern eq 'CODE') {
722 # eval that mofo, which gives them $form
723 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
724 10 debug 2, "$field: does $pattern($value, $extra) ret true ?";
725 10 ▶ unless (&{$pattern}($value, $extra)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&{$pattern;}($value, $extra)) 0 10 10
726 0 $thisfail = ++$bad;
727 }
728 } elsif ($pattern eq 'VALUE') {
729 # Not null
730 1 debug 2, "$field: length '$value' > 0 ?";
731 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
732 0 $thisfail = ++$bad;
733 }
734 } elsif (! defined $pattern) {
735 9 debug 2, "$field: length('$value') > 0";
736 9 ▶ $thisfail = ++$bad unless length($value) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value > 0 2 7 9
737 } else {
738 # literal string is a literal comparison, but warn of typos...
739 4 ▶ belch "Validation string '$pattern' may be a typo of a builtin pattern"
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /^[A-Z]+$/ 0 4 4
740 if ($pattern =~ /^[A-Z]+$/);
741 # must reference to prevent serious problem if $value = "'; system 'rm -f /'; '"
742 4 debug 2, "$field: '$value' $pattern ? 1 : 0";
743 4 ▶ unless (eval qq(\$value $pattern ? 1 : 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "\$value $pattern ? 1 : 0") 0 4 4
744 0 $thisfail = ++$bad;
745 }
746 4 ▶ belch "Literal code eval error in validate: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
747 }
748  
749 # Just for debugging's sake
750 50 ▶ $thisfail ? debug 2, "$field: pattern FAILED"
Branch: 1 branch
BranchTrueFalseTotal
$thisfail ? : 10 40 50
751 : debug 2, "$field: pattern passed";
752
753 # run inflation subref if defined, trap errors and warn
754 50 ▶ if (defined $inflate) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inflate) 0 50 50
755 0 debug 1, "trying to inflate value '$value'";
756 0 my $inflated_value = eval { $inflate->($value) };
757 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
758 0 belch "Field $field: inflate failed on value '$value' due to '$@'";
759 0 $thisfail = ++$bad;
760 }
761 # cache for value():
762 0 push @{$self->{inflated_values}}, $inflated_value;
763  
764 # debugging:
765 0 ▶ $thisfail ? debug 2, "$field: inflate FAILED"
Branch: 1 branch
BranchTrueFalseTotal
$thisfail ? : 0 0 0
766 : debug 2, "$field: inflate passed";
767 }
768 }
769  
770 # If not $atleastone and they asked for validation, then we
771 # know that we have an error since this means no values
772 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
773 6 debug 1, "$field: validation FAILED";
774 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
775 6 $self->{missing} = $atleastone;
776 6 return;
777 } else {
778 35 debug 1, "$field: validation passed";
779 35 delete $self->{invalid}; # in case of previous run
780 35 delete $self->{missing}; # ditto
781 35 return 1;
782 }
783 }
784  
785 sub static () {
786 1551 ▶ my $self = shift;
Subroutine static: called CC=3 SCAR=11.0
Pod undocumented
787 1551 ▶ $self->{static} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1551 1551
788 1551 ▶ return $self->{static} if exists $self->{static};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"static"} 8 1543 1551
789 # check parent for this as well
790 1543 return $self->{_form}{static};
791 }
792  
793 sub disabled () {
794 383 ▶ my $self = shift;
Subroutine disabled: called CC=4 SCAR=14.2
Pod undocumented
795 383 ▶ $self->{disabled} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 383 383
796 return ($self->{disabled} ? 'disabled' : undef)
797 383 ▶ if exists $self->{disabled};
Branch: 2 branches
BranchTrueFalseTotal
if exists $self->{"disabled"} 8 375 383
$self->{'disabled'} ? : 8 0 8
798 # check parent for this as well
799 375 return $self->{_form}->disabled;
800 }
801  
802 sub javascript () {
803 688 ▶ my $self = shift;
Subroutine javascript: called CC=3 SCAR=11.4
Pod undocumented
804 688 ▶ $self->{javascript} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 688 688
805 688 ▶ return $self->{javascript} if exists $self->{javascript};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"javascript"} 0 688 688
806 # check parent for this as well
807 688 return $self->{_form}{javascript};
808 }
809  
810 sub growable () {
811 1541 ▶ my $self = shift;
Subroutine growable: called CC=5 SCAR=18.4
Pod undocumented
812 1541 ▶ $self->{growable} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1541 1541
813 1541 ▶ return unless $self->{growable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"growable"} 1515 26 1541
814 # check to make sure we're only a text or file type
815 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
816 0 belch "The 'growable' option only works with 'text' or 'file' fields";
817 0 return;
818 }
819 26 return $self->{growable};
820 }
821  
822 sub name () {
823 9629 ▶ my $self = shift;
Subroutine name: called CC=3 SCAR=11.4
Pod undocumented
824 9629 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9629 9629
825 confess "[".__PACKAGE__."::name] Fatal: Attempt to manipulate unnamed field"
826 9629 ▶ unless exists $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"name"} 0 9629 9629
827 9629 return $self->{name};
828 }
829  
830 327 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
831  
832 sub AUTOLOAD {
833 # This allows direct addressing by name, for quicker usage
834 10351 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
835 10351 my($name) = $AUTOLOAD =~ /.*::(.+)/;
836  
837 10351 debug 3, "-> dispatch to \$field->{$name} = @_";
838 10351 ▶ croak "self not ref in AUTOLOAD" unless ref $self; # nta
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 10351 10351
839  
840 10351 ▶ $self->{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 10345 10351
841 10351 return $self->{$name};
842 }
843  
844 10 1;
845 __END__
« blib/lib/CGI/FormBuilder/Field/checkbox.pm ↑ Summary blib/lib/CGI/FormBuilder/Field/radio.pm »