########################################################################### # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. # Please visit http://formbuilder.org for tutorials, support, and examples. ########################################################################### # The majority of this module's methods (including new) are # inherited directly from ::base, since they involve things # which are common, such as parameter parsing. The only methods # that are individual to different fields are those that affect # the rendering, such as script() and tag() package CGI::FormBuilder::Field::select; use strict; use warnings; no warnings 'uninitialized'; use CGI::FormBuilder::Util; use CGI::FormBuilder::Field; use base 'CGI::FormBuilder::Field'; our $VERSION = '3.10'; sub script { my $self = shift; my $name = $self->name; # The way script() works is slightly backwards: First the # type-specific JS DOM code is generated, then this is # passed as a string to Field->jsfield, which wraps this # in the generic handling. # Holders for different parts of JS code my $jsfunc = ''; my $jsfield = tovar($name); my $close_brace = ''; my $in = indent(my $idt = 1); # indent my $alertstr = escapejs($self->jsmessage); # handle embedded ' $alertstr .= '\n'; # Get value for field from select list # Always assume it's multiple to guarantee we get all values $jsfunc .= <other) { my $oth = $self->othername; $jsfunc .= <required; if (! selected_$jsfield) { alertstr += '$alertstr'; invalid++; } EOJS # indent the very last if/else tests so they're in the for loop $in = indent($idt += 2); return $self->jsfield($jsfunc, $close_brace, $in); } *render = \&tag; sub tag { local $^W = 0; # -w sucks my $self = shift; my $attr = $self->attr; my $jspre = $self->{_form}->jsprefix; my $tag = ''; my @value = $self->tag_value; # sticky is different in my @opt = $self->options; debug 2, "my(@opt) = \$field->options"; # Add in our "Other:" option if applicable push @opt, [$self->othername, $self->{_form}{messages}->form_other_default] if $self->other; debug 2, "$self->{name}: generating $attr->{type} input type"; # First the top-level select delete $attr->{type}; # type="select" invalid $self->multiple ? $attr->{multiple} = 'multiple' : delete $attr->{multiple}; belch "$self->{name}: No options specified for 'select' field" unless @opt; # Prefix options with "-select-", unless selectname => 0 if ($self->{_form}->smartness && ! $attr->{multiple} # set above && $self->selectname ne 0) { # Use selectname if => "choose" or messages otherwise my $name = $self->selectname =~ /\D+/ ? $self->selectname : $self->{_form}{messages}->form_select_default; unshift @opt, ['', $name] } # Special event handling for our _other field if ($self->other && $self->javascript) { my $b = $self->othername; # box # w/o newlines $attr->{onchange} .= "if (this.selectedIndex + 1 == this.options.length) { " . "${jspre}other_on('$b') } else { ${jspre}other_off('$b') }"; } # render '; # add an additional tag for our _other field $tag .= ' ' . $self->othertag if $self->other; debug 2, "$self->{name}: generated tag = $tag"; return $tag; # always return scalar tag } 1; __END__