File Coverage

blib/lib/Form/Field.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Form::Field;
2 14     14   29594 use overload '""' => \&render;
  14         16543  
  14         192  
3 14     14   777 use Carp;
  14         26  
  14         2118  
4 14     14   88 use base qw(HTML::Element Form::Base Class::Accessor);
  14         23  
  14         26163  
5             Form::Field->mk_attributes(qw/type _validation_types _tag/);
6             Form::Field->mk_accessors(qw/name _validation _form/);
7             Form::Field->_validation_types([ qw/ javascript perl / ]);
8             Form::Field->_tag("input");
9             use Module::Pluggable search_path=>['Form::Field'], require => 1;
10             my $sym = 0;
11              
12             sub render {
13             my $self = shift;
14             if ($self->_validation and
15             my $js = $self->_validation->{javascript}) {
16             $self->attr("id", "field".++$sym);
17             $self->attr("onblur", qq{validate('field$sym', '}.$self->name.
18             qq{', $js)});
19             }
20             $self->as_HTML;
21             }
22              
23             sub add_validation {
24             my ($self, $validation) = @_;
25             # This could be a regex, a hash of regexen, or a module name.
26             # We want to turn it into a hash.
27             if (ref $validation and UNIVERSAL::isa($validation, "Regexp")) {
28             $self->_validation({
29             map { $_ => $validation } @{$self->_validation_types}
30             });
31             } elsif (ref($validation) eq "HASH") {
32             $self->_validation($validation)
33             } else {
34             # We'll assume it's a module name, then.
35             Form::Maker->_load_and_run($validation);
36             $self->_validation($validation->validate($self));
37             }
38             }
39              
40             1;