FormBuilder/Field.pm

filter: Statementstmt 91.4% 117 / 128 Branchbran 79.6% 51 / 64 Conditioncond 75.0% 15 / 20 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 85.3% 204 / 239 CC 43 SCAR 27.9
CC
43
Cov
86%
build
23.8
_build_input
19.0
BUILDARGS
4.0
CRAP 16.3
↑ Summary FormBuilder/Validation.pm »
1 package HTML::FormBuilder::Field;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 extends qw(HTML::FormBuilder::Base);
13  
14 11 our $VERSION = '0.12'; ## VERSION
15  
16 has data => (
17 is => 'ro',
18 isa => sub {
19 97 ▶ my $data = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
20 97 ▶ croak('data should be a hashref') unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 97 97
21 },
22 default => sub {
23 0 ▶ {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 },
25 11 );
26  
27 sub BUILDARGS {
28 97 ▶ my (undef, @args) = @_;
Subroutine BUILDARGS: called CC=4 SCAR=14.0
Pod documented
29 97 ▶ my %args = (@args % 2) ? %{$args[0]} : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args % 2 ? : 0 97 97
30  
31 97 my $data = $args{data};
32  
33 # normalize: if 'input' is not an array, then make it as an array, so that
34 # we can process the array directly
35 97 ▶ if ($data->{input} && ref($data->{input}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"input"} and ref $data->{"input"} ne "ARRAY") 61 36 97
Condition100%3/3
$data->{"input"} and ref $data->{"input"} ne "ARRAY"
!ll&&!rl&&r
132361
MC/DC100%2/2
$data->{"input"} and ref $data->{"input"} ne "ARRAY"
$data->{"input"} ref $data->{"input"} ne "ARRAY"
Truth table100%3/3
A and B
A= $data->{"input"}B= ref $data->{"input"} ne "ARRAY"
ABresult
0X0
100
111
36 61 $data->{input} = [$data->{input}];
37 }
38  
39 97 return \%args;
40 }
41  
42 sub build {
43 71 ▶ my $self = shift;
Subroutine build: called CC=22 SCAR=31.7
Pod documented
44 71 my $env = shift;
45  
46 71 my $data = $self->{data};
47  
48 71 my $stacked = $env->{stacked};
49 71 my $classes = $self->classes;
50  
51 71 my $div_span = "div";
52 71 my $label_column = $classes->{label_column};
53 71 my $input_column = $classes->{input_column};
54 71 ▶ $input_column = $data->{override_input_class} if ($data->{override_input_class});
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"override_input_class"} 0 71 71
55  
56 71 ▶ if ($stacked == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stacked == 0) 0 71 71
57 0 $div_span = "span";
58 0 $label_column = "";
59 0 $input_column = "";
60 }
61 71 my $input_fields_html = '';
62  
63 71 my $stacked_attr = {};
64  
65 71 ▶ if ($stacked == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stacked == 1) 71 0 71
66 71 ▶ my $class = $data->{'class'} ? " $data->{class}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$data->{'class'} ? : 0 71 71
67  
68 71 ▶ if ($data->{'type'} and $data->{'type'} eq 'hidden') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'type'} and $data->{'type'} eq 'hidden') { } 0 71 71
Condition33%1/3
$data->{"type"} and $data->{"type"} eq "hidden"
!ll&&!rl&&r
7100
MC/DC0%0/2
$data->{"type"} and $data->{"type"} eq "hidden"
$data->{"type"} $data->{"type"} eq "hidden"
Truth table33%1/3
A and B
A= $data->{"type"}B= $data->{"type"} eq "hidden"
ABresult
0X0
100
111
69 0 $stacked_attr->{class} = $class;
70 } else {
71 71 $stacked_attr->{class} = "$classes->{row_padding} $classes->{row} clear$class";
72 }
73 }
74  
75 #create the field label
76 71 ▶ if (defined $data->{'label'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"label"}) 56 15 71
77 56 ▶ my $label_text = $data->{'label'}->{'text'} || '';
Condition100%2/2
$data->{'label'}{'text'} || ''
l!l
551
MC/DC100%1/1
$data->{'label'}{'text'} || ''
$data->{'label'}{'text'}
Truth table100%2/2
A
A= $data->{'label'}{'text'}
Aresult
11
00
78 56 undef $data->{'label'}->{'text'};
79 56 ▶ my $required_mark = delete $data->{label}{required_mark} || 0;
Condition100%2/2
delete $data->{'label'}{'required_mark'} || 0
l!l
254
MC/DC100%1/1
delete $data->{'label'}{'required_mark'} || 0
delete $data->{'label'}{'required_mark'}
Truth table100%2/2
A
A= delete $data->{'label'}{'required_mark'}
Aresult
11
00
80 56 my $label_html = $self->_build_element_and_attributes('label', $data->{'label'}, $label_text, {required_mark => $required_mark},);
81  
82 # add a tooltip explanation if given
83 56 ▶ if ($data->{'label'}{'tooltip'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'label'}{'tooltip'}) { } 1 55 56
84  
85 # img_url is the url of question mark picture
86 1 my $tooltip = _tooltip($data->{'label'}{'tooltip'}{'desc'}, $data->{'label'}{tooltip}{img_url});
87  
88 1 $input_fields_html .= qq{<div class="$classes->{extra_tooltip_container}">$label_html$tooltip</div>};
89 } else {
90 55 ▶ my $hide_mobile = $label_text ? "" : $classes->{hide_mobile};
Branch: 1 branch
BranchTrueFalseTotal
$label_text ? : 54 1 55
91  
92 55 $input_fields_html .= qq{<$div_span class="$label_column $hide_mobile form_label">$label_html</$div_span>};
93 }
94 }
95  
96 # create the input field
97 71 ▶ if (defined $data->{'input'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"input"}) 61 10 71
98  
99 #if there are more than 1 input field in a single row then we generate 1 by 1
100 61 my $inputs = $data->{input};
101 61 $input_fields_html .= qq{<$div_span class="$input_column">};
102 61 foreach my $input (@{$inputs}) {
103 76 $input_fields_html .= $self->_build_input($input, $env);
104 }
105 }
106  
107 71 ▶ if (defined $data->{'comment'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"comment"}) 1 70 71
108 1 ▶ $data->{'comment'}->{'class'} ||= '';
Condition50%1/2
$data->{"comment"}{"class"} ||= ""
l!l
01
MC/DC0%0/1
$data->{"comment"}{"class"} ||= ""
$data->{"comment"}{"class"}
Truth table50%1/2
A
A= $data->{"comment"}{"class"}
Aresult
11
00
109  
110 1 ▶ unless ($data->{comment}->{no_new_line}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"comment"}{"no_new_line"}) 1 0 1
111 1 $input_fields_html .= '<br>';
112 }
113 1 $input_fields_html .= $self->_build_element_and_attributes('p', $data->{'comment'}, $data->{'comment'}->{'text'});
114 }
115  
116 71 ▶ if (defined $data->{'error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"error"}) 48 23 71
117  
118 my @errors =
119 ref($data->{'error'}) eq 'ARRAY'
120 1 ? @{$data->{error}}
121 48 ▶ : $data->{error};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'error'} eq 'ARRAY' ? : 1 47 48
122  
123 48 foreach my $error_box (@errors) {
124 48 $input_fields_html .= $self->_build_element_and_attributes('p', $error_box, $error_box->{text});
125 }
126  
127 }
128  
129 #close the input tag
130 71 ▶ if (defined $data->{'input'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"input"}) 61 10 71
131 61 $input_fields_html .= '</' . $div_span . '>';
132 }
133  
134 71 ▶ if ($stacked == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stacked == 1) 71 0 71
135 71 $input_fields_html = $self->_build_element_and_attributes('div', $stacked_attr, $input_fields_html);
136 }
137  
138 71 return $input_fields_html;
139 }
140  
141 #####################################################################
142 # Usage : build the input field its own attributes
143 # Purpose : perform checking build the input field according to its own
144 # characteristics
145 # Returns : input field with its attributes in string
146 # Parameters : $input_field in HASH ref for example
147 # $attributes = {'id' => 'test', 'name' => 'test', 'class' => 'myclass'}
148 # Comments : check pod below to understand how to create different input fields
149 # See Also :
150 #####################################################################
151 sub _build_input {
152 76 ▶ my $self = shift;
Subroutine _build_input: called CC=18 SCAR=29.5
153 76 my $input_field = shift;
154  
155 76 my $html = '';
156  
157 # delete this so that it doesn't carry on to the next field
158 # I don't know why should delete it(undef it)
159 76 my $heading = delete $input_field->{'heading'};
160 76 my $trailing = delete $input_field->{'trailing'};
161  
162 # wrap <input>, heading & trailing <span> in <div>
163 76 my ($wrap_input, $wrap_heading, $wrap_trailing);
164 76 ▶ if ($input_field->{wrap_in_div_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_field->{"wrap_in_div_class"}) 0 76 76
165 0 ($wrap_input, $wrap_heading, $wrap_trailing) = @{$input_field->{wrap_in_div_class}}{'input', 'heading', 'trailing'};
166 }
167  
168 #create the filed input
169 76 ▶ if (eval { $input_field->can('widget_html') }) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { $input_field->can('widget_html') } }) { } 15 61 76
elsif ($input_field->{'type'} and $input_field->{'type'} eq 'textarea') { } 7 54 61
elsif ($input_field->{'type'}) { } 53 1 54
170 15 $html = $input_field->widget_html;
171 ▶ } elsif ($input_field->{'type'} and $input_field->{'type'} eq 'textarea') {
Condition100%3/3
$input_field->{"type"} and $input_field->{"type"} eq "textarea"
!ll&&!rl&&r
1537
MC/DC100%2/2
$input_field->{"type"} and $input_field->{"type"} eq "textarea"
$input_field->{"type"} $input_field->{"type"} eq "textarea"
Truth table100%3/3
A and B
A= $input_field->{"type"}B= $input_field->{"type"} eq "textarea"
ABresult
0X0
100
111
172 7 undef $input_field->{'type'};
173 7 ▶ my $textarea_value = $input_field->{'value'} || '';
Condition50%1/2
$input_field->{'value'} || ''
l!l
70
MC/DC0%0/1
$input_field->{'value'} || ''
$input_field->{'value'}
Truth table50%1/2
A
A= $input_field->{'value'}
Aresult
11
00
174 7 undef $input_field->{'value'};
175 7 $html = $self->_build_element_and_attributes('textarea', $input_field, $textarea_value);
176 } elsif ($input_field->{'type'}) {
177 53 my $type = $input_field->{'type'};
178 53 ▶ if ($type =~ /^(?:text|password)$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /^(?:text|password)$/i) { } 28 25 53
elsif ($type =~ /button|submit/) { } 1 24 25
179 28 $input_field->{'class'} .= ' text';
180 } elsif ($type =~ /button|submit/) {
181 1 $input_field->{'class'} .= ' button';
182 }
183  
184 53 ▶ my $tag = ($type =~ /button|submit/ ? 'button' : 'input');
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /button|submit/ ? : 1 52 53
185  
186 53 $html = $self->_build_element_and_attributes($tag, $input_field);
187  
188 53 ▶ if ($type =~ /button|submit/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /button|submit/) 1 52 53
189 1 $html = qq{<span class="$input_field->{class}">$html</span>};
190 }
191 }
192 76 ▶ if ($wrap_input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap_input) 0 76 76
193 0 $html = qq{<div class="$wrap_input">$html</div>};
194 }
195  
196 76 ▶ if ($heading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heading) 3 73 76
197 3 my $heading_html = qq{<span id="inputheading">$heading</span>};
198 3 ▶ if ($wrap_heading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap_heading) 0 3 3
199 0 $heading_html = qq{<div class="$wrap_heading">$heading_html</div>};
200 }
201  
202 3 ▶ if ($input_field->{'type'}
Branch: 1 branch
BranchTrueFalseTotal
if ($input_field->{'type'} and $input_field->{'type'} =~ /radio|checkbox/i) { } 1 2 3
Condition66%2/3
$input_field->{"type"} and $input_field->{"type"} =~ /radio|checkbox/i
!ll&&!rl&&r
021
MC/DC50%1/2
$input_field->{"type"} and $input_field->{"type"} =~ /radio|checkbox/i
$input_field->{"type"} $input_field->{"type"} =~ /radio|checkbox/i
Truth table66%2/3
A and B
A= $input_field->{"type"}B= $input_field->{"type"} =~ /radio|checkbox/i
ABresult
0X0
100
111
203 && ($input_field->{'type'} =~ /radio|checkbox/i))
204 {
205 1 $html .= qq{$heading_html<br />};
206 } else {
207 2 $html = $heading_html . $html;
208 }
209 }
210  
211 76 ▶ if ($trailing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailing) 1 75 76
212 1 my $trailing_html = qq{<span class="$self->{classes}{inputtrailing}">$trailing</span>};
213 1 ▶ if ($wrap_trailing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap_trailing) 0 1 1
214 0 $trailing_html = qq{<div class="$wrap_trailing">$trailing_html</div>};
215 }
216 1 $html .= $trailing_html;
217 }
218  
219 76 return $html;
220 }
221  
222 #####################################################################
223 # Usage : _tooltip($content, $url)
224 # Purpose : create tooltip html code
225 # Returns : HTML
226 # Comments :
227 # See Also :
228 #####################################################################
229 sub _tooltip {
230 1 ▶ my $content = shift;
Subroutine _tooltip: called CC=1 SCAR=0.0
231 1 my $url = shift;
232 1 $content =~ s/\'/&apos;/g; # Escape for quoting below
233  
234 1 return qq{ <a href='#' title='$content' rel='tooltip'><img src="$url" /></a>};
235 }
236  
237 11 1;
238  
239 =head1 NAME
240  
241 HTML::FormBuilder::Field - Field container used by HTML::FormBuilder
242  
243 =head1 SYNOPSIS
244  
245 my $form = HTML::FormBuilder->new(data => {id => 'testform});
246  
247 my $fieldset = $form->add_fieldset({id => 'fieldset1'});
248  
249 $fieldset->add_field({input => {type => 'text', value => 'Join'}});
250  
251 # Text only, without input fields
252 $fieldset->add_field({
253 comment => {
254 text => 'Please check on checkbox below:',
255 class => 'grd-grid-12',
256 # no extra <br/> before <span> for comment
257 no_new_line => 1,
258 },
259 });
260  
261 # checkbox & explanation text
262 $fieldset->add_field({
263 input => {
264 id => 'tnc',
265 name => 'tnc',
266 type => 'checkbox',
267 trailing => 'I have read & agree to all terms & condition.',
268 # wrap <input> & trailing <span> respectively in <div>, with class:
269 wrap_in_div_class => {
270 input => 'grd-grid-1',
271 trailing => 'grd-grid-11'
272 },
273 },
274 error => {
275 id => 'error_tnc',
276 class => 'errorfield',
277 },
278 validation => [{
279 type => 'checkbox_checked',
280 err_msg => localize('Please agree in order to proceed.'),
281 }],
282 # override div container class for input
283 override_input_class => 'grd-grid-12',
284 });
285  
286 $form->add_field($fieldset_index, {input => {type => 'text', value => 'Join'}});
287  
288 =head1 METHODS
289  
290 =head2 BUILDARGS
291  
292 =head2 build
293  
294 =head2 data
295  
296 =head1 AUTHOR
297  
298 Chylli L<mailto:chylli@binary.com>
299  
300 =head1 CONTRIBUTOR
301  
302 Fayland Lam L<mailto:fayland@binary.com>
303  
304 Tee Shuwn Yuan L<mailto:shuwnyuan@binary.com>
305  
306 =head1 COPYRIGHT AND LICENSE
307  
308 =cut
309  
↑ Summary FormBuilder/Validation.pm »