CGI-FormBuilder
view release on metacpan or search on metacpan
lib/CGI/FormBuilder/Field.pm view on Meta::CPAN
sub DESTROY { 1 }
sub AUTOLOAD {
# This allows direct addressing by name, for quicker usage
my $self = shift;
my($name) = $AUTOLOAD =~ /.*::(.+)/;
debug 3, "-> dispatch to \$field->{$name} = @_";
croak "self not ref in AUTOLOAD" unless ref $self; # nta
$self->{$name} = shift if @_;
return $self->{$name};
}
1;
__END__
=head1 DESCRIPTION
This module is internally used by B<FormBuilder> to create and maintain
field information. Usually, you will not want to directly access this
set of data structures. However, one big exception is if you are going
to micro-control form rendering. In this case, you will need to access
the field objects directly.
To do so, you will want to loop through the fields in order:
for my $field ($form->field) {
# $field holds an object stringified to a field name
if ($field =~ /_date$/) {
$field->sticky(0); # clear CGI value
print "Enter $field here:", $field->tag;
} else {
print $field->label, ': ', $field->tag;
}
}
As illustrated, each C<$field> variable actually holds a stringifiable
object. This means if you print them out, you will get the field name,
allowing you to check for certain fields. However, since it is an object,
you can then run accessor methods directly on that object.
The most useful method is C<tag()>. It generates the HTML input tag
for the field, including all option and type handling, and returns a
string which you can then print out or manipulate appropriately.
Second to this method is the C<script> method, which returns the appropriate
JavaScript validation routine for that field. This is useful at the top of
your form rendering, when you are printing out the leading C<< <head> >> section
of your HTML document. It is called by the C<$form> method of the same name.
The following methods are provided for each C<$field> object.
=head1 METHODS
=head2 new($form, %args)
This creates a new C<$field> object. The first argument must be a reference
to the top-level C<$form> object, for callbacks. The remaining arguments
should be hash, of which one C<key/value> pair must specify the C<name> of
the field. Normally you should not touch this method. Ever.
=head2 field(%args)
This is a delegated field call. This is how B<FormBuilder> tweaks its fields.
Once you have a C<$field> object, you call this method the exact same way
that you would call the main C<field()> method, minus the field name. Again
you should use the top-level call instead.
=head2 inflate($subref)
This sets the inflate attribute: subroutine reference used to inflate values
returned by value() into objects or whatever you want. If no parameter,
returns the inflate subroutine reference that is set. For example:
use DateTime::Format::Strptime;
my $date_format = DateTime::Format::Strptime->new(
pattern => '%D', # for MM/DD/YYYY american dates
locale => 'en_US',
time_zone => 'America/Los_Angeles',
);
$field->inflate( sub { return $date_format->format_datetime(shift) } );
=head2 invalid
This returns the opposite value that C<validate()> would return, with
some extra magic that keeps state for form rendering purposes.
=head2 jsfunc()
Returns the appropriate JavaScript validation code (see above).
=head2 label($str)
This sets and returns the field's label. If unset, it will be generated
from the name of the field.
=head2 tag($type)
Returns an XHTML form input tag (see above). By default it renders the
tag based on the type set from the top-level field method:
$form->field(name => 'poetry', type => 'textarea');
However, if you are doing custom rendering you can override this temporarily
by passing in the type explicitly. This is usually not useful unless you
have a custom rendering module that forcibly overrides types for certain
fields.
=head2 type($type)
This sets and returns the field's type. If unset, it will automatically
generate the appropriate field type, depending on the number of options and
whether multiple values are allowed:
Field options?
No = text (done)
Yes:
Less than 'selectnum' setting?
( run in 0.704 second using v1.01-cache-2.11-cpan-39bf76dae61 )