=item $input = $form->find_input( $name )
=item $input = $form->find_input( $name, $type )
=item $input = $form->find_input( $name, $type, $index )
This method is used to locate specific inputs within the form. All
inputs that match the arguments given are returned. In scalar context
only the first is returned, or C<undef> if none match.
If $name is specified, then the input must have the indicated name.
If $type is specified, then the input must have the specified type.
The following type names are used: "text", "password", "hidden",
"textarea", "file", "image", "submit", "radio", "checkbox" and "option".
The $index is the sequence number of the input matched where 1 is the
first. If combined with $name and/or $type then it select the I<n>th
input with the given name and/or type.
=cut
sub find_input
{
my($self, $name, $type, $no) = @_;
if (wantarray) {
my @res;
my $c;
for (@{$self->{'inputs'}}) {
if (defined $name) {
next unless exists $_->{name};
next if $name ne $_->{name};
}
next if $type && $type ne $_->{type};
$c++;
next if $no && $no != $c;
push(@res, $_);
}
return @res;
}
else {
$no ||= 1;
for (@{$self->{'inputs'}}) {
if (defined $name) {
next unless exists $_->{name};
next if $name ne $_->{name};
}
next if $type && $type ne $_->{type};
next if --$no;
return $_;
}
return undef;
}
}
sub fixup
{
my $self = shift;
for (@{$self->{'inputs'}}) {
$_->fixup;
}
}
=item $value = $form->value( $name )
=item $form->value( $name, $new_value )
The value() method can be used to get/set the value of some input. If
strict is enabled and no input has the indicated name, then this method will croak.
If multiple inputs have the same name, only the first one will be
affected.
The call:
$form->value('foo')
is basically a short-hand for:
$form->find_input('foo')->value;
=cut
sub value
{
my $self = shift;
my $key = shift;
my $input = $self->find_input($key);
unless ($input) {
Carp::croak("No such field '$key'") if $self->{strict};
return undef unless @_;
$input = $self->push_input("text", { name => $key, value => "" });
}
local $Carp::CarpLevel = 1;
$input->value(@_);
}
=5= |