| lib/CGI/FormBuilder/Field/text.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 65 | 74 | 87.8 |
| branch | 21 | 32 | 65.6 |
| condition | 13 | 24 | 54.1 |
| subroutine | 8 | 8 | 100.0 |
| pod | 1 | 2 | 50.0 |
| total | 108 | 140 | 77.1 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | |||||||
| 2 | ########################################################################### | ||||||
| 3 | # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. | ||||||
| 4 | # Please visit http://formbuilder.org for tutorials, support, and examples. | ||||||
| 5 | ########################################################################### | ||||||
| 6 | |||||||
| 7 | # The majority of this module's methods (including new) are | ||||||
| 8 | # inherited directly from ::base, since they involve things | ||||||
| 9 | # which are common, such as parameter parsing. The only methods | ||||||
| 10 | # that are individual to different fields are those that affect | ||||||
| 11 | # the rendering, such as script() and tag() | ||||||
| 12 | |||||||
| 13 | package CGI::FormBuilder::Field::text; | ||||||
| 14 | |||||||
| 15 | 6 | 6 | 39 | use strict; | |||
| 6 | 12 | ||||||
| 6 | 210 | ||||||
| 16 | 6 | 6 | 40 | use warnings; | |||
| 6 | 12 | ||||||
| 6 | 207 | ||||||
| 17 | 6 | 6 | 29 | no warnings 'uninitialized'; | |||
| 6 | 10 | ||||||
| 6 | 210 | ||||||
| 18 | |||||||
| 19 | 6 | 6 | 31 | use CGI::FormBuilder::Util; | |||
| 6 | 13 | ||||||
| 6 | 1172 | ||||||
| 20 | 6 | 6 | 36 | use CGI::FormBuilder::Field; | |||
| 6 | 12 | ||||||
| 6 | 142 | ||||||
| 21 | 6 | 6 | 30 | use base 'CGI::FormBuilder::Field'; | |||
| 6 | 11 | ||||||
| 6 | 13711 | ||||||
| 22 | |||||||
| 23 | |||||||
| 24 | our $VERSION = '3.09'; | ||||||
| 25 | |||||||
| 26 | sub script { | ||||||
| 27 | 218 | 218 | 0 | 287 | my $self = shift; | ||
| 28 | 218 | 588 | my $name = $self->name; | ||||
| 29 | |||||||
| 30 | # The way script() works is slightly backwards: First the | ||||||
| 31 | # type-specific JS DOM code is generated, then this is | ||||||
| 32 | # passed as a string to Field->jsfield, which wraps this | ||||||
| 33 | # in the generic handling. | ||||||
| 34 | |||||||
| 35 | # Holders for different parts of JS code | ||||||
| 36 | 218 | 434 | my $jsfunc = ''; | ||||
| 37 | 218 | 646 | my $jsfield = tovar($name); | ||||
| 38 | 218 | 339 | my $close_brace = ''; | ||||
| 39 | 218 | 629 | my $in = indent(my $idt = 1); # indent | ||||
| 40 | |||||||
| 41 | 218 | 726 | my $alertstr = escapejs($self->jsmessage); # handle embedded ' | ||||
| 42 | 218 | 383 | $alertstr .= '\n'; | ||||
| 43 | |||||||
| 44 | 218 | 100 | 726 | if ($self->growable) { | |||
| 45 | # special handling for growable, have to dynamically | ||||||
| 46 | # find out how many have been created | ||||||
| 47 | 2 | 10 | $jsfunc .= < | ||||
| 48 | // $name: growable text or file box | ||||||
| 49 | var $jsfield = null; | ||||||
| 50 | var entered_$jsfield = 0; | ||||||
| 51 | var i = 0; | ||||||
| 52 | while (1) { | ||||||
| 53 | var growel = document.getElementById('$jsfield'+'_'+i); | ||||||
| 54 | if (growel == null) break; // last element | ||||||
| 55 | $jsfield = growel.value; | ||||||
| 56 | entered_$jsfield++; | ||||||
| 57 | i++; | ||||||
| 58 | EOJS | ||||||
| 59 | |||||||
| 60 | 2 | 7 | $close_brace = < | ||||
| 61 | |||||||
| 62 | } // while $name | ||||||
| 63 | EOJS | ||||||
| 64 | |||||||
| 65 | # required? | ||||||
| 66 | 2 | 50 | 10 | $close_brace .= < |
|||
| 67 | if (! entered_$jsfield) { | ||||||
| 68 | alertstr += '$alertstr'; | ||||||
| 69 | invalid++; | ||||||
| 70 | } | ||||||
| 71 | EOJS | ||||||
| 72 | # indent the very last if/else tests so they're in the while loop | ||||||
| 73 | 2 | 7 | $in = indent($idt += 1); | ||||
| 74 | |||||||
| 75 | } else { | ||||||
| 76 | |||||||
| 77 | # get value from text or other straight input | ||||||
| 78 | # at least this part makes some sense | ||||||
| 79 | 216 | 755 | $jsfunc .= < | ||||
| 80 | // $name: standard text, hidden, password, or textarea box | ||||||
| 81 | var $jsfield = form.elements['$name'].value; | ||||||
| 82 | EOJS | ||||||
| 83 | |||||||
| 84 | } | ||||||
| 85 | |||||||
| 86 | 218 | 704 | return $self->jsfield($jsfunc, $close_brace, $in); | ||||
| 87 | } | ||||||
| 88 | |||||||
| 89 | *render = \&tag; | ||||||
| 90 | sub tag { | ||||||
| 91 | 240 | 240 | 1 | 880 | local $^W = 0; # -w sucks | ||
| 92 | 240 | 407 | my $self = shift; | ||||
| 93 | 240 | 1881 | my $attr = $self->attr; | ||||
| 94 | |||||||
| 95 | 240 | 1477 | my $jspre = $self->{_form}->jsprefix; | ||||
| 96 | |||||||
| 97 | 240 | 392 | my $tag = ''; | ||||
| 98 | 240 | 727 | my @value = $self->tag_value; # sticky is different in |
||||
| 99 | 240 | 782 | my @opt = $self->options; | ||||
| 100 | 240 | 975 | debug 2, "my(@opt) = \$field->options"; | ||||
| 101 | |||||||
| 102 | # Add in our "Other:" option if applicable | ||||||
| 103 | 240 | 50 | 827 | push @opt, [$self->othername, $self->{_form}{messages}->form_other_default] | |||
| 104 | if $self->other; | ||||||
| 105 | |||||||
| 106 | 240 | 1114 | debug 2, "$self->{name}: generating $attr->{type} input type"; | ||||
| 107 | |||||||
| 108 | # We iterate over each value - this is the only reliable | ||||||
| 109 | # way to handle multiple form values of the same name | ||||||
| 110 | # (i.e., multiple or |
||||||
| 111 | 240 | 100 | 794 | @value = (undef) unless @value; # this creates a single-element array | |||
| 112 | |||||||
| 113 | # growable handling | ||||||
| 114 | 240 | 413 | my $count = 0; # for tracking the size of growable fields | ||||
| 115 | 240 | 297 | my $limit; # for providing (optional) limits to growable fields | ||||
| 116 | my $at_limit; # have we reached the limit of a growable field? | ||||||
| 117 | 240 | 50 | 66 | 889 | if ($self->growable && $self->growable ne 1) { | ||
| 118 | 0 | 0 | $limit = $self->growable; | ||||
| 119 | } | ||||||
| 120 | |||||||
| 121 | 240 | 502 | for my $value (@value) { | ||||
| 122 | 242 | 50 | 33 | 594 | if ($limit && $count == $limit) { | ||
| 123 | 0 | 0 | belch "Number of supplied values (" . @value . ")" | ||||
| 124 | . " for '$attr->{name}' exceeds growable limit $limit - discarding excess"; | ||||||
| 125 | 0 | 0 | $at_limit = 1; | ||||
| 126 | 0 | 0 | last; | ||||
| 127 | } | ||||||
| 128 | |||||||
| 129 | # setup the value | ||||||
| 130 | 242 | 549 | $attr->{value} = $value; # override | ||||
| 131 | 242 | 100 | 635 | delete $attr->{value} unless defined $value; | |||
| 132 | |||||||
| 133 | 242 | 100 | 100 | 756 | if ($self->growable && $self->javascript) { | ||
| 134 | # the inputs in growable fields need a unique id for fb_grow() | ||||||
| 135 | 2 | 12 | $attr->{id} = tovar("$attr->{name}_$count"); | ||||
| 136 | 2 | 5 | $count++; | ||||
| 137 | } | ||||||
| 138 | |||||||
| 139 | # render the tag | ||||||
| 140 | 242 | 744 | $tag .= htmltag('input', $attr); | ||||
| 141 | |||||||
| 142 | # | ||||||
| 143 | # If we have options, lookup the label instead of the true value. | ||||||
| 144 | # This code is reached if a field is marked 'static', since | ||||||
| 145 | # that is still rendered from ::text (here) but as a 'hidden' | ||||||
| 146 | # field. These options would be leftover from a former select | ||||||
| 147 | # or radio group that is now shown on a confirm() screen. Got it? | ||||||
| 148 | # | ||||||
| 149 | 242 | 617 | for (@opt) { | ||||
| 150 | 0 | 0 | my($o,$n) = optval($_); | ||||
| 151 | 0 | 0 | 0 | if ($o eq $value) { | |||
| 152 | 0 | 0 | 0 | $n ||= $attr->{labels}{$o} || ($self->nameopts ? toname($o) : $o); | |||
| 0 | |||||||
| 153 | 0 | 0 | $value = $n; | ||||
| 154 | 0 | 0 | last; | ||||
| 155 | } | ||||||
| 156 | } | ||||||
| 157 | |||||||
| 158 | 242 | 100 | 100 | 780 | if ($self->growable && $self->javascript) { | ||
| 159 | # put linebreaks between the input tags in growable fields | ||||||
| 160 | # this puts the "Additonal [label]" button on the same line | ||||||
| 161 | # as the last input tag | ||||||
| 162 | 2 | 50 | 11 | $tag .= ' ' unless $count == @value; |
|||
| 163 | } else { | ||||||
| 164 | 240 | 50 | 1357 | $tag .= ' ' if $self->linebreaks; |
|||
| 165 | } | ||||||
| 166 | } | ||||||
| 167 | # check to see if we just hit the limit | ||||||
| 168 | 240 | 50 | 33 | 740 | $at_limit = 1 if $limit && $count == $limit; | ||
| 169 | |||||||
| 170 | # add the "Additional [label]" button | ||||||
| 171 | 240 | 100 | 100 | 767 | if ($self->growable && $self->javascript) { | ||
| 172 | 2 | 50 | 17 | $tag .= ' ' . htmltag('input', | |||
| 173 | id => $self->growname, | ||||||
| 174 | type => 'button', | ||||||
| 175 | onclick => "${jspre}grow('$attr->{name}')", | ||||||
| 176 | value => sprintf($self->{_form}{messages}->form_grow_default, $self->label), | ||||||
| 177 | ( $at_limit ? ( disabled => 'disabled') : () ), | ||||||
| 178 | ); | ||||||
| 179 | } | ||||||
| 180 | |||||||
| 181 | # add an additional tag for our _other field | ||||||
| 182 | 240 | 50 | 724 | $tag .= ' ' . $self->othertag if $self->other; | |||
| 183 | |||||||
| 184 | 240 | 1068 | debug 2, "$self->{name}: generated tag = $tag"; | ||||
| 185 | 240 | 2034 | return $tag; # always return scalar tag | ||||
| 186 | } | ||||||
| 187 | |||||||
| 188 | 1; | ||||||
| 189 | |||||||
| 190 | __END__ |