File Coverage

blib/lib/HTML/FormHandler/Field/Text.pm
Criterion Covered Total %
statement 23 23 100.0
branch 13 16 81.2
condition n/a
subroutine 5 5 100.0
pod 1 2 50.0
total 42 46 91.3


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