FormBuilder.pm

filter: Statementstmt 94.1% 192 / 204 Branchbran 76.7% 66 / 86 Conditioncond 64.8% 24 / 37 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 92.5% 25 / 27 Podpod 33.3% 3 / 9 totaltotal 82.8% 318 / 384 SCAR 44.3
CC
71
Cov
86%
get_field_value
13.6
set_field_value
11.4
build_confirmation_button_with_all_inputs_hidden
10.5
CRAP 84.1
↑ Summary FormBuilder/Validation.pm »
1 package HTML::FormBuilder;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 our $VERSION = '0.11';
7  
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use HTML::FormBuilder::FieldSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use String::Random ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 extends qw(HTML::FormBuilder::Base);
15  
16 has data => (
17 is => 'ro',
18 isa => sub {
19 27 ▶ my $data = shift;
Subroutine __ANON__: called CC=3 SCAR=11.7
20 27 ▶ croak('data should be a hashref') unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 27 27
21 27 ▶ croak("Form must be given an id when instantiating a HTML::FormBuilder->new object in $0.") if !defined $data->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data->{"id"} 0 27 27
22 },
23 default => sub {
24 0 ▶ {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 },
26 7 );
27  
28 has fieldsets => (
29 is => 'rw',
30 isa => sub {
31 27 ▶ my $fieldsets = shift;
Subroutine __ANON__: called CC=3 SCAR=11.7
32 27 ▶ return 1 unless $fieldsets;
Branch: 1 branch
BranchTrueFalseTotal
unless $fieldsets 0 27 27
33 27 ▶ croak('fieldsets should be an arrayref')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fieldsets eq "ARRAY" 0 27 27
34 unless ref($fieldsets) eq 'ARRAY';
35 },
36 default => sub {
37 27 ▶ [];
Subroutine __ANON__: called CC=1 SCAR=0.0
38 },
39 7 );
40  
41 has after_form => (
42 is => 'rw',
43 isa => sub {
44 1 ▶ return !ref($_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
45 7 });
46  
47 7 has csrftoken => (is => 'ro');
48  
49 sub BUILDARGS {
50 27 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=7 SCAR=19.5
Pod undocumented
51 27 ▶ my %args = (@args % 2) ? %{$args[0]} : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args % 2 ? : 0 27 27
52  
53 # set default class
54 27 ▶ if ($args{classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'classes'}) { } 16 11 27
55 16 $args{classes} = {%{$HTML::FormBuilder::Base::CLASSES}, %{$args{classes}}};
56 } else {
57 11 $args{classes} = {%{$HTML::FormBuilder::Base::CLASSES}};
58 }
59  
60 27 ▶ if (($args{csrftoken} // '') eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (($args{"csrftoken"} // "") eq 1) 2 25 27
Condition100%2/2
$args{"csrftoken"} // ""
l!l
324
MC/DC100%1/1
$args{"csrftoken"} // ""
$args{"csrftoken"}
Truth table100%2/2
A
A= $args{"csrftoken"}
Aresult
11
00
61 2 $args{csrftoken} = String::Random::random_regex('[a-zA-Z0-9]{16}');
62 }
63  
64 27 ▶ $args{data}{method} ||= 'get';
Condition100%2/2
$args{"data"}{"method"} ||= "get"
l!l
720
MC/DC100%1/1
$args{"data"}{"method"} ||= "get"
$args{"data"}{"method"}
Truth table100%2/2
A
A= $args{"data"}{"method"}
Aresult
11
00
65 27 ▶ $args{data}{method} = 'get' if $args{data}{method} ne 'post';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"data"}{"method"} ne "post" 20 7 27
66 27 return \%args;
67 }
68  
69 #####################################################################
70 # Usage : Add a new fieldset to the form
71 # Purpose : Allow the form object to carry more than 1 fieldsets
72 # Returns : Fieldset object
73 # Parameters : Hash reference with keys in <fieldsets> supported attributes
74 # Comments : Fieldset works like a table, which allow one form to
75 # have more than 1 fieldset. Each Fieldset has its own
76 # input fields.
77 # See Also :
78 #####################################################################
79 sub add_fieldset {
80 26 ▶ my $self = shift;
Subroutine add_fieldset: called CC=2 SCAR=7.0
Pod documented
81 26 my $_args = shift;
82  
83 #check if the $args is a ref HASH
84 26 ▶ croak("Parameters must in HASH reference in $0.")
Branch: 1 branch
BranchTrueFalseTotal
if ref $_args ne "HASH" 0 26 26
85 if (ref $_args ne 'HASH');
86  
87 26 my $fieldset = HTML::FormBuilder::FieldSet->new(
88 data => $_args,
89 classes => $self->classes,
90 localize => $self->localize
91 );
92  
93 26 push @{$self->{fieldsets}}, $fieldset;
94  
95 26 return $fieldset;
96 }
97  
98 #####################################################################
99 # Usage : Add a new input fields to the fieldset
100 # Purpose : Check is the fieldset is created, and if is created
101 # add the input field into the fieldset
102 # Returns :
103 # Parameters : Hash reference with keys
104 # 'label' => $ref_hash
105 # 'input' => $ref_hash
106 # 'error' => $ref_hash
107 # 'comment' => $ref_hash
108 # Comments : check pod below to understand how to create different input fields
109 # See Also :
110 #####################################################################
111 sub add_field {
112 2 ▶ my $self = shift;
Subroutine add_field: called CC=3 SCAR=11.4
Pod documented
113 2 my $fieldset_index = shift;
114 2 my $_args = shift;
115  
116 #check if the fieldset_index is number
117 2 ▶ croak("The fieldset_index should be a number")
Branch: 1 branch
BranchTrueFalseTotal
unless $fieldset_index =~ /^\d+$/ 1 1 2
118 unless ($fieldset_index =~ /^\d+$/);
119  
120 #check if the fieldset array is already created
121 croak("The fieldset does not exist in $0. form_id[$self->{data}{'id'}]")
122 1 ▶ if ($fieldset_index > $#{$self->{fieldsets}});
Branch: 1 branch
BranchTrueFalseTotal
if $fieldset_index > $#{$$self{"fieldsets"};} 1 0 1
123  
124 0 my $fieldset = $self->{fieldsets}[$fieldset_index];
125 0 return $fieldset->add_field($_args);
126  
127 }
128  
129 #####################################################################
130 # Usage : generate the form
131 # Purpose : check and parse the parameters and generate the form
132 # properly
133 # Returns : form HTML
134 # Parameters : Fieldset index that would like to print, null to print all
135 # Comments :
136 # See Also :
137 # TODO : seems the parameter fieldset index useless.
138 #####################################################################
139 sub build {
140 16 ▶ my $self = shift;
Subroutine build: called CC=9 SCAR=22.0
Pod documented
141 16 my $print_fieldset_index = shift;
142  
143 # build the fieldset, if $print_fieldset_index is specifed then
144 # we only generate that praticular fieldset with that index
145 16 my @fieldsets;
146 16 ▶ if (defined $print_fieldset_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $print_fieldset_index) { } 0 16 16
147 0 push @fieldsets, $self->{'fieldsets'}[$print_fieldset_index];
148 } else {
149 16 @fieldsets = @{$self->{'fieldsets'}};
150 }
151  
152 16 my %grouped_fieldset;
153  
154 # build the form fieldset
155 16 foreach my $fieldset (@fieldsets) {
156 18 my ($fieldset_group, $fieldset_html) = $fieldset->build();
157 18 push @{$grouped_fieldset{$fieldset_group}}, $fieldset_html;
158 }
159  
160 16 my $fieldsets_html = '';
161 16 foreach my $fieldset_group (sort keys %grouped_fieldset) {
162 14 ▶ if ($fieldset_group ne 'no-group') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldset_group ne "no-group") 1 13 14
163 1 $fieldsets_html .= qq[<div id="$fieldset_group" class="$self->{classes}{fieldset_group}">];
164 }
165  
166 14 foreach my $fieldset_html (@{$grouped_fieldset{$fieldset_group}}) {
167 18 $fieldsets_html .= $fieldset_html;
168 }
169  
170 14 ▶ if ($fieldset_group ne 'no-group') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldset_group ne "no-group") 1 13 14
171 1 $fieldsets_html .= '</div>';
172 }
173 }
174  
175 16 ▶ if ($self->csrftoken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->csrftoken) 2 14 16
176 2 $fieldsets_html .= sprintf qq(<input type="hidden" name="csrftoken" value="%s"/>), $self->csrftoken;
177 }
178  
179 16 my $html = $self->_build_element_and_attributes('form', $self->{data}, $fieldsets_html);
180  
181 16 ▶ if ($self->after_form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->after_form) 1 15 16
182 1 $html .= $self->after_form;
183 }
184  
185 16 return $html;
186 }
187  
188 #
189 # This builds a bare-bone version of the form with all inputs hidden and only
190 # displays a confirmation button. It can be used for when we'd like to ask
191 # the Client to confirm what has been entered before processing.
192 #
193 # This output currently only outputs text or hidden fields, and ignores the
194 # rest. Extra functionality would need to be added to handle any type of form.
195 #
196 sub build_confirmation_button_with_all_inputs_hidden {
197 3 ▶ my $self = shift;
Subroutine build_confirmation_button_with_all_inputs_hidden: called CC=10 SCAR=23.5
Pod undocumented
198 3 my @inputs;
199  
200 # get all inputs that are to be output as hidden
201 3 foreach my $fieldset (@{$self->{'fieldsets'}}) {
202 INPUT:
203 2 foreach my $input_field (@{$fieldset->{'fields'}}) {
204 2 my $data = $input_field->{data};
205 2 ▶ next INPUT if (not defined $data->{'input'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"input"} 0 2 2
206  
207 2 push @inputs, @{$data->{'input'}};
208  
209 }
210 }
211  
212 3 my $html = '';
213  
214 3 foreach my $input (@inputs) {
215 2 ▶ next if ($input->{'type'} and $input->{'type'} eq 'submit');
Branch: 1 branch
BranchTrueFalseTotal
if $input->{"type"} and $input->{"type"} eq "submit" 0 2 2
Condition33%1/3
$input->{"type"} and $input->{"type"} eq "submit"
!ll&&!rl&&r
020
MC/DC0%0/2
$input->{"type"} and $input->{"type"} eq "submit"
$input->{"type"} $input->{"type"} eq "submit"
Truth table33%1/3
A and B
A= $input->{"type"}B= $input->{"type"} eq "submit"
ABresult
0X0
100
111
216 2 ▶ my $n = $input->{'name'} || '';
Condition50%1/2
$input->{'name'} || ''
l!l
20
MC/DC0%0/1
$input->{'name'} || ''
$input->{'name'}
Truth table50%1/2
A
A= $input->{'name'}
Aresult
11
00
217 2 ▶ my $val = $self->get_field_value($input->{'id'}) || '';
Condition50%1/2
$self->get_field_value($input->{'id'}) || ''
l!l
02
MC/DC0%0/1
$self->get_field_value($input->{'id'}) || ''
$self->get_field_value($input->{'id'})
Truth table50%1/2
A
A= $self->get_field_value($input->{'id'})
Aresult
11
00
218 2 $html .= qq{<input type="hidden" name="$n" value="$val"/>};
219 }
220  
221 3 ▶ if ($self->csrftoken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->csrftoken) 1 2 3
222 1 $html .= sprintf qq(<input type="hidden" name="csrftoken" value="%s"/>), $self->csrftoken;
223 }
224  
225 3 $html .= '<input type="hidden" name="process" value="1"/>';
226 $html .= _link_button({
227 value => $self->_localize('Back'),
228 class => $self->{classes}{backbutton},
229 3 href => 'javascript:history.go(-1)',
230 });
231 3 $html .= ' <span class="button"><button id="submit" class="button" type="submit">' . $self->_localize('Confirm') . '</button></span>';
232 3 $html = $self->_build_element_and_attributes('form', $self->{data}, $html);
233  
234 3 return $html;
235 }
236  
237 ################################################################################
238 # Usage : $form_obj->set_field_value('input_element_id', 'foo');
239 # Purpose : Set input value based on input element id
240 # Returns : none
241 # Parameters : $field_id: Input element ID
242 # $field_value: Value (text)
243 # Comments : Public
244 # See Also : get_field_value
245 ################################################################################
246 sub set_field_value {
247 62 ▶ my $self = shift;
Subroutine set_field_value: called CC=11 SCAR=24.4
Pod undocumented
248 62 my $field_id = shift;
249 62 my $field_value = shift;
250 62 ▶ return unless $field_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_id 0 62 62
251  
252 62 my $input_field = $self->_get_input_field($field_id);
253 62 ▶ return unless $input_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_field 2 60 62
254  
255 60 my $data = $input_field->{data};
256 60 my $inputs = $data->{input};
257  
258 map {
259 86 ▶ if ($_->{'id'} and $_->{'id'} eq $field_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"id"} and $_->{"id"} eq $field_id) 60 26 86
Condition66%2/3
$_->{"id"} and $_->{"id"} eq $field_id
!ll&&!rl&&r
02660
MC/DC50%1/2
$_->{"id"} and $_->{"id"} eq $field_id
$_->{"id"} $_->{"id"} eq $field_id
Truth table66%2/3
A and B
A= $_->{"id"}B= $_->{"id"} eq $field_id
ABresult
0X0
100
111
260 60 ▶ if (eval { $_->can('value') }) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { $_->can('value') } }) { } 13 47 60
elsif ($_->{'type'} =~ /(?:text|textarea|password|hidden|file)/i) { } 36 11 47
elsif ($_->{'type'} eq 'checkbox') { } 11 0 11
261 # for select box
262 13 $_->value($field_value);
263 } elsif ($_->{'type'} =~ /(?:text|textarea|password|hidden|file)/i) {
264 36 ▶ $_->{'value'} = HTML::Entities::encode_entities($field_value // '');
Condition50%1/2
$field_value // ""
l!l
360
MC/DC0%0/1
$field_value // ""
$field_value
Truth table50%1/2
A
A= $field_value
Aresult
11
00
265 } elsif ($_->{'type'} eq 'checkbox') {
266 # if value not set during $fieldset->add_field(), default to browser default value for checkbox: 'on'
267 11 ▶ my $checkbox_value = $_->{'value'} // 'on';
Condition50%1/2
$_->{"value"} // "on"
l!l
110
MC/DC0%0/1
$_->{"value"} // "on"
$_->{"value"}
Truth table50%1/2
A
A= $_->{"value"}
Aresult
11
00
268 11 ▶ $_->{'checked'} = 'checked' if ($field_value eq $checkbox_value);
Branch: 1 branch
BranchTrueFalseTotal
if $field_value eq $checkbox_value 11 0 11
269 }
270 }
271 60 } @{$inputs};
272 60 return;
273 }
274  
275 ################################################################################
276 # Usage : $form_obj->get_field_value('input_element_id');
277 # Purpose : Get input value based on input element id
278 # Returns : text (Input value) / undef
279 # Parameters : $field_id: Input element ID
280 # Comments : Public
281 # See Also : set_field_value
282 ################################################################################
283 sub get_field_value {
284 88 ▶ my $self = shift;
Subroutine get_field_value: called CC=13 SCAR=26.1
Pod undocumented
285 88 my $field_id = shift;
286  
287 88 my $input_field = $self->_get_input_field($field_id);
288 88 ▶ return unless $input_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_field 2 86 88
289  
290 86 my $inputs = $input_field->{data}{input};
291 86 foreach my $input (@$inputs) {
292 103 ▶ if ($input->{'id'} and $input->{'id'} eq $field_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"id"} and $input->{"id"} eq $field_id) 86 17 103
Condition66%2/3
$input->{"id"} and $input->{"id"} eq $field_id
!ll&&!rl&&r
01786
MC/DC50%1/2
$input->{"id"} and $input->{"id"} eq $field_id
$input->{"id"} $input->{"id"} eq $field_id
Truth table66%2/3
A and B
A= $input->{"id"}B= $input->{"id"} eq $field_id
ABresult
0X0
100
111
293 86 ▶ if (eval { $input->can('value') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $input->can("value") } }) 13 73 86
294 13 return $input->value;
295 }
296 73 ▶ return unless $input->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $input->{"type"} 0 73 73
297  
298 73 ▶ if ($input->{type} =~ /(?:text|textarea|password|hidden|file)/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input->{'type'} =~ /(?:text|textarea|password|hidden|file)/i) { } 58 15 73
elsif ($input->{'type'} eq 'checkbox' and $input->{'checked'} and $input->{'checked'} eq 'checked') { } 11 8 19
299 58 ▶ return HTML::Entities::decode_entities($input->{value} // '');
Condition50%1/2
$input->{"value"} // ""
l!l
580
MC/DC0%0/1
$input->{"value"} // ""
$input->{"value"}
Truth table50%1/2
A
A= $input->{"value"}
Aresult
11
00
300 ▶ } elsif ($input->{type} eq 'checkbox' && $input->{checked} && $input->{checked} eq 'checked') {
Condition66%4/6
$input->{"type"} eq "checkbox" and $input->{"checked"} and $input->{"checked"} eq "checked"
!ll&&!rl&&r
4011
$input->{"type"} eq "checkbox" and $input->{"checked"}
!ll&&!rl&&r
0411
MC/DC33%1/3
$input->{"type"} eq "checkbox" and $input->{"checked"} and $input->{"checked"} eq "checked"
$input->{"type"} eq "checkbox" $input->{"checked"} $input->{"checked"} eq "checked"
Truth table50%2/4
A and B and C
A= $input->{"type"} eq "checkbox"B= $input->{"checked"}C= $input->{"checked"} eq "checked"
ABCresult
0XX0
10X0
1100
1111
301 # if value not set during $fieldset->add_field(), default to browser default value for checkbox: 'on'
302 11 ▶ return ($input->{value} // 'on');
Condition50%1/2
$input->{"value"} // "on"
l!l
110
MC/DC0%0/1
$input->{"value"} // "on"
$input->{"value"}
Truth table50%1/2
A
A= $input->{"value"}
Aresult
11
00
303 }
304 }
305 }
306 4 return;
307 }
308  
309 ################################################################################
310 # Usage : 1. $form_obj->set_field_error_message('input_element_id', 'some error');
311 # 2. $form_obj->set_field_error_message('error_element_id', 'some error');
312 # Purpose : Set error message based on input element id or error element id
313 # Returns : none
314 # Parameters : $field_id: Field ID (input or error)
315 # $error_msg: Error message text
316 # Comments : Public
317 # See Also : get_field_error_message
318 ################################################################################
319 sub set_field_error_message {
320 12 ▶ my $self = shift;
Subroutine set_field_error_message: called CC=3 SCAR=11.0
Pod undocumented
321 12 my $field_id = shift;
322 12 my $error_msg = shift;
323  
324 12 my $input_field = $self->_get_input_field($field_id);
325 12 ▶ if ($input_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_field) 11 1 12
326 11 $input_field->{data}{'error'}{'text'} = $error_msg;
327 11 return;
328 }
329  
330 1 my $error_field = $self->_get_error_field($field_id);
331 1 ▶ if ($error_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_field) 1 0 1
332 1 $error_field->{data}{'error'}{'text'} = $error_msg;
333 1 return;
334 }
335 0 return;
336 }
337  
338 ################################################################################
339 # Usage : 1. $form_obj->get_field_error_message('input_element_id');
340 # 2. $form_obj->get_field_error_message('error_element_id');
341 # Purpose : Get error message based on input element id or error element id
342 # Returns : text (Error message)
343 # Parameters : $field_id: Field ID (input or error)
344 # Comments : Public
345 # See Also : set_field_error_message
346 ################################################################################
347 sub get_field_error_message {
348 8 ▶ my $self = shift;
Subroutine get_field_error_message: called CC=3 SCAR=11.2
Pod undocumented
349 8 my $field_id = shift;
350  
351 8 my $input_field = $self->_get_input_field($field_id);
352 8 ▶ return $input_field->{data}{'error'}{'text'} if $input_field;
Branch: 1 branch
BranchTrueFalseTotal
if $input_field 7 1 8
353  
354 1 my $error_field = $self->_get_error_field($field_id);
355 1 ▶ return $error_field->{data}{'error'}{'text'} if $error_field;
Branch: 1 branch
BranchTrueFalseTotal
if $error_field 1 0 1
356  
357 0 return;
358 }
359  
360 #####################################################################
361 # Usage : $self->_get_input_field('amount');
362 # Purpose : Get the element based on input field id
363 # Returns : Element contains input field
364 # Parameters : $field_id: Field ID
365 # Comments : Private
366 # See Also :
367 #####################################################################
368 sub _get_input_field {
369 170 ▶ my $self = shift;
Subroutine _get_input_field: called CC=7 SCAR=19.5
370 170 my $field_id = shift;
371  
372 170 ▶ return unless $field_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_id 1 169 170
373 169 foreach my $fieldset (@{$self->{'fieldsets'}}) {
374 169 foreach my $input_field (@{$fieldset->{'fields'}}) {
375 757 my $inputs = $input_field->{data}{input};
376 757 foreach my $sub_input_field (@$inputs) {
377 689 ▶ if ( $sub_input_field->{id}
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_input_field->{"id"} and $sub_input_field->{"id"} eq $field_id) 164 525 689
Condition66%2/3
$sub_input_field->{"id"} and $sub_input_field->{"id"} eq $field_id
!ll&&!rl&&r
0525164
MC/DC50%1/2
$sub_input_field->{"id"} and $sub_input_field->{"id"} eq $field_id
$sub_input_field->{"id"} $sub_input_field->{"id"} eq $field_id
Truth table66%2/3
A and B
A= $sub_input_field->{"id"}B= $sub_input_field->{"id"} eq $field_id
ABresult
0X0
100
111
378 and $sub_input_field->{id} eq $field_id)
379 {
380 164 return $input_field;
381 }
382 }
383 }
384 }
385  
386 5 return;
387 }
388  
389 #####################################################################
390 # Usage : $self->_get_error_field('error_amount');
391 # Purpose : Get the element based on error field id
392 # Returns : Element contains error field
393 # Parameters : $error_id: Error ID
394 # Comments : Private
395 # See Also :
396 #####################################################################
397 sub _get_error_field {
398 2 ▶ my $self = shift;
Subroutine _get_error_field: called CC=6 SCAR=18.0
399 2 my $error_id = shift;
400  
401 2 ▶ return unless $error_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $error_id 0 2 2
402  
403 #build the form fieldset
404 2 foreach my $fieldset (@{$self->{'fieldsets'}}) {
405 2 foreach my $input_field (@{$fieldset->{'fields'}}) {
406 16 ▶ if ( $input_field->{data}{error}{id}
Branch: 1 branch
BranchTrueFalseTotal
if ($input_field->{"data"}{"error"}{"id"} and $input_field->{"data"}{"error"}{"id"} eq $error_id) 2 14 16
Condition100%3/3
$input_field->{"data"}{"error"}{"id"} and $input_field->{"data"}{"error"}{"id"} eq $error_id
!ll&&!rl&&r
4102
MC/DC100%2/2
$input_field->{"data"}{"error"}{"id"} and $input_field->{"data"}{"error"}{"id"} eq $error_id
$input_field->{"data"}{"error"}{"id"} $input_field->{"data"}{"error"}{"id"} eq $error_id
Truth table100%3/3
A and B
A= $input_field->{"data"}{"error"}{"id"}B= $input_field->{"data"}{"error"}{"id"} eq $error_id
ABresult
0X0
100
111
407 and $input_field->{data}{error}{id} eq $error_id)
408 {
409 2 return $input_field;
410 }
411 }
412 }
413 0 return;
414 }
415  
416 #####################################################################
417 # Usage : $self->_link_button({value => 'back', class => 'backbutton', href => '})
418 # Purpose : create link button html
419 # Returns : HTML
420 # Parameters : {value, class, href}
421 # Comments :
422 # See Also :
423 #####################################################################
424 sub _link_button {
425 3 ▶ my $args = shift;
Subroutine _link_button: called CC=4 SCAR=14.6
426  
427 3 ▶ my $myclass = $args->{'class'} ? 'button ' . $args->{'class'} : 'button';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'class'} ? : 3 0 3
428  
429 3 ▶ my $myid = $args->{'id'} ? 'id="' . $args->{'id'} . '"' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'id'} ? : 0 3 3
430 3 ▶ my $myspanid = $args->{'id'} ? 'id="span_' . $args->{'id'} . '"' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'id'} ? : 0 3 3
431  
432 3 return qq{<a class="$myclass" href="$args->{href}" $myid><span class="$myclass" $myspanid>$args->{value}</span></a>};
433 }
434  
435 #####################################################################
436 # Usage : $self->_wrap_fieldset($fieldset_html)
437 # Purpose : wrap fieldset html by template
438 # Returns : HTML
439 # Comments :
440 # See Also :
441 #####################################################################
442 sub _wrap_fieldset {
443 0 ▶ my ($self, $fieldset_html) = @_;
Subroutine _wrap_fieldset: not called CC=1 SCAR=6.9
444 0 my $output = '';
445 0 my $fieldset_template = <<EOF;
446 <div class="rbox form">
447 <div class="rbox-wrap">
448 $fieldset_html
449 <span class="tl">&nbsp;</span><span class="tr">&nbsp;</span><span class="bl">&nbsp;</span><span class="br">&nbsp;</span>
450 </div>
451 </div>
452 EOF
453  
454 0 return $fieldset_template;
455 }
456  
457 7 1;
458  
459 =head1 NAME
460  
461 HTML::FormBuilder - A Multi-part HTML form
462  
463 =head1 SYNOPSIS
464  
465 #define a form
466 my $form = HTML::FormBuilder->new(
467 data =>{id => 'formid',
468 class => 'formclass'},
469 classes => {row => 'rowdev'})
470  
471 #create fieldset
472 my $fieldset = $form->add_fieldset({id => 'fieldset1});
473  
474 #add field
475 $fieldset->add_field({input => {name => 'name', type => 'text', value => 'Join'}});
476  
477 #set field value
478 $form->set_field_value('name', 'Omid');
479  
480 #output the form
481 print $form->build;
482  
483 =head1 DESCRIPTION
484  
485 Object-oriented module for displaying an HTML form.
486  
487 =head2 Overview of Form's HTML structure
488  
489 The root of the structure is the <form> element and follow by multiple <fieldset> elements.
490  
491 In each <fieldset>, you can create rows which contain label, different input types, error message and comment <p> element.
492  
493 Please refer to L</"A full sample result">
494  
495 =head1 Attributes
496  
497 =head2 data
498  
499 The form attributes. It should be a hashref.
500  
501 =head2 classes
502  
503 The form classes. It should be a hashref. You can customize the form's layout by the classes.
504 The class names used are:
505  
506 fieldset_group
507 no_stack_field_parent
508 row_padding
509 fieldset_footer
510 comment
511 row
512 extra_tooltip_container
513 backbutton
514 required_asterisk
515 inputtrailing
516 label_column
517 input_column
518 hide_mobile
519  
520 =head2 localize
521  
522 The subroutine ref which can be called when translate something like 'Confirm'. The default value is no translating.
523  
524 =head2 fieldsets
525  
526 The fieldsets the form have.
527  
528 =head1 Methods
529  
530 =head2 new
531  
532 my $form = HTML::FormBuilder->new(
533 data =>{id => 'formid',
534 class => 'formclass'},
535 classes => {row => 'rowdev'})
536  
537 The id is required for the form.
538  
539 =head2 add_fieldset
540  
541 my $fieldset = $form->add_fieldset({id => 'fieldset1});
542  
543 the parameter is the fieldset attributes.
544 It will return the fielset object.
545  
546 =head2 add_field
547  
548 $form->add_field(0, {input => {name => 'name', type => 'text', value => 'Join'}});
549  
550 The parameter is the fieldset index to which you want to add the field and the field attributes.
551  
552 =head2 build
553  
554 print $form->build;
555  
556 the data in the $form will be changed when build the form. So you cannot get the same result if you call build twice.
557  
558 =head1 Cookbook
559  
560 =head2 a full sample
561  
562 # Before create a form, create a classes hash for the form
563 my $classes = {comment => 'comment', 'input_column' => 'column'};
564 # And maybe you need a localize function to translate something
565 my $localize = sub {i18n(shift)};
566  
567 # First, create the Form object. The keys in the HASH reference is the attributes of the form
568 $form_attributes => {'id' => 'id_of_the_form',
569 'name' => 'name_of_the_form',
570 'method' => 'post', # or get
571 'action' => 'page_to_submit',
572 'header' => 'My Form',
573 'localize' => $localize,
574 'classes' => $classes,
575 }; #header of the form
576 my $form = HTML::FormBuilder->new(data => $form_attributes, classes => $classes, localize => $localize);
577  
578 #Then create fieldset, the form is allow to have more than 1 fieldset
579 #The keys in the HASH reference is the attributes of the fieldset
580  
581 $fieldset_attributes => {'id' => 'id_of_the_fieldset',
582 'name' => 'name_of_the_fieldset',
583 'class' => 'myclass',
584 'header' => 'User details', #header of the fieldset
585 'comment' => 'please fill in', #message at the top of the fieldset
586 'footer' => '* - required',}; #message at the bottom of the fieldset
587 };
588 my $fieldset = $form->add_fieldset($fieldset_attributes);
589  
590 ####################################
591 #Create the input fields.
592 ####################################
593 #When creating an input fields, there are 4 supported keys.
594 #The keys are label, input, error, comment
595 # Label define the title of the input field
596 # Input define and create the actual input type
597 # In input fields, you can defined a key 'heading', which create a text before the input is displayed,
598 # however, if the input type is radio the text is behind the radio box
599 # Error message that go together with the input field when fail in validation
600 # Comment is the message added to explain the input field.
601  
602 ####################################
603 ###Creating a input text
604 ####################################
605  
606 my $input_text = {'label' => {'text' => 'Register Name', for => 'name'},
607 'input' => {'type' => 'text', 'value' => 'John', 'id' => 'name', 'name' => 'name', 'maxlength' => '22'},
608 'error' => { 'id' => 'error_name' ,'text' => 'Name must be in alphanumeric', 'class' => 'errorfield hidden'},
609 'comment' => {'text' => 'Please tell us your name'}};
610  
611 ####################################
612 ###Creating a select option
613 ####################################
614 my @options;
615 push @options, {'value' => 'Mr', 'text' => 'Mr'};
616 push @options, {'value' => 'Mrs', 'text' => 'Mrs'};
617  
618 my $input_select = {'label' => {'text' => 'Title', for => 'mrms'},
619 'input' => {'type' => 'select', 'id' => 'mrms', 'name' => 'mrms', 'options' => \@options},
620 'error' => {'text' => 'Please select a title', 'class' => 'errorfield hidden'}};
621  
622  
623 ####################################
624 ###Creating a hidden value
625 ####################################
626 my $input_hidden = {'input' => {'type' => 'hidden', 'value' => 'John', 'id' => 'name', 'name' => 'name'}};
627  
628 ####################################
629 ###Creating a submit button
630 ####################################
631 my $input_submit_button = {'input' => {'type' => 'submit', 'value' => 'Submit Form', 'id' => 'submit', 'name' => 'submit'}};
632  
633 ###NOTES###
634 Basically, you just need to change the type to the input type that you want and generate parameters with the input type's attributes
635  
636 ###########################################################
637 ###Having more than 1 input field in a single row
638 ###########################################################
639 my $input_select_dobdd = {'type' => 'select', 'id' => 'dobdd', 'name' => 'dobdd', 'options' => \@ddoptions};
640 my $input_select_dobmm = {'type' => 'select', 'id' => 'dobmm', 'name' => 'dobmm', 'options' => \@mmoptions};
641 my $input_select_dobyy = {'type' => 'select', 'id' => 'dobyy', 'name' => 'dobyy', 'options' => \@yyoptions};
642 my $input_select = {'label' => {'text' => 'Birthday', for => 'dobdd'},
643 'input' => [$input_select_dobdd, $input_select_dobmm, $input_select_dobyy],
644 'error' => {'text' => 'Invalid date.'}};
645  
646 #Then we add the input field into the Fieldset
647 #You can add using index of the fieldset
648 $fieldset->add_field($input_text);
649 $fieldset->add_field($input_select);
650 $fieldset->add_field($input_submit_button);
651  
652 ###########################################################
653 ### Field value accessors
654 ###########################################################
655 $form->set_field_value('name', 'Omid');
656 $form->get_field_value('name'); # Returns 'Omid'
657  
658 ###########################################################
659 ### Error message accessors
660 ###########################################################
661 $form->set_field_error_message('name', 'Your name is not good :)');
662 # or
663 $form->set_field_error_message('error_name', 'Your name is not good :)');
664  
665 $form->get_field_error_message('name'); # Return 'Your name is not good :)'
666 # or
667 $form->get_field_error_message('error_name'); # Return 'Your name is not good :)'
668  
669 #Finally, we output the form
670 print $form->build();
671  
672  
673 =head2 A full sample result
674  
675 <form id="onlineIDForm" method="post" action="">
676 <fieldset id="fieldset_one" name="fieldset_one" class="formclass">
677 <div>
678 <label for="name">Register Name</label>
679 <em>:</em>
680 <input type="text" value="John" id="name" name="name">
681 <p id = "error_name" class="errorfield hidden">Name must be in alphanumeric</p>
682 <p>Please tell us your name</p>
683 </div>
684 <div>
685 <label for="mrms">Title</label>
686 <em>:</em>
687 <select id="mrms" name="mrms">
688 <option value="Mr">Mr</option>
689 <option value="Mrs">Mrs</option>
690 </select>
691 <p class="errorfield hidden">Please select a title</p>
692 </div>
693 <div>
694 <label for="dob">Birthday</label>
695 <em>:</em>
696 <select id="dobdd" name="dobdd">
697 <option value="1">1</option>
698 <option value="2">2</option>
699 </select>
700 <select id="dobmm" name="dobmm">
701 <option value="1">Jan</option>
702 <option value="2">Feb</option>
703 </select>
704 <select id="dobyy" name="dobyy">
705 <option value="1980">1980</option>
706 <option value="1981">1981</option>
707 </select>
708 <p class="errorfield hidden">Invalid date</p>
709 </div>
710 <div>
711 <input type="submit" value="Submit Form" id="submit" name="submit">
712 </div>
713 </fieldset>
714 </form>
715  
716 =head2 How to create a form with validation
717  
718 Please refer to <HTML::FormBuilder::Validation>
719  
720 =head2 CROSS SITE REQUEST FORGERY PROTECTION
721  
722 read <HTML::FormBuilder::Validation> for more details
723  
724 =head1 AUTHOR
725  
726 Chylli L<chylli@binary.com>
727  
728 =head1 CONTRIBUTOR
729  
730 Fayland Lam L<fayland@binary.com>
731  
732 Tee Shuwn Yuan L<shuwnyuan@binary.com>
733  
734 =head1 COPYRIGHT AND LICENSE
735  
736 =cut
737  
↑ Summary FormBuilder/Validation.pm »