File Coverage

blib/lib/HTML/FormHandler/Field/Text.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Field::Text;
2             # ABSTRACT: text field
3              
4 5     5   41100 use Moose;
  0            
  0            
5             extends 'HTML::FormHandler::Field';
6             our $VERSION = '0.01';
7              
8             has 'size' => ( isa => 'Int|Undef', is => 'rw', default => '0' );
9             has 'maxlength' => ( isa => 'Int|Undef', is => 'rw' );
10             has 'maxlength_message' => ( isa => 'Str', is => 'rw',
11             default => 'Field should not exceed [quant,_1,character]. You entered [_2]',
12             );
13             has 'minlength' => ( isa => 'Int|Undef', is => 'rw', default => '0' );
14             has 'minlength_message' => ( isa => 'Str', is => 'rw',
15             default => 'Field must be at least [quant,_1,character]. You entered [_2]' );
16              
17             has '+widget' => ( default => 'Text' );
18              
19             our $class_messages = {
20             'text_maxlength' => 'Field should not exceed [quant,_1,character]. You entered [_2]',
21             'text_minlength' => 'Field must be at least [quant,_1,character]. You entered [_2]',
22             };
23              
24             sub get_class_messages {
25             my $self = shift;
26             my $messages = {
27             %{ $self->next::method },
28             %$class_messages,
29             };
30             $messages->{text_minlength} = $self->minlength_message
31             if $self->minlength_message;
32             $messages->{text_maxlength} = $self->maxlength_message
33             if $self->maxlength_message;
34             return $messages;
35             }
36              
37              
38             sub validate {
39             my $field = shift;
40              
41             return unless $field->next::method;
42             my $value = $field->input;
43             # Check for max length
44             if ( my $maxlength = $field->maxlength ) {
45             return $field->add_error( $field->get_message('text_maxlength'),
46             $maxlength, length $value, $field->loc_label )
47             if length $value > $maxlength;
48             }
49              
50             # Check for min length
51             if ( my $minlength = $field->minlength ) {
52             return $field->add_error(
53             $field->get_message('text_minlength'),
54             $minlength, length $value, $field->loc_label )
55             if length $value < $minlength;
56             }
57             return 1;
58             }
59              
60              
61             __PACKAGE__->meta->make_immutable;
62             use namespace::autoclean;
63             1;
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             HTML::FormHandler::Field::Text - text field
74              
75             =head1 VERSION
76              
77             version 0.40057
78              
79             =head1 DESCRIPTION
80              
81             This is a simple text entry field. Widget type is 'text'.
82              
83             =head1 METHODS
84              
85             =head2 size [integer]
86              
87             This is used in constructing HTML. It determines the size of the input field.
88             The 'maxlength' field should be used as a constraint on the size of the field,
89             not this attribute.
90              
91             =head2 minlength [integer]
92              
93             This integer value, if non-zero, defines the minimum number of characters that must
94             be entered.
95              
96             =head2 maxlength [integer]
97              
98             A constraint on the maximum length of the text.
99              
100             =head2 error messages
101              
102             Set error messages (text_minlength, text_maxlength):
103              
104             has_field 'my_text' => ( type => 'Text', messages =>
105             { 'text_minlength' => 'Field is too short',
106             'text_maxlength' => 'Field is too long',
107             } );
108              
109             =head1 AUTHOR
110              
111             FormHandler Contributors - see HTML::FormHandler
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             This software is copyright (c) 2014 by Gerda Shank.
116              
117             This is free software; you can redistribute it and/or modify it under
118             the same terms as the Perl 5 programming language system itself.
119              
120             =cut