File Coverage

blib/lib/Data/MuForm/Field/Integer.pm
Criterion Covered Total %
statement 24 25 96.0
branch 12 16 75.0
condition 6 6 100.0
subroutine 4 4 100.0
pod 1 2 50.0
total 47 53 88.6


line stmt bran cond sub pod time code
1             package Data::MuForm::Field::Integer;
2             # ABSTRACT: validate an integer value
3              
4 8     8   5706 use Moo;
  8         8010  
  8         66  
5 8     8   3017 use Data::MuForm::Meta;
  8         11  
  8         57  
6             extends 'Data::MuForm::Field::Text';
7              
8             has '+html5_input_type' => ( default => 'number' );
9             has '+size' => ( default => 8 );
10              
11             has 'range_start' => ( is => 'rw' );
12             has 'range_end' => ( is => 'rw' );
13              
14             our $class_messages = {
15             'integer_needed' => 'Value must be an integer',
16             'range_too_low' => 'Value must be greater than or equal to [_1]',
17             'range_too_high' => 'Value must be less than or equal to [_1]',
18             'range_incorrect' => 'Value must be between [_1] and [_2]',
19             };
20              
21             sub get_class_messages {
22 6     6 0 6 my $self = shift;
23             return {
24 6         4 %{ $self->next::method },
  6         19  
25             %$class_messages,
26             }
27             }
28              
29             sub validate {
30 19     19 1 15 my $field = shift;
31              
32 19         33 my $value = $field->value;
33 19 50       43 return 1 unless defined $value;
34              
35 19         16 $value =~ s/^\+//;
36 19         25 $field->value($value);
37              
38 19 100       60 unless ( $value =~ /^-?[0-9]+$/ ) {
39 2         9 $field->add_error($field->get_message('integer_needed'));
40             }
41              
42 19         380 my $low = $field->range_start;
43 19         26 my $high = $field->range_end;
44              
45 19 100 100     68 if ( defined $low && defined $high ) {
46             return
47 11 100 100     57 $value >= $low && $value <= $high ? 1 :
48             $field->add_error( $field->get_message('range_incorrect'), low => $low, high => $high );
49             }
50              
51 8 100       14 if ( defined $low ) {
52             return
53 2 100       7 $value >= $low ? 1 :
54             $field->add_error( $field->get_message('range_too_low'), low => $low );
55             }
56              
57 6 50       8 if ( defined $high ) {
58             return
59 0 0       0 $value <= $high ? 1 :
60             $field->add_error( $field->get_message('range_too_high'), high => $high );
61             }
62              
63 6         9 return 1;
64             }
65              
66              
67              
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Data::MuForm::Field::Integer - validate an integer value
79              
80             =head1 VERSION
81              
82             version 0.04
83              
84             =head1 DESCRIPTION
85              
86             This accepts a positive or negative integer. Negative integers may
87             be prefixed with a dash. By default a max of eight digits are accepted.
88             Widget type is 'text'.
89              
90             If form has 'is_html5' flag active it will render <input type="number" ... />
91             instead of type="text"
92              
93             The 'range_start' and 'range_end' attributes may be used to limit valid numbers.
94              
95             =head1 AUTHOR
96              
97             Gerda Shank
98              
99             =head1 COPYRIGHT AND LICENSE
100              
101             This software is copyright (c) 2017 by Gerda Shank.
102              
103             This is free software; you can redistribute it and/or modify it under
104             the same terms as the Perl 5 programming language system itself.
105              
106             =cut