FormBuilder.pm

filter: Statementstmt 94.5% 192 / 203 Branchbran 76.7% 66 / 86 Conditioncond 64.8% 24 / 37 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 92.5% 25 / 27 Podpod 100% 9 / 9 totaltotal 84.6% 324 / 383 CC 71 SCAR 19.8
CC
71
Cov
87%
get_field_value
13.6
set_field_value
11.4
build_confirmation_button_with_all_inputs_hidden
10.5
CRAP 7.3
« FormBuilder/Validation.pm ↑ Summary FormBuilder/Base.pm »
1 package HTML::FormBuilder;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 our $VERSION = '0.12';
7  
8 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use HTML::FormBuilder::FieldSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use String::Random ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 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 9 );
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 9 );
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 9 });
46  
47 9 has csrftoken => (is => 'ro');
48  
49 sub BUILDARGS {
50 27 ▶ my (undef, @args) = @_;
Subroutine BUILDARGS: called CC=7 SCAR=19.5
Pod documented
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 documented
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 documented
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 documented
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 documented
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 documented
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 $fieldset_template = <<EOF;
445 <div class="rbox form">
446 <div class="rbox-wrap">
447 $fieldset_html
448 <span class="tl">&nbsp;</span><span class="tr">&nbsp;</span><span class="bl">&nbsp;</span><span class="br">&nbsp;</span>
449 </div>
450 </div>
451 EOF
452  
453 0 return $fieldset_template;
454 }
455  
456 9 1;
457  
458 =head1 NAME
459  
460 HTML::FormBuilder - A Multi-part HTML form
461  
462 =head1 SYNOPSIS
463  
464 #define a form
465 my $form = HTML::FormBuilder->new(
466 data =>{id => 'formid',
467 class => 'formclass'},
468 classes => {row => 'rowdev'})
469  
470 #create fieldset
471 my $fieldset = $form->add_fieldset({id => 'fieldset1});
472  
473 #add field
474 $fieldset->add_field({input => {name => 'name', type => 'text', value => 'Join'}});
475  
476 #set field value
477 $form->set_field_value('name', 'Omid');
478  
479 #output the form
480 print $form->build;
481  
482 =head1 DESCRIPTION
483  
484 Object-oriented module for displaying an HTML form.
485  
486 =head2 Overview of Form's HTML structure
487  
488 The root of the structure is the <form> element and follow by multiple <fieldset> elements.
489  
490 In each <fieldset>, you can create rows which contain label, different input types, error message and comment <p> element.
491  
492 Please refer to L</"A full sample result">
493  
494 =head1 Attributes
495  
496 =head2 data
497  
498 The form attributes. It should be a hashref.
499  
500 =head2 classes
501  
502 The form classes. It should be a hashref. You can customize the form's layout by the classes.
503 The class names used are:
504  
505 fieldset_group
506 no_stack_field_parent
507 row_padding
508 fieldset_footer
509 comment
510 row
511 extra_tooltip_container
512 backbutton
513 required_asterisk
514 inputtrailing
515 label_column
516 input_column
517 hide_mobile
518  
519 =head2 localize
520  
521 The subroutine ref which can be called when translate something like 'Confirm'. The default value is no translating.
522  
523 =head2 fieldsets
524  
525 The fieldsets the form have.
526  
527 =head1 Methods
528  
529 =head2 new
530  
531 my $form = HTML::FormBuilder->new(
532 data =>{id => 'formid',
533 class => 'formclass'},
534 classes => {row => 'rowdev'})
535  
536 The id is required for the form.
537  
538 =head2 add_fieldset
539  
540 my $fieldset = $form->add_fieldset({id => 'fieldset1});
541  
542 the parameter is the fieldset attributes.
543 It will return the fielset object.
544  
545 =head2 add_field
546  
547 $form->add_field(0, {input => {name => 'name', type => 'text', value => 'Join'}});
548  
549 The parameter is the fieldset index to which you want to add the field and the field attributes.
550  
551 =head2 build
552  
553 print $form->build;
554  
555 the data in the $form will be changed when build the form. So you cannot get the same result if you call build twice.
556  
557 =head2 BUILDARGS
558  
559 =head2 build_confirmation_button_with_all_inputs_hidden
560  
561 =head2 csrftoken
562  
563 =head2 get_field_error_message
564  
565 =head2 get_field_value
566  
567 =head2 set_field_error_message
568  
569 =head2 set_field_value
570  
571 =head1 Cookbook
572  
573 =head2 a full sample
574  
575 # Before create a form, create a classes hash for the form
576 my $classes = {comment => 'comment', 'input_column' => 'column'};
577 # And maybe you need a localize function to translate something
578 my $localize = sub {i18n(shift)};
579  
580 # First, create the Form object. The keys in the HASH reference is the attributes of the form
581 $form_attributes => {'id' => 'id_of_the_form',
582 'name' => 'name_of_the_form',
583 'method' => 'post', # or get
584 'action' => 'page_to_submit',
585 'header' => 'My Form',
586 'localize' => $localize,
587 'classes' => $classes,
588 }; #header of the form
589 my $form = HTML::FormBuilder->new(data => $form_attributes, classes => $classes, localize => $localize);
590  
591 #Then create fieldset, the form is allow to have more than 1 fieldset
592 #The keys in the HASH reference is the attributes of the fieldset
593  
594 $fieldset_attributes => {'id' => 'id_of_the_fieldset',
595 'name' => 'name_of_the_fieldset',
596 'class' => 'myclass',
597 'header' => 'User details', #header of the fieldset
598 'comment' => 'please fill in', #message at the top of the fieldset
599 'footer' => '* - required',}; #message at the bottom of the fieldset
600 };
601 my $fieldset = $form->add_fieldset($fieldset_attributes);
602  
603 ####################################
604 #Create the input fields.
605 ####################################
606 #When creating an input fields, there are 4 supported keys.
607 #The keys are label, input, error, comment
608 # Label define the title of the input field
609 # Input define and create the actual input type
610 # In input fields, you can defined a key 'heading', which create a text before the input is displayed,
611 # however, if the input type is radio the text is behind the radio box
612 # Error message that go together with the input field when fail in validation
613 # Comment is the message added to explain the input field.
614  
615 ####################################
616 ###Creating a input text
617 ####################################
618  
619 my $input_text = {'label' => {'text' => 'Register Name', for => 'name'},
620 'input' => {'type' => 'text', 'value' => 'John', 'id' => 'name', 'name' => 'name', 'maxlength' => '22'},
621 'error' => { 'id' => 'error_name' ,'text' => 'Name must be in alphanumeric', 'class' => 'errorfield hidden'},
622 'comment' => {'text' => 'Please tell us your name'}};
623  
624 ####################################
625 ###Creating a select option
626 ####################################
627 my @options;
628 push @options, {'value' => 'Mr', 'text' => 'Mr'};
629 push @options, {'value' => 'Mrs', 'text' => 'Mrs'};
630  
631 my $input_select = {'label' => {'text' => 'Title', for => 'mrms'},
632 'input' => {'type' => 'select', 'id' => 'mrms', 'name' => 'mrms', 'options' => \@options},
633 'error' => {'text' => 'Please select a title', 'class' => 'errorfield hidden'}};
634  
635  
636 ####################################
637 ###Creating a hidden value
638 ####################################
639 my $input_hidden = {'input' => {'type' => 'hidden', 'value' => 'John', 'id' => 'name', 'name' => 'name'}};
640  
641 ####################################
642 ###Creating a submit button
643 ####################################
644 my $input_submit_button = {'input' => {'type' => 'submit', 'value' => 'Submit Form', 'id' => 'submit', 'name' => 'submit'}};
645  
646 ###NOTES###
647 Basically, you just need to change the type to the input type that you want and generate parameters with the input type's attributes
648  
649 ###########################################################
650 ###Having more than 1 input field in a single row
651 ###########################################################
652 my $input_select_dobdd = {'type' => 'select', 'id' => 'dobdd', 'name' => 'dobdd', 'options' => \@ddoptions};
653 my $input_select_dobmm = {'type' => 'select', 'id' => 'dobmm', 'name' => 'dobmm', 'options' => \@mmoptions};
654 my $input_select_dobyy = {'type' => 'select', 'id' => 'dobyy', 'name' => 'dobyy', 'options' => \@yyoptions};
655 my $input_select = {'label' => {'text' => 'Birthday', for => 'dobdd'},
656 'input' => [$input_select_dobdd, $input_select_dobmm, $input_select_dobyy],
657 'error' => {'text' => 'Invalid date.'}};
658  
659 #Then we add the input field into the Fieldset
660 #You can add using index of the fieldset
661 $fieldset->add_field($input_text);
662 $fieldset->add_field($input_select);
663 $fieldset->add_field($input_submit_button);
664  
665 ###########################################################
666 ### Field value accessors
667 ###########################################################
668 $form->set_field_value('name', 'Omid');
669 $form->get_field_value('name'); # Returns 'Omid'
670  
671 ###########################################################
672 ### Error message accessors
673 ###########################################################
674 $form->set_field_error_message('name', 'Your name is not good :)');
675 # or
676 $form->set_field_error_message('error_name', 'Your name is not good :)');
677  
678 $form->get_field_error_message('name'); # Return 'Your name is not good :)'
679 # or
680 $form->get_field_error_message('error_name'); # Return 'Your name is not good :)'
681  
682 #Finally, we output the form
683 print $form->build();
684  
685  
686 =head2 A full sample result
687  
688 <form id="onlineIDForm" method="post" action="">
689 <fieldset id="fieldset_one" name="fieldset_one" class="formclass">
690 <div>
691 <label for="name">Register Name</label>
692 <em>:</em>
693 <input type="text" value="John" id="name" name="name">
694 <p id = "error_name" class="errorfield hidden">Name must be in alphanumeric</p>
695 <p>Please tell us your name</p>
696 </div>
697 <div>
698 <label for="mrms">Title</label>
699 <em>:</em>
700 <select id="mrms" name="mrms">
701 <option value="Mr">Mr</option>
702 <option value="Mrs">Mrs</option>
703 </select>
704 <p class="errorfield hidden">Please select a title</p>
705 </div>
706 <div>
707 <label for="dob">Birthday</label>
708 <em>:</em>
709 <select id="dobdd" name="dobdd">
710 <option value="1">1</option>
711 <option value="2">2</option>
712 </select>
713 <select id="dobmm" name="dobmm">
714 <option value="1">Jan</option>
715 <option value="2">Feb</option>
716 </select>
717 <select id="dobyy" name="dobyy">
718 <option value="1980">1980</option>
719 <option value="1981">1981</option>
720 </select>
721 <p class="errorfield hidden">Invalid date</p>
722 </div>
723 <div>
724 <input type="submit" value="Submit Form" id="submit" name="submit">
725 </div>
726 </fieldset>
727 </form>
728  
729 =head2 How to create a form with validation
730  
731 Please refer to <HTML::FormBuilder::Validation>
732  
733 =head2 CROSS SITE REQUEST FORGERY PROTECTION
734  
735 read <HTML::FormBuilder::Validation> for more details
736  
737 =head1 AUTHOR
738  
739 Chylli L<mailto:chylli@binary.com>
740  
741 =head1 CONTRIBUTOR
742  
743 Fayland Lam L<mailto:fayland@binary.com>
744  
745 Tee Shuwn Yuan L<mailto:shuwnyuan@binary.com>
746  
747 =head1 COPYRIGHT AND LICENSE
748  
749 =cut
750  
« FormBuilder/Validation.pm ↑ Summary FormBuilder/Base.pm »