| 1 | package HTML::FormHandler::Field::Text; | ||||||||||
| 2 | # ABSTRACT: text field | ||||||||||
| 3 | 140 | $HTML::FormHandler::Field::Text::VERSION = '0.410002'; | |||||||||
| 4 | 140 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 140 | extends 'HTML::FormHandler::Field'; | |||||||||
| 6 | |||||||||||
| 7 | 140 | has 'size' => ( isa => 'Int|Undef', is => 'rw', default => '0' ); | |||||||||
| 8 | 140 | has 'maxlength' => ( isa => 'Int|Undef', is => 'rw' ); | |||||||||
| 9 | 140 | has 'maxlength_message' => ( isa => 'Str', is => 'rw', | |||||||||
| 10 | default => 'Field should not exceed [quant,_1,character]. You entered [_2]', | ||||||||||
| 11 | ); | ||||||||||
| 12 | 140 | has 'minlength' => ( isa => 'Int|Undef', is => 'rw', default => '0' ); | |||||||||
| 13 | 140 | has 'minlength_message' => ( isa => 'Str', is => 'rw', | |||||||||
| 14 | default => 'Field must be at least [quant,_1,character]. You entered [_2]' ); | ||||||||||
| 15 | |||||||||||
| 16 | 140 | has '+widget' => ( default => 'Text' ); | |||||||||
| 17 | |||||||||||
| 18 | 140 | 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 | 96 | ▶ | sub get_class_messages { | ||||||||
|
Subroutine
get_class_messages: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 25 | 96 | my $self = shift; | |||||||||
| 26 | my $messages = { | ||||||||||
| 27 | 96 | %{ $self->next::method }, | |||||||||
| 28 | %$class_messages, | ||||||||||
| 29 | }; | ||||||||||
| 30 | 96 | ▶ | $messages->{text_minlength} = $self->minlength_message | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | if $self->minlength_message; | ||||||||||
| 32 | 96 | ▶ | $messages->{text_maxlength} = $self->maxlength_message | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | if $self->maxlength_message; | ||||||||||
| 34 | 96 | return $messages; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 582 | ▶ | sub _inner_validate_field { | ||||||||
|
Subroutine
_inner_validate_field: called CC=2 SCAR=6.9
| |||||||||||
| 38 | 582 | my $self = shift; | |||||||||
| 39 | # Check for multiple values | ||||||||||
| 40 | 582 | ▶ | if ( ref $self->value eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 3 | return $self->add_error( | |||||||||
| 42 | $self->get_message('multiple_values_disallowed'), | ||||||||||
| 43 | ); | ||||||||||
| 44 | } | ||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 558 | ▶ | sub validate { | ||||||||
|
Subroutine
validate: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 48 | 558 | my $field = shift; | |||||||||
| 49 | |||||||||||
| 50 | 558 | ▶ | return unless $field->next::method; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 558 | my $value = $field->value; | |||||||||
| 52 | # Check for max length | ||||||||||
| 53 | 558 | ▶ | if ( my $maxlength = $field->maxlength ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 12 | ▶ | return $field->add_error( $field->get_message('text_maxlength'), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | $maxlength, length $value, $field->loc_label ) | ||||||||||
| 56 | if length $value > $maxlength; | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | # Check for min length | ||||||||||
| 60 | 554 | ▶ | if ( my $minlength = $field->minlength ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 12 | ▶ | return $field->add_error( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | $field->get_message('text_minlength'), | ||||||||||
| 63 | $minlength, length $value, $field->loc_label ) | ||||||||||
| 64 | if length $value < $minlength; | ||||||||||
| 65 | } | ||||||||||
| 66 | 551 | return 1; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | 140 | __PACKAGE__->meta->make_immutable; | |||||||||
| 71 | 140 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 140 | 1; | |||||||||
| 73 | |||||||||||
| 74 | __END__ | ||||||||||