blib/lib/WWW/Form.pm

filter: Statementstmt 65.7% 259 / 394 Branchbran 45.1% 37 / 82 Conditioncond 53.5% 15 / 28 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 75.0% 36 / 48 Podpod 100% 30 / 30 totaltotal 63.6% 380 / 597 SCAR 57.7
CC
67
Cov
62%
validateFields
42.0
getSubmitButtonHTML
25.7
_getRadioButtonHTML
20.0
CRAP 319.1
↑ Summary blib/lib/WWW/FieldValidator.pm »
1 package WWW::Form;
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  
6 9 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 sub new {
12 8 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
13  
14 # Hash that contains various bits of data in regard to the form fields,
15 # i.e. the form field's label, its input type (e.g. radio, text, textarea,
16 # select, etc.) validators to check the user entered input against a
17 # default value to use before the form is submitted and an option group
18 # hash if the type of the form input is select or radio this hash should
19 # be keyed with the values you want to use for the name attributes of your
20 # form inputs
21 8 my $fieldsData = shift;
22  
23 # Values to populate value keys of field hashes with generally this will
24 # be a hash of HTTP params needs to have the same keys as fieldsData
25 8 ▶ my $fieldValues = shift || {};
Condition100%2/2
shift() || {}
l!l
71
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
26  
27 # Array ref of field name keys that should be in the order that you want
28 # to display your form inputs
29 8 ▶ my $fieldsOrder = shift || [];
Condition100%2/2
shift() || []
l!l
35
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
30  
31 8 my $self = {};
32  
33 8 $self->{fieldsOrder} = $fieldsOrder;
34  
35 8 bless($self, $class);
36  
37 # Set up a fields hash ref for the fields, so we will not need
38 # autovivificatiopn later
39 8 $self->{fields} = {};
40  
41 # Creates and populates fields hash
42 8 $self->_setFields($fieldsData, $fieldValues);
43  
44 8 return $self;
45 }
46  
47  
48  
49 sub validateFields {
50 0 ▶ my $self = shift;
Subroutine validateFields: not called CC=6 SCAR=37.4
Pod documented
51  
52 # Initialize hash of valid fields
53 0 my %validFields = ();
54  
55 # Init isValid property to 1 that is, the form starts out as being valid
56 # until an invalid field is found, at which point the form gets set to
57 # invalid (i.e., $self->{isValid} = 0)
58 0 $self->{isValid} = 1;
59  
60 # Go through all the fields and look to see if they have any validators,
61 # if so check the validators to see if the input is valid, if the field
62 # has no validators then the field is always valid
63 0 foreach my $fieldName (keys %{$self->{fields}}) {
64  
65 # Look up hash ref of data for the current field name
66 0 my $field = $self->getField($fieldName);
67  
68 0 my $fieldValue = $self->getFieldValue($fieldName);
69  
70 # If this field has any validators, run them
71 0 ▶ if (scalar(@{$field->{validators}}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$field->{'validators'};} > 0) { } 0 0 0
72  
73 # Keeps track of how many validators pass
74 0 my $validValidators = 0;
75  
76 # Check the field's validator(s) to see if the user input is valid
77 0 foreach my $validator (@{$field->{validators}}) {
78  
79 0 ▶ if ($validator->validate($fieldValue)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator->validate($fieldValue)) { } 0 0 0
80 # Increment the validator counter because the current
81 # validator passed, i.e. the form input was good
82 0 $validValidators++;
83 }
84 else {
85 # Mark field as invalid so error feedback can be displayed
86 # to the user
87 0 $field->{isValid} = 0;
88  
89 # Mark form as invalid because at least one input is not
90 # valid
91 0 $self->{isValid} = 0;
92  
93 # Add the validators feedback to the array of feedback for
94 # this field
95 0 push @{$field->{feedback}}, $validator->{feedback};
96 }
97 }
98  
99 # Only set the field to valid if ALL of the validators pass
100 0 ▶ if (scalar(@{$field->{validators}}) == $validValidators) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$field->{"validators"};} == $validValidators) 0 0 0
101 0 $field->{isValid} = 1;
102 0 $validFields{$fieldName} = $fieldValue;
103 }
104 }
105 else {
106 # This field didn't have any validators so it's ok
107 0 $field->{isValid} = 1;
108 0 $validFields{$fieldName} = $fieldValue;
109 }
110 }
111  
112 # Return hash ref of valid fields
113 0 return \%validFields;
114 }
115  
116  
117 9 *validate_fields = \&validateFields;
118  
119  
120  
121 sub getFields {
122 0 ▶ my $self = shift;
Subroutine getFields: not called CC=1 SCAR=6.9
Pod documented
123 0 return $self->{fields};
124 }
125  
126  
127 9 *get_fields = \&getFields;
128  
129  
130  
131 sub resetFields {
132 0 ▶ my ($self, %args) = @_;
Subroutine resetFields: not called CC=3 SCAR=24.8
Pod documented
133 0 my $fields = $self->getFields();
134  
135 0 for my $fieldName (keys %$fields) {
136 0 $self->setFieldValue($fieldName, '');
137  
138 $self->getField($fieldName)->{defaultValue} = ''
139 0 ▶ if ($args{include_defaults});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"include_defaults"} 0 0 0
140 }
141 }
142  
143  
144 9 *reset_fields = \&resetFields;
145  
146  
147  
148 sub getField {
149 67 ▶ my $self = shift;
Subroutine getField: called CC=1 SCAR=0.0
Pod documented
150 67 my $fieldName = shift;
151 67 return $self->{fields}{$fieldName};
152 }
153  
154  
155 9 *get_field = \&getField;
156  
157  
158  
159 sub getFieldErrorFeedback {
160 4 ▶ my $self = shift;
Subroutine getFieldErrorFeedback: called CC=2 SCAR=7.1
Pod documented
161 4 my $fieldName = shift;
162  
163 4 my $field = $self->getField($fieldName);
164  
165 4 ▶ if ($field->{feedback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{'feedback'}) { } 4 0 4
166 4 return @{$field->{feedback}};
167 }
168 else {
169 0 return ();
170 }
171 }
172  
173  
174 9 *get_field_error_feedback = \&getFieldErrorFeedback;
175  
176  
177  
178 sub getFieldsOrder {
179 4 ▶ my $self = shift;
Subroutine getFieldsOrder: called CC=1 SCAR=0.0
Pod documented
180 4 return $self->{fieldsOrder};
181 }
182  
183  
184 9 *get_fields_order = \&getFieldsOrder;
185  
186  
187  
188 sub getFieldValue {
189 2 ▶ my $self = shift;
Subroutine getFieldValue: called CC=1 SCAR=0.0
Pod documented
190 2 my $fieldName = shift;
191 2 return $self->getField($fieldName)->{value};
192 }
193  
194  
195 9 *get_field_value = \&getFieldValue;
196  
197  
198  
199 sub isFieldValid {
200 0 ▶ my $self = shift;
Subroutine isFieldValid: not called CC=1 SCAR=6.9
Pod documented
201 0 my $fieldName = shift;
202  
203 0 return $self->getField($fieldName)->{isValid};
204 }
205  
206  
207 9 *is_field_valid = \&isFieldValid;
208  
209  
210  
211 sub getFieldValidators {
212 0 ▶ my ($self, $fieldName) = @_;
Subroutine getFieldValidators: not called CC=1 SCAR=6.9
Pod documented
213 0 return $self->getField($fieldName)->{validators};
214 }
215  
216  
217 9 *get_field_validators = \&getFieldValidators;
218  
219  
220  
221 sub getFieldType {
222 4 ▶ my $self = shift;
Subroutine getFieldType: called CC=1 SCAR=0.0
Pod documented
223 4 my $fieldName = shift;
224 4 return $self->getField($fieldName)->{type};
225 }
226  
227  
228 9 *get_field_type = \&getFieldType;
229  
230  
231  
232 sub getFieldLabel {
233 4 ▶ my $self = shift;
Subroutine getFieldLabel: called CC=2 SCAR=7.2
Pod documented
234 4 my $fieldName = shift;
235  
236 4 my $field = $self->getField($fieldName);
237  
238 4 ▶ if ($self->getFieldType($fieldName) eq 'checkbox') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getFieldType($fieldName) eq 'checkbox') { } 0 4 4
239 0 return "<label for='$fieldName'>" . $field->{label} . '</label>';
240 }
241 else {
242 4 return $field->{label};
243 }
244 }
245  
246  
247 9 *get_field_label = \&getFieldLabel;
248  
249  
250  
251 sub getFieldHint {
252 4 ▶ my $self = shift;
Subroutine getFieldHint: called CC=1 SCAR=0.0
Pod documented
253 4 my $fieldName = shift;
254  
255 4 my $field = $self->getField($fieldName);
256  
257 4 return $field->{hint};
258 }
259  
260  
261 9 *get_field_hint = \&getFieldHint;
262  
263  
264  
265 sub setFieldValue {
266 0 ▶ my $self = shift;
Subroutine setFieldValue: not called CC=2 SCAR=17.9
Pod documented
267 0 my $fieldName = shift;
268 0 my $newValue = shift;
269  
270 0 ▶ if (my $field = $self->getField($fieldName)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $self->getField($fieldName)) { } 0 0 0
271 0 $field->{value} = $newValue;
272 #warn("set field value for field: $fieldName to '$new_value'");
273 }
274 else {
275 #warn("could not find field for field name: '$fieldName'");
276 }
277 }
278  
279  
280 9 *set_field_value = \&setFieldValue;
281  
282  
283  
284 sub isValid {
285 0 ▶ my $self = shift;
Subroutine isValid: not called CC=1 SCAR=6.9
Pod documented
286 0 return $self->{isValid};
287 }
288  
289  
290 9 *is_valid = \&isValid;
291  
292  
293  
294 sub isSubmitted {
295 0 ▶ my $self = shift;
Subroutine isSubmitted: not called CC=3 SCAR=24.8
Pod documented
296  
297 # The actual HTTP request method that the form was sent using
298 0 my $formRequestMethod = shift;
299  
300 # This should be GET or POST, defaults to POST
301 0 ▶ my $formMethodToCheck = shift || 'POST';
Condition0%0/2
shift() || 'POST'
l!l
00
MC/DC0%0/1
shift() || 'POST'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
302  
303 0 ▶ if ($formRequestMethod eq $formMethodToCheck) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formRequestMethod eq $formMethodToCheck) { } 0 0 0
304 0 return 1;
305 }
306 else {
307 0 return 0;
308 }
309 }
310  
311  
312 9 *is_submitted = \&isSubmitted;
313  
314  
315  
316 # Private method
317 #
318 # Populates fields hash for each field of the form
319 sub _setFields {
320 11 ▶ my $self = shift;
Subroutine _setFields: called CC=2 SCAR=6.9
321 11 my $fieldsData = shift;
322 11 my $fieldValues = shift;
323  
324 # TODO :
325 # Create a _setField() method that will encapsulate the functionality
326 # inside the loop. This will enable adding more variables to each field
327 # in the sub-classes more easily.
328  
329 11 foreach my $fieldName (keys %{$fieldsData}) {
330 $self->_setField(
331 'name' => $fieldName,
332 'params' => $fieldsData->{$fieldName},
333 24 'value' => $fieldValues->{$fieldName}
334 );
335 }
336 }
337  
338 sub _getFieldInitParams
339 {
340 26 ▶ my $self = shift;
Subroutine _getFieldInitParams: called CC=9 SCAR=22.4
341  
342 26 my %args = (@_);
343  
344 26 my $fieldName = $args{name};
345 26 my $params = $args{params};
346 26 my $user_given_field_value = $args{value};
347  
348 # This is the output parameters that we eventually place under
349 # $out_params->. It is declared it so it can later be filled
350 # in by a different function other
351 26 my $out_params = {};
352  
353 # Use the supplied field value if one is given. Generally the supplied
354 # data will be a hash of HTTP POST data
355 26 my $fieldValue = '';
356  
357 # Only use the default value of a check box if the form has been
358 # submitted, that is, the default value should be the value that you
359 # want to show up in the POST data if the checkbox is selected when
360 # the form is submitted
361 26 ▶ if ($params->{type} eq 'checkbox') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'type'} eq 'checkbox') { } 2 24 26
362  
363 # If the checkbox was selected then we're going to use the default
364 # value for the checkbox input's value in our WWW::Form object, if
365 # the checkbox was not selected and the form was submitted that
366 # variable will not show up in the hash of HTTP variables
367 2 ▶ if ($user_given_field_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_given_field_value) 1 1 2
368 1 $fieldValue = $params->{defaultValue};
369 }
370  
371 # See if this checkbox should be checked by default
372 $out_params->{defaultChecked} =
373 2 $params->{defaultChecked};
374 }
375 else {
376 # If a key exists in the $fieldValues hashref, use that value
377 # instead of the default, we generally want to favor displaying
378 # user entered values than defaults
379 24 ▶ if (defined($user_given_field_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_given_field_value) { } 18 6 24
380 18 $fieldValue = $user_given_field_value;
381 }
382 else {
383 6 $fieldValue = $params->{defaultValue};
384 }
385 }
386  
387 # Value suitable for displaying to users as a label for a form input,
388 # e.g. 'Email address', 'Full name', 'Street address', 'Phone number',
389 # etc.
390 26 $out_params->{label} = $params->{label};
391  
392 # Holds the value that the user enters after the form is submitted
393 26 $out_params->{value} = $fieldValue;
394  
395 # The value to pre-populate a form input with before the form is
396 # submitted, the only exception is a checkbox form input in the case
397 # of a checkbox, the default value will be the value of the checkbox
398 # input if the check box is selected and the form is submitted, see
399 # form_test.pl for an example
400 $out_params->{defaultValue} =
401 26 $params->{defaultValue};
402  
403 # The validators for this field, validators are used to test user
404 # entered form input to make sure that it the user entered data is
405 # acceptable
406 $out_params->{validators} =
407 26 \@{$params->{validators}};
408  
409 # Type of the form input, i.e. 'radio', 'text', 'select', 'checkbox',
410 # etc. this is mainly used to determine what type of HTML method
411 # should be used to display the form input in a web page
412 26 $out_params->{type} = $params->{type};
413  
414 # If any validators fail, this property will contain the error
415 # feedback associated with those failing validators
416 26 $out_params->{feedback} = [];
417  
418 # If the input type is a select box or a radio button then we need an
419 # array of labels and values for the radio button group or select box
420 # option groups
421 26 ▶ if (my $optionsGroup = $params->{optionsGroup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $optionsGroup = $params->{"optionsGroup"}) 0 26 26
422 0 $out_params->{optionsGroup} = \@{$optionsGroup};
423 }
424  
425 # Arbitrary HTML attributes that will be used when the field's input
426 # element is displayed.
427 $out_params->{extraAttributes} =
428 26 ▶ ($params->{extraAttributes} || "");
Condition50%1/2
$params->{'extraAttributes'} || ''
l!l
026
MC/DC0%0/1
$params->{'extraAttributes'} || ''
$params->{'extraAttributes'}
Truth table50%1/2
A
A= $params->{'extraAttributes'}
Aresult
11
00
429  
430 # Add the hint
431 # 2004-Jan-04 - Added by Shlomi Fish:
432 # Ben, no. Actually it's a hint that will always be displayed below
433 # the table row to instruct the users what to input there. For instance
434 # +----------+---------------------------+
435 # | City: | [================] |
436 # +----------+---------------------------+
437 # | Input the city in which you live |
438 # | in. |
439 # +---------------------------------------
440 # So "Input the city..." would be the hint.
441 26 ▶ if (my $hint = $params->{hint})
Branch: 1 branch
BranchTrueFalseTotal
if (my $hint = $params->{"hint"}) 3 23 26
442 {
443 3 $out_params->{hint} = $hint;
444 }
445  
446 # Add the container_attributes. These are HTML attributes that would
447 # be added to the rows of this HTML row.
448 26 ▶ if (my $attribs = $params->{container_attributes})
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribs = $params->{"container_attributes"}) 0 26 26
449 {
450 0 $out_params->{container_attributes} = $attribs;
451 }
452  
453 # Add the hint_container_attributes. These are HTML attributes that
454 # would be added to the Hint row of this HTML row.
455 26 ▶ if (my $attribs = $params->{hint_container_attributes})
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribs = $params->{"hint_container_attributes"}) 0 26 26
456 {
457 0 $out_params->{hint_container_attributes} = $attribs;
458 }
459  
460 26 return $out_params;
461 }
462  
463 # This function should not be left alone in sub-classing.
464 # Instead override _getFieldInitParams() to add your own parameters
465 # there.
466 sub _setField
467 {
468 25 ▶ my $self = shift;
Subroutine _setField: called CC=1 SCAR=0.0
469  
470 25 my %args = (@_);
471  
472 25 my $params = $self->_getFieldInitParams(%args);
473  
474 25 $self->{fields}{$args{name}} = $params;
475  
476 25 return $self;
477 }
478  
479  
480 sub asString {
481 0 ▶ my $self = shift;
Subroutine asString: not called CC=1 SCAR=6.9
Pod documented
482 0 require Data::Dumper;
483 0 return Data::Dumper::Dumper($self);
484 }
485  
486  
487 9 *as_string = \&asString;
488  
489 sub _getFieldType
490 {
491 19 ▶ my $self = shift;
Subroutine _getFieldType: called CC=1 SCAR=0.0
492 19 my $fieldName = shift;
493  
494 19 return $self->getField($fieldName)->{type};
495 }
496  
497 #-----------------------------------------------------------------------------
498 # Convenience methods for displaying HTML form data including form inputs,
499 # labels, and error feedback
500 #
501 # Note: You do not need to use these methods to display your form inputs, but
502 # they should be reasonably flexible enough to handle most cases
503 #-----------------------------------------------------------------------------
504  
505  
506 sub getFieldFormInputHTML {
507 4 ▶ my $self = shift;
Subroutine getFieldFormInputHTML: called CC=7 SCAR=22.4
Pod documented
508  
509 # The value of the HTML name attribute of the form field
510 4 my $fieldName = shift;
511  
512 # A string that can contain an arbitrary number of HTML attribute
513 # name=value pairs, this lets you apply CSS classes to form inputs
514 # or control the size of your text inputs, for example
515 4 ▶ my $attributesString = shift || '';
Condition50%1/2
shift() || ''
l!l
04
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
516  
517 4 my $type = $self->_getFieldType($fieldName);
518  
519 4 ▶ if ($type =~ /text$|password|hidden|file/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type =~ /text$|password|hidden|file/u) { } 2 2 4
elsif ($type eq 'checkbox') { } 0 2 2
elsif ($type eq 'radio') { } 0 2 2
elsif ($type eq 'select') { } 0 2 2
elsif ($type eq 'textarea') { } 2 0 2
520  
521 2 return $self->_getInputHTML($fieldName, $attributesString);
522  
523 }
524 elsif ($type eq 'checkbox') {
525  
526 0 return $self->_getCheckBoxHTML($fieldName, $attributesString);
527  
528 }
529 elsif ($type eq 'radio') {
530  
531 0 return $self->_getRadioButtonHTML($fieldName, $attributesString);
532  
533 }
534 elsif ($type eq 'select') {
535  
536 0 return $self->_getSelectBoxHTML($fieldName, $attributesString);
537  
538 }
539 elsif ($type eq 'textarea') {
540  
541 2 return $self->_getTextAreaHTML($fieldName, $attributesString);
542 }
543 }
544  
545  
546 9 *get_field_form_input_HTML = \&getFieldFormInputHTML;
547  
548  
549  
550 sub getFieldLabelTdHTML
551 {
552 4 ▶ return "<td>";
Subroutine getFieldLabelTdHTML: called CC=1 SCAR=0.0
Pod documented
553 }
554  
555  
556 sub getFieldInputTdHTML
557 {
558 4 ▶ return "<td>";
Subroutine getFieldInputTdHTML: called CC=1 SCAR=0.0
Pod documented
559 }
560  
561  
562 sub renderFieldHTMLRow
563 {
564 4 ▶ my $self = shift;
Subroutine renderFieldHTMLRow: called CC=1 SCAR=0.0
Pod documented
565 4 my (%args) = (@_);
566 4 my $fieldName = $args{'fieldName'};
567 4 my $attributesString = $args{'attributesString'};
568 4 my $tr_attr_string = $args{'trAttrString'};
569 return
570 4 "<tr${tr_attr_string}>" . $self->getFieldLabelTdHTML($fieldName) .
571 $self->getFieldLabel($fieldName) . "</td>" .
572 $self->getFieldInputTdHTML($fieldName) . $self->getFieldFormInputHTML(
573 $fieldName,
574 $attributesString
575 )
576 . "</td></tr>\n";
577 }
578  
579  
580 sub renderHintHTMLRow
581 {
582 4 ▶ my $self = shift;
Subroutine renderHintHTMLRow: called CC=4 SCAR=20.1
Pod documented
583 4 my $fieldName = shift;
584 4 my (%func_args) = (@_);
585  
586 4 my $field = $self->getField($fieldName);
587  
588 4 my $tr_attributes = $self->_getTrAttributes($fieldName);
589  
590 4 my $form_args = $func_args{'form_args'};
591  
592 4 my $hint = $self->getFieldHint($fieldName);
593  
594 4 ▶ if (defined($hint)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hint) { } 0 4 4
595 0 my %hint_attributes = ();
596 0 my $hint_attributes = $form_args->{'hint_container_attributes'};
597  
598 0 ▶ if (defined($hint_attributes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hint_attributes) 0 0 0
599 0 %hint_attributes = (%hint_attributes, %$hint_attributes);
600 }
601  
602 0 %hint_attributes = (%hint_attributes, %$tr_attributes);
603  
604 0 ▶ if (exists($field->{hint_container_attributes})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"hint_container_attributes"}) 0 0 0
605 0 %hint_attributes = (%hint_attributes, %{$field->{hint_container_attributes}});
606 }
607  
608 0 my $hint_attr_string = $self->_render_attributes(\%hint_attributes);
609 0 return "<tr${hint_attr_string}><td colspan=\"2\">$hint</td></tr>\n";
610 }
611 else
612 {
613 4 return "";
614 }
615 }
616  
617 sub _getTrAttributes
618 {
619 8 ▶ my $self = shift;
Subroutine _getTrAttributes: called CC=2 SCAR=7.5
620 8 my $fieldName = shift;
621  
622 8 my %tr_attributes = ();
623  
624 8 my $field = $self->getField($fieldName);
625  
626 8 ▶ if (exists($field->{container_attributes})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"container_attributes"}) 0 8 8
627 0 %tr_attributes = (%tr_attributes, %{$field->{container_attributes}});
628 }
629 8 return \%tr_attributes;
630 }
631  
632 sub _render_attributes {
633 8 ▶ my $self = shift;
Subroutine _render_attributes: called CC=1 SCAR=0.0
634 8 my $attribs = shift;
635  
636 # We sort the keys to produce reproducible output on perl 5.8.1 and above
637 # where the order of the hash keys is not deterministic
638 return join("",
639 6 map { " $_=\"" . $self->_escapeValue($attribs->{$_}) . "\"" }
640 8 (sort {$a cmp $b} keys(%$attribs))
641 );
642 }
643  
644 sub _getTrAttrString
645 {
646 4 ▶ my $self = shift;
Subroutine _getTrAttrString: called CC=1 SCAR=0.0
647 4 my $fieldName = shift;
648 4 return $self->_render_attributes($self->_getTrAttributes($fieldName));
649 }
650  
651  
652 sub getFieldHTMLRow {
653 5 ▶ my $self = shift;
Subroutine getFieldHTMLRow: called CC=4 SCAR=14.0
Pod documented
654 5 my $fieldName = shift;
655  
656 5 ▶ if ($self->_getFieldType($fieldName) eq "hidden")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_getFieldType($fieldName) eq "hidden") 1 4 5
657 {
658 1 return $self->_getHiddenFieldHTMLRow($fieldName);
659 }
660  
661 4 my %func_args = (@_);
662 4 my $attributesString = $func_args{'attributesString'};
663 4 my $form_args = $func_args{'form_args'};
664  
665 4 my $field = $self->getField($fieldName);
666  
667 4 ▶ $attributesString ||= $field->{extraAttributes};
Condition33%1/3
$attributesString ||= $field->{"extraAttributes"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$attributesString ||= $field->{"extraAttributes"}
$attributesString $field->{"extraAttributes"}
Truth table33%1/3
A ||= B
A= $attributesStringB= $field->{"extraAttributes"}
ABresult
1X1
011
000
668  
669 4 my @feedback = $self->getFieldErrorFeedback($fieldName);
670  
671 4 my $html = "";
672  
673 4 my $tr_attr_string = $self->_getTrAttrString($fieldName);
674  
675 4 foreach my $error (@feedback) {
676 0 $html .= "<tr${tr_attr_string}><td colspan='2'>"
677 . "<span style='color: #ff3300'>$error</span>"
678 . "</td></tr>\n";
679 }
680  
681 4 $html .= $self->renderFieldHTMLRow(
682 'fieldName' => $fieldName,
683 'attributesString' => $attributesString,
684 'trAttrString' => $tr_attr_string,
685 );
686  
687 4 $html .=
688 $self->renderHintHTMLRow(
689 $fieldName,
690 'form_args' => $form_args,
691 );
692  
693 4 return $html;
694 }
695  
696  
697 9 *get_field_HTML_row = \&getFieldHTMLRow;
698  
699  
700 sub getFieldHTMLRowNoHidden
701 {
702 5 ▶ my $self = shift;
Subroutine getFieldHTMLRowNoHidden: called CC=2 SCAR=6.9
Pod documented
703 5 my $fieldName = shift;
704  
705 5 ▶ if ($self->_getFieldType($fieldName) eq "hidden")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_getFieldType($fieldName) eq 'hidden') { } 1 4 5
706 {
707 1 return "";
708 }
709 else
710 {
711 4 return $self->getFieldHTMLRow($fieldName);
712 }
713 }
714  
715  
716 9 *get_field_HTML_row_no_hidden = \&getFieldHTMLRowNoHidden;
717  
718  
719 sub getFieldFeedbackHTML {
720 0 ▶ my $self = shift;
Subroutine getFieldFeedbackHTML: not called CC=2 SCAR=17.9
Pod documented
721 0 my $fieldName = shift;
722  
723 0 my @feedback = $self->getFieldErrorFeedback($fieldName);
724  
725 0 my $feedbackHTML = '';
726  
727 0 foreach my $fieldFeedback (@feedback) {
728 0 $feedbackHTML .= "<div class='feedback'>\n";
729 0 $feedbackHTML .= $fieldFeedback . "\n</div>\n";
730 }
731  
732 0 return $feedbackHTML;
733 }
734  
735  
736 9 *get_field_feedback_HTML = \&getFieldFeedbackHTML;
737  
738  
739  
740 sub startForm {
741 2 ▶ my ($self, %args) = @_;
Subroutine startForm: called CC=6 SCAR=19.7
Pod documented
742  
743 2 ▶ my $method = $args{method} || 'post';
Condition50%1/2
$args{'method'} || 'post'
l!l
02
MC/DC0%0/1
$args{'method'} || 'post'
$args{'method'}
Truth table50%1/2
A
A= $args{'method'}
Aresult
11
00
744 2 ▶ my $attributes = $args{attributes} || {};
Condition50%1/2
$args{'attributes'} || {}
l!l
02
MC/DC0%0/1
$args{'attributes'} || {}
$args{'attributes'}
Truth table50%1/2
A
A= $args{'attributes'}
Aresult
11
00
745  
746 2 my $name_attributes = '';
747 2 ▶ if ($args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"name"}) 0 2 2
748 0 $name_attributes = " name='$args{name}' id='$args{name}'";
749 }
750  
751 2 my $html = "<form action='$args{action}'"
752 . " method='$method'$name_attributes";
753  
754 # If this form contains a file input then set the enctype attribute
755 # to multipart/form-data
756 2 ▶ if ($args{is_file_upload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"is_file_upload"}) 0 2 2
757 0 $html .= " enctype='multipart/form-data'";
758 }
759  
760 2 for my $attribute (keys %{$attributes}) {
761 0 $html .= " $attribute='$attributes->{$attribute}'";
762 }
763  
764 # Chop off last space if there is one
765 2 $html =~ s/\s$//;
766  
767 2 return $html . '>';
768 }
769  
770  
771 9 *start_form = \&startForm;
772  
773  
774  
775 sub endForm {
776 2 ▶ my $self = shift;
Subroutine endForm: called CC=1 SCAR=0.0
Pod documented
777 2 return '</form>';
778 }
779  
780  
781 9 *end_form = \&endForm;
782  
783  
784  
785 sub getFormHTML {
786 2 ▶ my ($self, %args) = @_;
Subroutine getFormHTML: called CC=4 SCAR=13.9
Pod documented
787  
788 2 my $html = $self->startForm(%args) . "\n";
789  
790 2 $html .= $self->getHiddenFieldsHTML();
791 2 $html .= "<table>\n";
792  
793 # Go through all of our form fields and build an HTML input for each field
794 2 for my $fieldName (@{$self->getFieldsOrder()}) {
795 #warn("field name is: $fieldName");
796 5 $html .= $self->getFieldHTMLRowNoHidden(
797 $fieldName,
798 'form_args' => \%args,
799 );
800 }
801  
802 2 $html .= "</table>\n";
803  
804 2 ▶ unless ($args{submit_label}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"submit_label"}) 2 0 2
805 2 $args{submit_label} = 'Submit';
806 }
807  
808 2 ▶ unless ($args{submit_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"submit_name"}) 2 0 2
809 2 $args{submit_name} = 'submit';
810 }
811  
812 # Add submit button
813 2 $html .= "<p>" . $self->_getSubmitButtonHTML(%args) . "</p>\n";
814  
815 2 return $html . $self->endForm() . "\n";
816 }
817  
818  
819 9 *get_form_HTML = \&getFormHTML;
820  
821  
822 sub getHiddenFieldsHTML
823 {
824 2 ▶ my $self = shift;
Subroutine getHiddenFieldsHTML: called CC=1 SCAR=0.0
Pod documented
825  
826 return
827 join("",
828 1 (map { $self->_getInputHTML($_, "") . "\n" }
829 5 grep { $self->_getFieldType($_) eq "hidden" }
830 2 (@{$self->getFieldsOrder()}))
831 );
832 }
833  
834  
835 9 *get_hidden_fields_HTML = \&getHiddenFieldsHTML;
836  
837 sub _getHiddenFieldHTMLRow
838 {
839 1 ▶ my $self = shift;
Subroutine _getHiddenFieldHTMLRow: called CC=1 SCAR=0.0
840 1 my $fieldName = shift;
841 1 return "<tr style=\"display:none\">\n" .
842 "<td></td>\n" .
843 "<td>" . $self->_getInputHTML($fieldName, "") ."</td>\n" .
844 "</tr>\n";
845 }
846  
847 #-----------------------------------------------------------------------------
848 # More private methods
849 #-----------------------------------------------------------------------------
850  
851 # Returns HTML to display a form text input.
852 sub _getInputHTML {
853 8 ▶ my $self = shift;
Subroutine _getInputHTML: called CC=2 SCAR=6.9
854 8 my $fieldName = shift;
855 8 my $attributesString = shift;
856  
857 8 my $field = $self->getField($fieldName);
858  
859 8 my $inputHTML = "<input type='$field->{type}'"
860 . " name='$fieldName' id='$fieldName' value=\"";
861  
862 8 my $value_to_put;
863 8 ▶ if ($field->{type} eq 'checkbox') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{'type'} eq 'checkbox') { } 2 6 8
864 2 $value_to_put = $field->{defaultValue};
865 }
866 else {
867 6 $value_to_put = $field->{value};
868 }
869 8 $inputHTML .= $self->_escapeValue($value_to_put);
870  
871 8 $inputHTML .= "\"" . $attributesString . " />";
872  
873 8 return $inputHTML;
874 }
875  
876  
877  
878 sub getSubmitButtonHTML {
879 2 ▶ my ($class, %args) = @_;
Subroutine getSubmitButtonHTML: called CC=12 SCAR=32.5
Pod documented
880  
881 2 ▶ if (exists($args{buttons})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"buttons"}) 0 2 2
882 0 my $xhtml;
883 0 foreach my $button (@{$args{buttons}}) {
884 0 $xhtml .= $class->_getSubmitButtonHTML(%$button);
885 }
886 0 return $xhtml;
887 }
888  
889 2 ▶ my $type = $args{submit_type} || 'submit';
Condition50%1/2
$args{'submit_type'} || 'submit'
l!l
02
MC/DC0%0/1
$args{'submit_type'} || 'submit'
$args{'submit_type'}
Truth table50%1/2
A
A= $args{'submit_type'}
Aresult
11
00
890  
891 # Optional param that specifies an image for the submit button, this
892 # should only be used if the type is 'image'
893 2 ▶ my $img_src = $args{submit_src} || '';
Condition50%1/2
$args{'submit_src'} || ''
l!l
02
MC/DC0%0/1
$args{'submit_src'} || ''
$args{'submit_src'}
Truth table50%1/2
A
A= $args{'submit_src'}
Aresult
11
00
894  
895 2 ▶ my $label = $args{submit_label} || 'Submit';
Condition50%1/2
$args{'submit_label'} || 'Submit'
l!l
20
MC/DC0%0/1
$args{'submit_label'} || 'Submit'
$args{'submit_label'}
Truth table50%1/2
A
A= $args{'submit_label'}
Aresult
11
00
896  
897 2 my $xhtml = "<input type='$type'";
898  
899 # If the type was specified as 'image' add the src attribute, otherwise
900 # add a value attribute
901 2 ▶ if ($type eq 'image') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'image') { } 0 2 2
902 # Warn the developer if type is 'image' and a src key wasn't specified
903 0 ▶ unless ($img_src) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($img_src) 0 0 0
904 0 warn(
905 "Won't be able to display image submit button properly" .
906 " because src for image was not specified"
907 );
908 }
909  
910 0 $xhtml .= " src='$img_src'";
911 }
912 else {
913 2 $xhtml .= " value='$label'";
914 }
915  
916 2 ▶ my $attributes = $args{submit_attributes} || {};
Condition50%1/2
$args{'submit_attributes'} || {}
l!l
02
MC/DC0%0/1
$args{'submit_attributes'} || {}
$args{'submit_attributes'}
Truth table50%1/2
A
A= $args{'submit_attributes'}
Aresult
11
00
917  
918 2 ▶ if ($args{submit_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"submit_class"}) 0 2 2
919 # Add class attribute if it's there
920 0 $xhtml .= " class='$args{submit_class}'";
921 # Add id attribute that uses same value as class, eventually should
922 # use separate params, though!
923 0 $xhtml .= " id='$args{submit_class}'";
924 }
925  
926 2 ▶ if ($args{submit_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"submit_name"}) 2 0 2
927 2 $xhtml .= " name='$args{submit_name}'";
928  
929 }
930  
931 # Add any other attribute name value pairs that the developer may want to
932 # enter
933 2 for my $attribute (keys %{$attributes}) {
934 0 $xhtml .= " $attribute='$attributes->{$attribute}'";
935 }
936  
937 2 $xhtml =~ s/\s$//; # Remove trailing whitespace
938 2 $xhtml .= " />\n";
939 2 return $xhtml;
940 }
941  
942  
943 # We have lots of names for this method. It used to be private, but now it's
944 # public.
945 9 *_get_submit_button_HTML = \&getSubmitButtonHTML;
946  
947  
948 9 *get_submit_button_HTML = \&getSubmitButtonHTML;
949 9 *_getSubmitButtonHTML = \&getSubmitButtonHTML;
950  
951  
952 # Returns HTML to display a checkbox.
953 sub _getCheckBoxHTML {
954 2 ▶ my $self = shift;
Subroutine _getCheckBoxHTML: called CC=3 SCAR=11.0
955 2 my $fieldName = shift;
956 2 my $attributesString = shift;
957  
958 2 my $field = $self->getField($fieldName);
959  
960 2 ▶ if ($self->getFieldValue($fieldName) || $field->{defaultChecked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getFieldValue($fieldName) or $field->{"defaultChecked"}) 1 1 2
Condition66%2/3
$self->getFieldValue($fieldName) or $field->{"defaultChecked"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$self->getFieldValue($fieldName) or $field->{"defaultChecked"}
$self->getFieldValue($fieldName) $field->{"defaultChecked"}
Truth table66%2/3
A or B
A= $self->getFieldValue($fieldName)B= $field->{"defaultChecked"}
ABresult
1X1
011
000
961 1 $attributesString .= " checked='checked'";
962 }
963  
964 2 return $self->_getInputHTML($fieldName, $attributesString);
965 }
966  
967 # Returns a radio button group
968 sub _getRadioButtonHTML {
969 0 ▶ my $self = shift;
Subroutine _getRadioButtonHTML: not called CC=4 SCAR=30.0
970 0 my $fieldName = shift;
971 0 my $attributesString = shift;
972  
973 0 my $field = $self->getField($fieldName);
974  
975 # Get the select boxes' list of options
976 0 my $group = $field->{optionsGroup};
977  
978 0 my $inputHTML = '';
979  
980 0 ▶ if ($group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) { } 0 0 0
981 0 foreach my $option (@{$group}) {
982 0 $inputHTML .= '<label>';
983  
984 # Reset for each radio button in the group
985 0 my $isChecked = '';
986  
987 0 my $value = $option->{value};
988 0 my $label = $option->{label};
989  
990 0 ▶ if ($value eq $self->getFieldValue($fieldName)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $self->getFieldValue($fieldName)) 0 0 0
991 0 $isChecked = " checked='checked'";
992 }
993  
994 0 $inputHTML .= "<input type='$field->{type}'"
995 . " name='$fieldName'";
996  
997 0 $inputHTML .= " value=\"". $self->_escapeValue($value) . "\" ";
998 0 $inputHTML .= $attributesString
999 . $isChecked
1000 . " /> $label</label><br />";
1001 }
1002 }
1003 else {
1004 0 warn(
1005 "No option group found for radio button group named: '$fieldName'"
1006 );
1007 }
1008 0 return $inputHTML;
1009 }
1010  
1011 # Returns HTML to display a textarea.
1012 sub _getTextAreaHTML {
1013 3 ▶ my $self = shift;
Subroutine _getTextAreaHTML: called CC=1 SCAR=0.0
1014 3 my $fieldName = shift;
1015 3 my $attributesString = shift;
1016  
1017 3 my $field = $self->getField($fieldName);
1018  
1019 3 my $textarea = "<textarea name='" . $fieldName . "'"
1020 . $attributesString;
1021  
1022 3 $textarea .= ">";
1023 3 $textarea .= $self->_escapeValue($field->{value});
1024 3 $textarea .= "</textarea>";
1025  
1026 3 return $textarea;
1027 }
1028  
1029 # Returns HTML to display a select box.
1030 sub _getSelectBoxHTML {
1031 0 ▶ my $self = shift;
Subroutine _getSelectBoxHTML: not called CC=4 SCAR=30.0
1032 0 my $fieldName = shift;
1033 0 my $attributesString = shift;
1034  
1035 0 my $html = "<select name='$fieldName'" . "$attributesString>\n";
1036  
1037 # Get the select boxes' list of options
1038 0 my $group = $self->getField($fieldName)->{optionsGroup};
1039  
1040 0 ▶ if ($group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) { } 0 0 0
1041 0 foreach my $option (@{$group}) {
1042 0 my $value = $option->{value};
1043 0 my $label = $option->{label};
1044  
1045 # If the current user value is equal to the current option value
1046 # then the current option should be selected in the form
1047 0 my $isSelected;
1048  
1049 0 ▶ if ($value eq $self->getField($fieldName)->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $self->getField($fieldName)->{'value'}) { } 0 0 0
1050 0 $isSelected = " selected='selected'";
1051 }
1052 else {
1053 0 $isSelected = "";
1054 }
1055 0 $html .= "<option value=\"" . $self->_escapeValue($value)
1056 . "\"${isSelected}>$label</option>\n";
1057 }
1058 }
1059 else {
1060 0 warn("No option group found for select box named: '$fieldName'");
1061 }
1062  
1063 0 $html .= "</select>\n";
1064 0 return $html;
1065 }
1066  
1067 sub _escapeValue {
1068 17 ▶ my $self = shift;
Subroutine _escapeValue: called CC=1 SCAR=0.0
1069 17 my $string = shift;
1070 17 return HTML::Entities::encode_entities($string);
1071 }
1072  
1073 9 1;
1074  
1075 __END__
↑ Summary blib/lib/WWW/FieldValidator.pm »