File: Text.pm

package info (click to toggle)
libhtml-formhandler-perl 0.40067-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,432 kB
  • ctags: 697
  • sloc: perl: 9,312; makefile: 2
file content (129 lines) | stat: -rw-r--r-- 3,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package HTML::FormHandler::Field::Text;
# ABSTRACT: text field
$HTML::FormHandler::Field::Text::VERSION = '0.40067';
use Moose;
extends 'HTML::FormHandler::Field';

has 'size' => ( isa => 'Int|Undef', is => 'rw', default => '0' );
has 'maxlength' => ( isa => 'Int|Undef', is => 'rw' );
has 'maxlength_message' => ( isa => 'Str', is => 'rw',
    default => 'Field should not exceed [quant,_1,character]. You entered [_2]',
);
has 'minlength' => ( isa => 'Int|Undef', is => 'rw', default => '0' );
has 'minlength_message' => ( isa => 'Str', is => 'rw',
    default => 'Field must be at least [quant,_1,character]. You entered [_2]' );

has '+widget' => ( default => 'Text' );

our $class_messages = {
    'text_maxlength' => 'Field should not exceed [quant,_1,character]. You entered [_2]',
    'text_minlength' => 'Field must be at least [quant,_1,character]. You entered [_2]',
    'multiple_values_disallowed' => 'Field must contain a single value',
};

sub get_class_messages {
    my $self = shift;
    my $messages = {
        %{ $self->next::method },
        %$class_messages,
    };
    $messages->{text_minlength} = $self->minlength_message
        if $self->minlength_message;
    $messages->{text_maxlength} = $self->maxlength_message
        if $self->maxlength_message;
    return $messages;
}

sub _inner_validate_field {
    my $self = shift;
    # Check for multiple values
    if ( ref $self->value eq 'ARRAY' ) {
        return $self->add_error(
            $self->get_message('multiple_values_disallowed'),
        );
    }
}

sub validate {
    my $field = shift;

    return unless $field->next::method;
    my $value = $field->value;
    # Check for max length
    if ( my $maxlength = $field->maxlength ) {
        return $field->add_error( $field->get_message('text_maxlength'),
            $maxlength, length $value, $field->loc_label )
            if length $value > $maxlength;
    }

    # Check for min length
    if ( my $minlength = $field->minlength ) {
        return $field->add_error(
            $field->get_message('text_minlength'),
            $minlength, length $value, $field->loc_label )
            if length $value < $minlength;
    }
    return 1;
}


__PACKAGE__->meta->make_immutable;
use namespace::autoclean;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormHandler::Field::Text - text field

=head1 VERSION

version 0.40067

=head1 DESCRIPTION

This is a simple text entry field. Widget type is 'text'.

=head1 METHODS

=head2 size [integer]

This is used in constructing HTML. It determines the size of the input field.
The 'maxlength' field should be used as a constraint on the size of the field,
not this attribute.

=head2 minlength [integer]

This integer value, if non-zero, defines the minimum number of characters that must
be entered.

=head2 maxlength [integer]

A constraint on the maximum length of the text.

=head2 error messages

Set error messages (text_minlength, text_maxlength):

    has_field 'my_text' => ( type => 'Text', messages =>
        {  'text_minlength' => 'Field is too short',
           'text_maxlength' => 'Field is too long',
        } );

=head1 AUTHOR

FormHandler Contributors - see HTML::FormHandler

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Gerda Shank.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut