| 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 | # static fields are a special FormBuilder type that turns any | ||
| 8 | # normal field into a hidden field with the value printed. | ||
| 9 | # As such, the code has to basically handle all field types. | ||
| 10 | |||
| 11 | package CGI::FormBuilder::Field::static; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | no warnings 'uninitialized'; | ||
| 16 | |||
| 17 | use CGI::FormBuilder::Util; | ||
| 18 | use CGI::FormBuilder::Field; | ||
| 19 | use base 'CGI::FormBuilder::Field'; | ||
| 20 | |||
| 21 | |||
| 22 | our $VERSION = '3.10'; | ||
| 23 | |||
| 24 | sub script { | ||
| 25 | return ''; # static fields get no messages | ||
| 26 | } | ||
| 27 | |||
| 28 | *render = \&tag; | ||
| 29 | sub tag { | ||
| 30 | local $^W = 0; # -w sucks | ||
| 31 | my $self = shift; | ||
| 32 | my $attr = $self->attr; | ||
| 33 | |||
| 34 | my $jspre = $self->{_form}->jsprefix; | ||
| 35 | |||
| 36 | my @tag; | ||
| 37 | my @value = $self->tag_value; # sticky is different in <tag> | ||
| 38 | my @opt = $self->options; | ||
| 39 | debug 2, "my(@opt) = \$field->options"; | ||
| 40 | |||
| 41 | # Add in our "Other:" option if applicable | ||
| 42 | push @opt, [$self->othername, $self->{_form}{messages}->form_other_default] | ||
| 43 | if $self->other; | ||
| 44 | |||
| 45 | debug 2, "$self->{name}: generating $attr->{type} input type"; | ||
| 46 | |||
| 47 | # static fields are actually hidden | ||
| 48 | $attr->{type} = 'hidden'; | ||
| 49 | |||
| 50 | # We iterate over each value - this is the only reliable | ||
| 51 | # way to handle multiple form values of the same name | ||
| 52 | # (i.e., multiple <input> or <hidden> fields) | ||
| 53 | @value = (undef) unless @value; # this creates a single-element array | ||
| 54 | |||
| 55 | for my $value (@value) { | ||
| 56 | my $tmp = ''; | ||
| 57 | |||
| 58 | # setup the value | ||
| 59 | $attr->{value} = $value; # override | ||
| 60 | delete $attr->{value} unless defined $value; | ||
| 61 | |||
| 62 | # render the tag | ||
| 63 | $tmp .= htmltag('input', $attr); | ||
| 64 | |||
| 65 | # | ||
| 66 | # If we have options, lookup the label instead of the true value | ||
| 67 | # to print next to the field. This will happen when radio/select | ||
| 68 | # lists are converted to 'static'. | ||
| 69 | # | ||
| 70 | for (@opt) { | ||
| 71 | my($o,$n) = optval($_); | ||
| 72 | if ($o eq $value) { | ||
| 73 | $n ||= $attr->{labels}{$o} || ($self->nameopts ? toname($o) : $o); | ||
| 74 | $value = $n; | ||
| 75 | last; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | # print the value out too when in a static context | ||
| 80 | $tmp .= $self->cleanopts ? escapehtml($value) : $value; | ||
| 81 | push @tag, $tmp; | ||
| 82 | } | ||
| 83 | |||
| 84 | debug 2, "$self->{name}: generated tag = @tag"; | ||
| 85 | return join ' ', @tag; # always return scalar tag | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ |