FormBuilder/Validation.pm

filter: Statementstmt 93.3% 169 / 181 Branchbran 81.8% 54 / 66 Conditioncond 73.3% 44 / 60 MC/DCmcdc 48.4% 16 / 33 Subroutinesub 87.5% 21 / 24 Podpod 50.0% 4 / 8 totaltotal 82.8% 308 / 372 SCAR 42.9
CC
64
Cov
87%
_validate_field
18.3
validate
16.6
_build_javascript_validation
10.2
CRAP 73.1
« FormBuilder.pm ↑ Summary FormBuilder/Field.pm »
1 package HTML::FormBuilder::Validation;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.11';
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Class::Std::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 extends qw(HTML::FormBuilder);
17  
18 has has_error_of => (
19 is => 'rw',
20 default => 0,
21 isa => sub {
22 16 ▶ croak('has_error_of should be 0 or 1') unless $_[0] == 0 || $_[0] == 1;
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] == 0 or $_[0] == 1 0 16 16
Condition66%2/3
$_[0] == 0 or $_[0] == 1
l!l&&r!l&&!r
5110
MC/DC0%0/2
$_[0] == 0 or $_[0] == 1
$_[0] == 0 $_[0] == 1
Truth table66%2/3
A or B
A= $_[0] == 0B= $_[0] == 1
ABresult
1X1
011
000
23 },
24 2 );
25  
26 has custom_server_side_check_of => (
27 is => 'rw',
28 isa => sub {
29 0 ▶ croak('custom_server_side_check_of should be code')
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" 0 0 0
30 unless ref $_[0] eq 'CODE';
31 2 });
32  
33 2 has onsubmit_js_error => (
34 is => 'rw',
35 default => '',
36 );
37  
38 ########################################################################
39 # Usage : $form_validation_obj->set_input_fields(\%input);
40 # Purpose : Set input fields value based on last submit form.
41 # Returns : none
42 # Parameters : \%input: HASH ref to %input
43 # Comments : Public
44 # NOTE: This subroutine can use only if fields have same
45 # name and id.
46 # (i.e. <input id="name" name="name" type="text" />)
47 # See Also : n / a
48 ########################################################################
49 sub set_input_fields {
50 3 ▶ my $self = shift;
Subroutine set_input_fields: called CC=3 SCAR=11.0
Pod documented
51 3 my $input = shift;
52  
53 3 for my $element_id (keys %{$input}) {
54 8 ▶ if ($element_id eq 'csrftoken') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element_id eq 'csrftoken') { } 2 6 8
55 2 $self->{__input_csrftoken} = $input->{$element_id};
56 } else {
57 6 $self->set_field_value($element_id, $input->{$element_id});
58 }
59 }
60 3 return;
61 }
62  
63 sub build {
64 2 ▶ my $self = shift;
Subroutine build: called CC=6 SCAR=17.9
Pod documented
65 2 my $print_fieldset_index = shift;
66  
67 2 my $html;
68 2 my $javascript_validation = '';
69  
70 # build the fieldset, if $print_fieldset_index is specifed then we only generate that praticular fieldset with that index
71 2 my @fieldsets;
72 2 ▶ if (defined $print_fieldset_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $print_fieldset_index) { } 0 2 2
73 0 push @fieldsets, $self->{'fieldsets'}->[$print_fieldset_index];
74 } else {
75 2 @fieldsets = @{$self->{'fieldsets'}};
76 }
77  
78 # build the form fieldset
79 2 foreach my $fieldset (@fieldsets) {
80 2 foreach my $input_field (@{$fieldset->{'fields'}}) {
81  
82 # build inputs javascript validation
83 7 ▶ my $validation = $self->_build_javascript_validation({'input_field' => $input_field})
Condition100%2/2
$self->_build_javascript_validation({'input_field', $input_field}) || ''
l!l
52
MC/DC100%1/1
$self->_build_javascript_validation({'input_field', $input_field}) || ''
$self->_build_javascript_validation({'input_field', $input_field})
Truth table100%2/2
A
A= $self->_build_javascript_validation({'input_field', $input_field})
Aresult
11
00
84 || '';
85 7 $javascript_validation .= $validation;
86 }
87 }
88  
89 2 my $onsubmit_js_error = $self->onsubmit_js_error;
90 2 ▶ if ($onsubmit_js_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($onsubmit_js_error) 1 1 2
91 1 $onsubmit_js_error = "if (bResult == false) { $onsubmit_js_error; }";
92 }
93 2 $self->{data}{'onsubmit'} = "return (function () { var bResult = true; $javascript_validation; $onsubmit_js_error return bResult; })();";
94  
95 2 return $self->SUPER::build();
96 }
97  
98 ########################################################################
99 # Usage : $form_validation_obj->validate();
100 # Purpose : Validate form input
101 # Returns : true (No ERROR) / false
102 # Parameters : none
103 # Comments : Public
104 # See Also : n / a
105 ########################################################################
106 sub validate {
107 10 ▶ my $self = shift;
Subroutine validate: called CC=15 SCAR=28.1
Pod documented
108  
109 10 ▶ if ($self->csrftoken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->csrftoken) 2 8 10
110 2 ▶ $self->validate_csrf() or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->validate_csrf or return 0 1 1 2
111 }
112  
113 9 my @fieldsets = @{$self->{'fieldsets'}};
114 9 foreach my $fieldset (@fieldsets) {
115 INPUT_FIELD:
116 9 foreach my $input_field (@{$fieldset->{'fields'}}) {
117 49 my $data = $input_field->{data};
118 49 ▶ if ($data->{'input'} and $data->{'error'}->{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"input"} and $data->{"error"}{"id"}) 32 17 49
Condition100%3/3
$data->{"input"} and $data->{"error"}{"id"}
!ll&&!rl&&r
8932
MC/DC100%2/2
$data->{"input"} and $data->{"error"}{"id"}
$data->{"input"} $data->{"error"}{"id"}
Truth table100%3/3
A and B
A= $data->{"input"}B= $data->{"error"}{"id"}
ABresult
0X0
100
111
119 32 foreach my $input_element (@{$data->{'input'}}) {
120 40 ▶ if (eval { $input_element->{'input'}->can('value') }
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $input_element->{"input"}->can("value") } } and not defined $self->get_field_value($input_element->{"id"})) 0 40 40
Condition33%1/3
eval { and not defined $self->get_field_value($input_element->{"id"})
!ll&&!rl&&r
4000
MC/DC0%0/2
eval { do { $input_element->{"input"}->can("value") } } and not defined $self->get_field_value($input_element->{"id"})
eval { do { $input_element->{"input"}->can("value") } } not defined $self->get_field_value($input_element->{"id"})
Truth table33%1/3
A and not(B)
A= eval { do { $input_element->{"input"}->can("value") } }B= not defined $self->get_field_value($input_element->{"id"})
ABresult
0X0
100
111
121 and (not defined $self->get_field_value($input_element->{'id'})))
122 {
123 0 $self->set_field_error_message($input_element->{'id'}, $self->_localize('Invalid amount'));
124 0 next INPUT_FIELD;
125 }
126 }
127 }
128  
129 # Validate each field
130 49 ▶ if ( defined $data->{'validation'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"validation"} and $data->{"input"} and $data->{"error"}{"id"}) 32 17 49
Condition66%4/6
defined $data->{"validation"} and $data->{"input"} and $data->{"error"}{"id"}
!ll&&!rl&&r
17032
defined $data->{"validation"} and $data->{"input"}
!ll&&!rl&&r
17032
MC/DC33%1/3
defined $data->{"validation"} and $data->{"input"} and $data->{"error"}{"id"}
defined $data->{"validation"} $data->{"input"} $data->{"error"}{"id"}
Truth table50%2/4
A and B and C
A= defined $data->{"validation"}B= $data->{"input"}C= $data->{"error"}{"id"}
ABCresult
0XX0
10X0
1100
1111
131 and $data->{'input'}
132 and $data->{'error'}->{'id'})
133 {
134 $self->_validate_field({
135 'validation' => $data->{'validation'},
136 32 'input_element' => $data->{'input'},
137 });
138 }
139 }
140 }
141  
142 9 ▶ if ($self->custom_server_side_check_of) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->custom_server_side_check_of) 0 9 9
143 0 $self->custom_server_side_check_of->();
144 }
145  
146 9 ▶ return ($self->get_has_error) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_has_error ? : 7 2 9
147 }
148  
149 sub validate_csrf {
150 2 ▶ my ($self) = @_;
Subroutine validate_csrf: called CC=3 SCAR=11.0
Pod undocumented
151  
152 2 ▶ if (($self->{__input_csrftoken} // '') eq $self->csrftoken) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"__input_csrftoken"} // "") eq $self->csrftoken) 1 1 2
Condition50%1/2
$self->{"__input_csrftoken"} // ""
l!l
20
MC/DC0%0/1
$self->{"__input_csrftoken"} // ""
$self->{"__input_csrftoken"}
Truth table50%1/2
A
A= $self->{"__input_csrftoken"}
Aresult
11
00
153 1 return 1;
154 }
155  
156 1 $self->_set_has_error();
157 1 return 0;
158 }
159  
160 sub is_error_found_in {
161 0 ▶ my $self = shift;
Subroutine is_error_found_in: not called CC=1 SCAR=6.9
Pod documented
162 0 my $input_element_id = shift;
163  
164 0 return $self->get_field_error_message($input_element_id);
165 }
166  
167 ########################################################################
168 # Usage : $self->_set_has_error();
169 # Purpose : Set has error to indicate form has error and should be
170 # rebuild again.
171 # Returns : none
172 # Parameters : none
173 # Comments : Private
174 # See Also : n / a
175 ########################################################################
176 sub _set_has_error {
177 11 ▶ my $self = shift;
Subroutine _set_has_error: called CC=1 SCAR=0.0
178  
179 11 $self->has_error_of(1);
180 11 return;
181 }
182  
183 ########################################################################
184 # Usage : $form_validation_obj->get_has_error();
185 # Purpose : Check if form has error
186 # Returns : 0 / 1
187 # Parameters : none
188 # Comments : Public
189 # See Also : n / a
190 ########################################################################
191 sub get_has_error {
192 11 ▶ my $self = shift;
Subroutine get_has_error: called CC=1 SCAR=0.0
Pod undocumented
193 11 return $self->has_error_of;
194 }
195  
196 sub set_field_error_message {
197 10 ▶ my $self = shift;
Subroutine set_field_error_message: called CC=2 SCAR=7.0
Pod undocumented
198 10 my $element_id = shift;
199 10 my $error_message = shift;
200  
201 10 $self->SUPER::set_field_error_message($element_id, $error_message);
202 10 ▶ if ($error_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_message) 10 0 10
203 10 $self->_set_has_error();
204 }
205 10 return;
206 }
207  
208 ########################################################################
209 # Usage : $self->_build_javascript_validation
210 # ({
211 # 'validation' => $input_field->{'validation'},
212 # 'input_element' => $input_field->{'input'},
213 # 'error_element_id' => $input_field->{'error'}->{'id'},
214 # });
215 # Purpose : Create javascript validation code.
216 # Returns : text (Javascript code)
217 # Parameters : $arg_ref:
218 # {
219 # 'validation': ARRAY ref to $input_field->{'validation'}
220 # 'input_element': HASH ref to input element
221 # 'error_element_id': error element id
222 # }
223 # Comments : Private
224 # See Also : build()
225 ########################################################################
226 sub _build_javascript_validation {
227 7 ▶ my $self = shift;
Subroutine _build_javascript_validation: called CC=10 SCAR=23.2
228 7 my $arg_ref = shift;
229 7 my $javascript;
230  
231 7 my $input_field = $arg_ref->{'input_field'};
232  
233 7 my $data = $input_field->{data};
234 7 ▶ if ( defined $data->{'validation'}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $data->{'validation'} and $data->{'input'} and $data->{'error'}{'id'}) { } 4 3 7
elsif (defined $data->{'error'} and defined $data->{'error'}{'id'}) { } 1 4 5
Condition66%4/6
defined $data->{"validation"} and $data->{"input"} and $data->{"error"}{"id"}
!ll&&!rl&&r
304
defined $data->{"validation"} and $data->{"input"}
!ll&&!rl&&r
304
MC/DC33%1/3
defined $data->{"validation"} and $data->{"input"} and $data->{"error"}{"id"}
defined $data->{"validation"} $data->{"input"} $data->{"error"}{"id"}
Truth table50%2/4
A and B and C
A= defined $data->{"validation"}B= $data->{"input"}C= $data->{"error"}{"id"}
ABCresult
0XX0
10X0
1100
1111
235 and $data->{'input'}
236 and $data->{'error'}->{'id'})
237 {
238  
239 4 my @validations = @{$data->{'validation'}};
240 4 my $input_element = $data->{'input'};
241 4 my $error_element_id = $data->{'error'}->{'id'};
242  
243 4 my $input_element_id;
244 my $input_element_conditions;
245  
246 4 foreach my $input_field (@{$input_element}) {
247 5 ▶ if (defined $input_field->{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_field->{"id"}) 5 0 5
248 5 $input_element_id = $input_field->{'id'};
249 5 $javascript .= "var input_element_$input_element_id = document.getElementById('$input_element_id');";
250 5 $input_element_conditions .= "input_element_$input_element_id && ";
251 }
252 }
253  
254 $javascript .=
255 4 "var error_element_$error_element_id = document.getElementById('$error_element_id');"
256 . "document.getElementById('$error_element_id').innerHTML = '';"
257 . "if ($input_element_conditions error_element_$error_element_id) {"
258 . 'var regexp;'
259 . 'var bInputResult = true;';
260  
261 4 foreach my $validation (@validations) {
262 next
263 10 ▶ unless ($validation->{'type'} =~ /(?:regexp|min_amount|max_amount|checkbox_checked|custom)/);
Branch: 1 branch
BranchTrueFalseTotal
unless $validation->{"type"} =~ /(?:regexp|min_amount|max_amount|checkbox_checked|custom)/ 0 10 10
264 10 $javascript .= $self->_build_single_javascript_validation($validation, $input_element_id, $error_element_id);
265 }
266  
267 4 $javascript .= 'if (!bInputResult)' . '{' . 'bResult = bInputResult;' . '}' . '}';
268  
269 }
270  
271 # get the general error field (contain only error message without input)
272 ▶ elsif ( defined $data->{'error'}
Condition66%2/3
defined $data->{"error"} and defined $data->{"error"}{"id"}
!ll&&!rl&&r
201
MC/DC50%1/2
defined $data->{"error"} and defined $data->{"error"}{"id"}
defined $data->{"error"} defined $data->{"error"}{"id"}
Truth table66%2/3
A and B
A= defined $data->{"error"}B= defined $data->{"error"}{"id"}
ABresult
0X0
100
111
273 and defined $data->{'error'}->{'id'})
274 {
275 1 my $error_id = $data->{'error'}->{'id'};
276  
277 1 $javascript = "var error_element_$error_id = document.getElementById('$error_id');" . "document.getElementById('$error_id').innerHTML = '';";
278 }
279  
280 7 return $javascript;
281 }
282  
283 ########################################################################
284 # Usage : $self->_build_single_validation
285 # ($validation, $input_element_id, $error_element_id);
286 # Purpose : Create javascript validation code for a validation.
287 # Returns : text (Javascript code)
288 # Parameters : validation, input_element_id, error_element_id
289 # Comments : Private
290 # See Also : build()
291 ########################################################################
292 sub _build_single_javascript_validation {
293 10 ▶ my $self = shift;
Subroutine _build_single_javascript_validation: called CC=10 SCAR=23.1
294 10 my $validation = shift;
295 10 my $input_element_id = shift;
296 10 my $error_element_id = shift;
297  
298 10 my $javascript = '';
299 10 my $err_msg = _encode_text($validation->{'err_msg'});
300  
301 # if the id define in the validation hash, meaning input has more than 1 fields, the validation is validated against the id
302 10 ▶ if ($validation->{'id'} and length $validation->{'id'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validation->{"id"} and length $validation->{"id"} > 0) 3 7 10
Condition66%2/3
$validation->{"id"} and length $validation->{"id"} > 0
!ll&&!rl&&r
703
MC/DC50%1/2
$validation->{"id"} and length $validation->{"id"} > 0
$validation->{"id"} length $validation->{"id"} > 0
Truth table66%2/3
A and B
A= $validation->{"id"}B= length $validation->{"id"} > 0
ABresult
0X0
100
111
303 3 $input_element_id = $validation->{'id'};
304 }
305  
306 10 ▶ my $error_if_true = $validation->{error_if_true} ? '' : '!';
Branch: 1 branch
BranchTrueFalseTotal
$validation->{'error_if_true'} ? : 0 10 10
307 10 my $test = '';
308 10 ▶ if ($validation->{'type'} eq 'regexp') {
Branch: 4 branches
BranchTrueFalseTotal
if ($validation->{'type'} eq 'regexp') { } 5 5 10
elsif ($validation->{'type'} =~ /^(min|max)_amount$/) { } 3 2 5
elsif ($validation->{'type'} eq 'checkbox_checked') { } 1 1 2
elsif ($validation->{'type'} eq 'custom') { } 1 0 1
309 5 my $regexp = $validation->{'regexp'};
310 5 $regexp =~ s/(\\|')/\\$1/g;
311 $javascript .=
312 5 ▶ ($validation->{'case_insensitive'})
Branch: 1 branch
BranchTrueFalseTotal
$validation->{'case_insensitive'} ? : 1 4 5
313 ? "regexp = new RegExp('$regexp', 'i');"
314 : "regexp = new RegExp('$regexp');";
315  
316 5 $test = qq[${error_if_true}regexp.test(input_element_$input_element_id.value)];
317 }
318  
319 # Min Max amount checking
320 elsif ($validation->{'type'} =~ /^(min|max)_amount$/) {
321 3 ▶ my $op = $1 eq 'min' ? '<' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'min' ? : 2 1 3
322 3 $test = qq[input_element_$input_element_id.value $op $validation->{amount}];
323 }
324  
325 # checkbox checked checking
326 elsif ($validation->{'type'} eq 'checkbox_checked') {
327 1 $test = qq[input_element_$input_element_id.checked === false];
328 }
329  
330 # Custom checking
331 elsif ($validation->{'type'} eq 'custom') {
332 1 $test = qq[${error_if_true}$validation->{function}];
333 }
334 10 $javascript .= qq[if (bInputResult && $test){error_element_$error_element_id.innerHTML = decodeURIComponent('$err_msg');bInputResult = false;}];
335  
336 10 return $javascript;
337 }
338  
339 ########################################################################
340 # Usage : $form_validation_obj->set_server_side_checks($custom_server_side_sub_ref);
341 # Purpose : Set custom server side validation
342 # Returns : none
343 # Parameters : $server_side_check_sub_ref: sub ref
344 # Comments : Public
345 # See Also : n / a
346 ########################################################################
347 sub set_server_side_checks {
348 0 ▶ my $self = shift;
Subroutine set_server_side_checks: not called CC=1 SCAR=6.9
Pod undocumented
349 0 my $server_side_check_sub_ref = shift;
350 0 $self->custom_server_side_check_of($server_side_check_sub_ref);
351 0 return;
352 }
353  
354 ########################################################################
355 # Usage : $self->_validate_field({
356 # 'validation' => $input_field->{'validation'},
357 # 'input_element' => $input_field->{'input'},
358 # });
359 # Purpose : Server side validation base on type of validation
360 # Returns : none
361 # Parameters : $arg_ref:
362 # {
363 # 'validation': ARRAY ref to $input_field->{'validation'}
364 # 'input_element': HASH ref to input element
365 # }
366 # Comments : Private
367 # See Also : validate()
368 ########################################################################
369 sub _validate_field {
370 32 ▶ my $self = shift;
Subroutine _validate_field: called CC=18 SCAR=29.1
371 32 my $arg_ref = shift;
372  
373 32 my @validations = @{$arg_ref->{'validation'}};
374 32 my $input_element = $arg_ref->{'input_element'};
375 32 my $input_element_id;
376 my $field_value;
377  
378 32 foreach my $validation (@validations) {
379 65 ▶ if ( $validation->{'type'}
Branch: 1 branch
BranchTrueFalseTotal
if ($validation->{"type"} and $validation->{"type"} =~ /(?:regexp|min_amount|max_amount|checkbox_checked)/) 61 4 65
Condition66%2/3
$validation->{"type"} and $validation->{"type"} =~ /(?:regexp|min_amount|max_amount|checkbox_checked)/
!ll&&!rl&&r
0461
MC/DC50%1/2
$validation->{"type"} and $validation->{"type"} =~ /(?:regexp|min_amount|max_amount|checkbox_checked)/
$validation->{"type"} $validation->{"type"} =~ /(?:regexp|min_amount|max_amount|checkbox_checked)/
Truth table66%2/3
A and B
A= $validation->{"type"}B= $validation->{"type"} =~ /(?:regexp|min_amount|max_amount|checkbox_checked)/
ABresult
0X0
100
111
380 and $validation->{'type'} =~ /(?:regexp|min_amount|max_amount|checkbox_checked)/)
381 {
382  
383 # The input_element must be an array. so if validation no 'id', then we use the first element's id
384 # because the array should be just one element.
385 61 ▶ $input_element_id = $validation->{id} || $input_element->[0]{id};
Condition66%2/3
$validation->{'id'} || $input_element->[0]{'id'}
l!l&&r!l&&!r
19420
MC/DC0%0/2
$validation->{'id'} || $input_element->[0]{'id'}
$validation->{'id'} $input_element->[0]{'id'}
Truth table66%2/3
A || B
A= $validation->{'id'}B= $input_element->[0]{'id'}
ABresult
1X1
011
000
386  
387 # Check with whitespace trimmed from both ends to make sure that it's reasonable.
388 61 ▶ $field_value = $self->get_field_value($input_element_id) || '';
Condition100%2/2
$self->get_field_value($input_element_id) || ''
l!l
565
MC/DC100%1/1
$self->get_field_value($input_element_id) || ''
$self->get_field_value($input_element_id)
Truth table100%2/2
A
A= $self->get_field_value($input_element_id)
Aresult
11
00
389 # $field_value =~ s/^\s+|\s+$//g;
390 61 $field_value =~ s/\A\s+//;
391 61 $field_value =~ s/\s+\z//;
392  
393 61 ▶ if ($validation->{'type'} eq 'regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if ($validation->{'type'} eq 'regexp') { } 37 24 61
elsif ($validation->{'type'} eq 'min_amount' and $field_value < $validation->{'amount'} or $validation->{'type'} eq 'max_amount' and $field_value > $validation->{'amount'}) { } 3 21 24
elsif ($validation->{'type'} eq 'checkbox_checked' and not $field_value) { } 1 33 34
394 my $regexp =
395 37 ▶ ($validation->{'case_insensitive'})
Branch: 1 branch
BranchTrueFalseTotal
$validation->{'case_insensitive'} ? : 8 29 37
396 ? qr{$validation->{'regexp'}}i
397 : qr{$validation->{'regexp'}};
398 37 ▶ if ($validation->{error_if_true} && $field_value =~ $regexp
Branch: 1 branch
BranchTrueFalseTotal
if ($validation->{"error_if_true"} and $field_value =~ /$regexp/ or not $validation->{"error_if_true"} || $field_value =~ /$regexp/) 6 31 37
Condition55%5/9
$validation->{"error_if_true"} and $field_value =~ /$regexp/ or not $validation->{"error_if_true"} || $field_value =~ /$regexp/
l!l&&r!l&&!r
0631
$validation->{"error_if_true"} and $field_value =~ /$regexp/
!ll&&!rl&&r
3700
$validation->{'error_if_true'} || $field_value =~ /$regexp/
l!l&&r!l&&!r
0316
MC/DC25%1/4
$validation->{"error_if_true"} and $field_value =~ /$regexp/ or not $validation->{"error_if_true"} || $field_value =~ /$regexp/
$validation->{"error_if_true"} $field_value =~ /$regexp/ $validation->{'error_if_true'} $field_value =~ /$regexp/
Truth table28%2/7
A and B or not(C || D)
A= $validation->{"error_if_true"}B= $field_value =~ /$regexp/C= $validation->{'error_if_true'}D= $field_value =~ /$regexp/
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
399 || !$validation->{error_if_true} && $field_value !~ $regexp)
400 {
401 6 $self->set_field_error_message($input_element_id, $validation->{'err_msg'});
402 6 return 0;
403 }
404 }
405  
406 # Min amount checking
407 ▶ elsif ($validation->{'type'} eq 'min_amount' && $field_value < $validation->{'amount'}
Condition100%9/9
$validation->{"type"} eq "min_amount" and $field_value < $validation->{"amount"} or $validation->{"type"} eq "max_amount" and $field_value > $validation->{"amount"}
l!l&&r!l&&!r
2121
$validation->{"type"} eq "min_amount" and $field_value < $validation->{"amount"}
!ll&&!rl&&r
1392
$validation->{"type"} eq "max_amount" and $field_value > $validation->{"amount"}
!ll&&!rl&&r
1741
MC/DC100%4/4
$validation->{"type"} eq "min_amount" and $field_value < $validation->{"amount"} or $validation->{"type"} eq "max_amount" and $field_value > $validation->{"amount"}
$validation->{"type"} eq "min_amount" $field_value < $validation->{"amount"} $validation->{"type"} eq "max_amount" $field_value > $validation->{"amount"}
Truth table71%5/7
A and B or C and D
A= $validation->{"type"} eq "min_amount"B= $field_value < $validation->{"amount"}C= $validation->{"type"} eq "max_amount"D= $field_value > $validation->{"amount"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
408 || $validation->{'type'} eq 'max_amount' && $field_value > $validation->{'amount'})
409 {
410 3 $self->set_field_error_message($input_element_id, $validation->{'err_msg'});
411 3 return 0;
412 }
413  
414 ▶ elsif ($validation->{'type'} eq 'checkbox_checked' && !$field_value) {
Condition100%3/3
$validation->{"type"} eq "checkbox_checked" and not $field_value
!ll&&!rl&&r
1371
MC/DC100%2/2
$validation->{"type"} eq "checkbox_checked" and not $field_value
$validation->{"type"} eq "checkbox_checked" not $field_value
Truth table100%3/3
A and not(B)
A= $validation->{"type"} eq "checkbox_checked"B= not $field_value
ABresult
0X0
100
111
415 1 $self->set_field_error_message($input_element_id, $validation->{'err_msg'});
416 1 return 0;
417 }
418 }
419 }
420 22 return 1;
421 }
422  
423 sub _encode_text {
424 10 ▶ my $text = shift;
Subroutine _encode_text: called CC=2 SCAR=7.0
425  
426 10 ▶ return unless ($text);
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 10 10
427  
428 # javascript cant load html entities
429 10 $text = Encode::encode("UTF-8", HTML::Entities::decode_entities($text));
430 10 $text = URI::Escape::uri_escape($text);
431 10 $text =~ s/(['"\\])/\\$1/g;
432  
433 10 return $text;
434 }
435  
436 2 1;
437  
438 =head1 NAME
439  
440 HTML::FormBuilder::Validation - An extention of the Form object, to allow for javascript-side validation of inputs
441 and also server-side validation after the form is POSTed
442  
443 =head1 SYNOPSIS
444  
445 First, create the Form object. The keys in the HASH reference is the attributes
446 of the form.
447  
448 # Form attributes require to create new form object
449 my $form_attributes =
450 {
451 'name' => 'name_test_form',
452 'id' => 'id_test_form',
453 'method' => 'post',
454 'action' => "http://www.domain.com/contact.cgi",
455 'class' => 'formObject',
456 };
457 my $form_obj = new HTML::FormBuilder::Validation(data => $form_attributes);
458  
459 my $fieldset = $form_obj->add_fieldset({});
460  
461  
462 =head2 Create the input fields with validation
463  
464 This is quite similar to creating input field in Form object. Likewise you can
465 add validation to HASH reference as the attribute of input field.
466  
467 Below you can see the sample included four types of validation:
468  
469 1. regexp: Just write the reqular expression that should be apply to the value
470  
471 2. min_amount: Needs both type=min_amount and also minimum amount that declared
472 in amount
473  
474 3. max_amount: Just like min_amount
475  
476 4. checkbox_checked: Ensure checkbox is checked by user
477  
478 5. custom: Just the javascript function call with parameters should be given to.
479 It only specifies client side validation.
480  
481 my $input_field_amount =
482 {
483 'label' =>
484 {
485 'text' => 'Amount',
486 'for' => 'amount',
487 'optional' => '0',
488 },
489 'input' =>
490 {
491 'type' => 'text',
492 'id' => 'amount',
493 'name' => 'amount',
494 'maxlength' => 40,
495 'value' => '',
496 },
497 'error' =>
498 {
499 'text' => '',
500 'id' => 'error_amount',
501 'class' => 'errorfield',
502 },
503 'validation' =>
504 [
505 {
506 'type' => 'regexp',
507 'regexp' => '\w+',
508 'err_msg' => 'Not empty',
509 },
510 {
511 'type' => 'regexp',
512 'regexp' => '\d+',
513 'err_msg' => 'Must be digit',
514 },
515 {
516 'type' => 'min_amount',
517 'amount' => 50,
518 'err_msg' => 'Too little',
519 },
520 {
521 'type' => 'max_amount',
522 'amount' => 500,
523 'err_msg' => 'Too much',
524 },
525 {
526 'type' => 'custom',
527 'function' => 'custom_amount_validation()',
528 'err_msg' => 'It is not good',
529 },
530 ],
531 };
532  
533 my $terms_and_condition_checkbox =
534 {
535 'label' =>
536 {
537 'text' => 'I have read & agree to the terms & condition of the site',
538 'for' => 'tnc',
539 },
540 'input' =>
541 {
542 'type' => 'checkbox',
543 'id' => 'tnc',
544 'name' => 'tnc',
545 'value' => '1', # optional
546 },
547 'error' =>
548 {
549 'id' => 'error_tnc',
550 'class' => 'errorfield',
551 },
552 'validation' =>
553 [
554 {
555 'type' => 'checkbox_checked',
556 'err_msg' => 'In order to proceed, you need to agree to the terms & condition',
557 },
558 ],
559 };
560  
561 Below is another example with two different fields. In this matter we need to
562 indicate the id of each field in validation attributes.
563  
564 my $select_curr =
565 {
566 'id' => 'select_text_curr',
567 'name' => 'select_text_curr',
568 'type' => 'select',
569 'options' => '<option value=""></option><option value="USD">USD</option><option value="EUR">EUR</option>',
570 };
571 my $input_amount =
572 {
573 'id' => 'select_text_amount',
574 'name' => 'select_text_amount',
575 'type' => 'text',
576 'value' => ''
577 };
578 my $input_field_select_text =
579 {
580 'label' =>
581 {
582 'text' => 'select_text',
583 'for' => 'select_text',
584 },
585 'input' => [ $select_curr, $input_amount ],
586 'error' =>
587 {
588 'text' => '',
589 'id' => 'error_select_text',
590 'class' => 'errorfield',
591 },
592 'validation' =>
593 [
594 {
595 'type' => 'regexp',
596 'id' => 'select_text_curr',
597 'regexp' => '\w+',
598 'err_msg' => 'Must be select',
599 },
600 {
601 'type' => 'regexp',
602 'id' => 'select_text_amount',
603 'regexp' => '\d+',
604 'err_msg' => 'Must be digits',
605 },
606 {
607 'type' => 'min_amount',
608 'id' => 'select_text_amount',
609 'amount' => 50,
610 'err_msg' => 'Too little',
611 },
612 ],
613 };
614  
615 my $general_error_field =
616 {
617 'error' =>
618 {
619 'text' => '',
620 'id' => 'error_general',
621 'class' => 'errorfield'
622 },
623 };
624  
625 =head2 Adding input fields to form object
626  
627 Here is just add fields to the form object like before.
628  
629 $form_obj->add_field($fieldset_index, $general_error_field);
630 $form_obj->add_field($fieldset_index, $input_field_amount);
631 $form_obj->add_field($fieldset_index, $input_field_select_text);
632  
633 =head2 Define Javascript code to be run, during onsubmit input validation error
634  
635 This javascript code will be run before onsubmit return false
636  
637 $form_obj->onsubmit_js_error("\$('#residence').attr('disabled', true);");
638 $form_obj->onsubmit_js_error('onsubmit_error_disable_fields()');
639  
640 =head2 Custom javascript validation
641  
642 Custom javascript validation should be defined and assigned to the form object.
643 Note that, the name and parameters should be the same as the way you indicate
644 function call in validation attributes.
645  
646 You can see a sample below:
647  
648 my $custom_javascript = qq~
649 function custom_amount_validation()
650 {
651 var input_amount = document.getElementById('amount');
652 if (input_amount.value == 100)
653 {
654 return false;
655 }
656 return true;
657 }~;
658  
659 =head2 Custom server side validation
660  
661 The custom server side validation is quite similar to javascript. A reference to
662 a subrotine should be pass to form object.
663  
664 my $custom_server_side_sub_ref = sub {
665 if ($form_obj->get_field_value('name') eq 'felix')
666 {
667 $form_obj->set_field_error_message('name', 'felix is not allow to use this page');
668 $form_obj->set_field_error_message('error_general', 'There is an error !!!');
669 }
670 };
671  
672 $form_obj->set_server_side_checks($custom_server_side_sub_ref);
673  
674 =head2 Use form object in cgi files
675  
676 Somewhere in cgi files you can just print the result of build().
677  
678 print $form_obj->build();
679  
680 In submit you need to fill form values, use set_input_fields(\%input) and pass
681 %input HASH and then show what ever you want in result of validation. Just like
682 below:
683  
684 if (not $form_obj->validate())
685 {
686 print '<h1>Test Form</h1>';
687 print $form_obj->build();
688 }
689 else
690 {
691 print '<h1>Success !!!</h1>';
692 }
693  
694 code_exit();
695  
696 =head1 Attributes
697  
698 =head2 has_error_of
699  
700 The tag that error happened during validation
701  
702 =head2 custom_server_side_check_of
703  
704 The custom server side subroutine that will be run on server side.
705  
706 =head2 onsubmit_js_error
707  
708 javasript code to run during onsubmit error by javasript validation
709  
710 =head1 METHODS
711  
712 =head2 set_input_fields
713  
714 $form_validation_obj->set_input_fields({username => $username});
715  
716 assign value to the input fields
717  
718 =head2 validate
719  
720 $form_validation_obj->validate();
721  
722 validate form input and return true or false
723  
724 =head2 is_error_found_in
725  
726 $form_validation_obj->is_error_found_in($input_element_id);
727  
728 check the erorr is founded in the input element or not
729  
730 =head1 CROSS SITE REQUEST FORGERY PROTECTION
731  
732 for plain CGI or other framework, read Dancer example below.
733  
734 =head2 CSRF and Dancer
735  
736 =over 4
737  
738 =item * create form HTML and store csrftoken in session
739  
740 my $form = HTML::FormBuilder::Validation->new(data => $form_attributes, csrftoken => 1);
741 ...
742 my $html = $form->build;
743  
744 # save csrf token in session or cookie
745 session(__csrftoken => $form->csrftoken);
746  
747 =item * validate csrftoken on form submit
748  
749 my $csrftoken = session('__csrftoken');
750 my $form = HTML::FormBuilder::Validation->new(data => $form_attributes, csrftoken => $csrftoken);
751 $form->validate_csrf() or die 'CSRF failed.';
752 # or call
753 if ( $form->validate() ) { # it calls validate_csrf inside
754 # Yap! it's ok
755 } else {
756 # NOTE we do not have error for csrf on form HTML build
757 # show form again with $form->build
758 }
759  
760 =back
761  
762 =head2 CSRF and Mojolicious
763  
764 if you're using L<Mojolicious> and have DefaultHelpers plugin enabled, it's simple to add csrftoken in Validation->new as below:
765  
766 my $form = HTML::FormBuilder::Validation->new(data => $form_attributes, csrftoken => $c->csrf_token);
767  
768 Mojolicious $c->csrf_token will handle the session part for you.
769  
770 =head1 AUTHOR
771  
772 Chylli L<chylli@binary.com>
773  
774 =head1 CONTRIBUTOR
775  
776 Fayland Lam L<fayland@binary.com>
777  
778 Tee Shuwn Yuan L<shuwnyuan@binary.com>
779  
780 =head1 COPYRIGHT AND LICENSE
781  
782 =cut
« FormBuilder.pm ↑ Summary FormBuilder/Field.pm »