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