| 1 |
package HTML::FormHandler::Field::Text; |
| 2 |
# ABSTRACT: text field |
| 3 |
$HTML::FormHandler::Field::Text::VERSION = '0.410002'; |
| 4 |
use Moose; |
| 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 |
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 |
sub _inner_validate_field { |
| 38 |
my $self = shift; |
| 39 |
# Check for multiple values |
| 40 |
if ( ref $self->value eq 'ARRAY' ) { |
| 41 |
return $self->add_error( |
| 42 |
$self->get_message('multiple_values_disallowed'), |
| 43 |
); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
sub validate { |
| 48 |
my $field = shift; |
| 49 |
|
| 50 |
return unless $field->next::method; |
| 51 |
my $value = $field->value; |
| 52 |
# Check for max length |
| 53 |
if ( my $maxlength = $field->maxlength ) { |
| 54 |
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 |
if ( my $minlength = $field->minlength ) { |
| 61 |
return $field->add_error( |
| 62 |
$field->get_message('text_minlength'), |
| 63 |
$minlength, length $value, $field->loc_label ) |
| 64 |
if length $value < $minlength; |
| 65 |
} |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
__PACKAGE__->meta->make_immutable; |
| 71 |
use namespace::autoclean; |
| 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.410002 |
| 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 |