File Coverage

blib/lib/Rose/HTML/Form/Field/Integer.pm
Criterion Covered Total %
statement 34 35 97.1
branch 8 10 80.0
condition 6 12 50.0
subroutine 7 8 87.5
pod 2 3 66.6
total 57 68 83.8


line stmt bran cond sub pod time code
1              
2             use strict;
3 5     5   87412  
  5         11  
  5         175  
4             use Rose::HTML::Object::Errors qw(:number);
5 5     5   384  
  5         10  
  5         43  
6             use base 'Rose::HTML::Form::Field::Numeric';
7 5     5   28  
  5         10  
  5         2323  
8             our $VERSION = '0.624';
9              
10             __PACKAGE__->add_required_html_attrs({
11             step => 1,
12             });
13              
14              
15             {
16             my($self) = shift;
17              
18 29     29 1 56 my $ok = $self->SUPER::validate(@_);
19             return $ok unless($ok);
20 29         81  
21 29 100       88 my $value = $self->internal_value;
22             return 1 unless(defined $value && length $value);
23 22         54  
24 22 100 66     87 my $name = sub { $self->error_label || $self->name };
25              
26 21 0   0   99 unless($value =~ /^-?\d+$/)
  0         0  
27             {
28 21 100       90 $self->add_error_id(NUM_INVALID_INTEGER, { label => $name });
29             return 0;
30 1         4 }
31 1         5  
32             return 1;
33             }
34 20         104  
35             my %Error_Map =
36             (
37             NUM_INVALID_NUMBER() => NUM_INVALID_INTEGER,
38             NUM_INVALID_NUMBER_POSITIVE() => NUM_INVALID_INTEGER_POSITIVE,
39             NUM_NOT_POSITIVE_NUMBER() => NUM_NOT_POSITIVE_INTEGER,
40             );
41              
42             {
43             my($self) = shift;
44             my $error_id = shift;
45             my $new_error_id = $Error_Map{$error_id} || $error_id;
46 8     8 0 18 return $self->SUPER::add_error_id($new_error_id, @_);
47 8         11 }
48 8   66     28  
49 8         29 {
50             my($self) = shift;
51              
52             if(@_)
53             {
54 22     22 1 186 my $error_id = shift;
55             my $new_error_id = $Error_Map{$error_id} || $error_id;
56 22 100       71 return $self->SUPER::error_id($new_error_id, @_);
57             }
58 18         31 else
59 18   33     95 {
60 18         102 my $error_id = $self->SUPER::error_id;
61             my $new_error_id = $Error_Map{$error_id} || $error_id;
62             return $new_error_id;
63             }
64 4         18 }
65 4   33     15  
66 4         15 if(__PACKAGE__->localizer->auto_load_messages)
67             {
68             __PACKAGE__->localizer->load_all_messages;
69             }
70              
71             use utf8; # The __DATA__ section contains UTF-8 text
72              
73             1;
74              
75 5     5   33  
  5         10  
  5         25  
76             [% LOCALE en %]
77              
78             NUM_INVALID_INTEGER = "[label] must be an integer."
79             NUM_INVALID_INTEGER_POSITIVE = "[label] must be a positive integer."
80             NUM_NOT_POSITIVE_INTEGER = "[label] must be a positive integer."
81              
82             [% LOCALE de %]
83              
84             NUM_INVALID_INTEGER = "[label] muß eine Ganzzahl sein."
85             NUM_INVALID_INTEGER_POSITIVE = "[label] muß eine positive Ganzzahl sein."
86             NUM_NOT_POSITIVE_INTEGER = "[label] muß eine positive Ganzzahl sein."
87              
88             [% LOCALE fr %]
89              
90             NUM_INVALID_INTEGER = "[label] doit être un entier."
91             NUM_INVALID_INTEGER_POSITIVE = "[label] doit être un entier positif."
92             NUM_NOT_POSITIVE_INTEGER = "[label] doit être un entier positif."
93              
94             [% LOCALE bg %]
95              
96             NUM_INVALID_INTEGER = "Полето '[label]' трябва да бъде цяло число."
97             NUM_INVALID_INTEGER_POSITIVE = "Полето '[label]' трябва да бъде цяло положително число."
98             NUM_NOT_POSITIVE_INTEGER = "Полето '[label]' трябва да бъде цяло положително число."
99              
100             __END__
101              
102             =head1 NAME
103              
104             Rose::HTML::Form::Field::Integer - Text field that only accepts integer values.
105              
106             =head1 SYNOPSIS
107              
108             $field =
109             Rose::HTML::Form::Field::Integer->new(
110             label => 'Count',
111             name => 'count',
112             maxlength => 6);
113              
114             $field->input_value('abc');
115             $field->validate; # false
116              
117             $field->input_value(123);
118             $field->validate; # true
119              
120             # Set minimum and maximum values
121             $field->min(2);
122             $field->max(100);
123              
124             $field->input_value(123);
125             $field->validate; # false
126              
127             $field->input_value(1);
128             $field->validate; # false
129              
130             $field->input_value(5);
131             $field->validate; # true
132              
133             print $field->html;
134             ...
135              
136             =head1 DESCRIPTION
137              
138             L<Rose::HTML::Form::Field::Integer> is a subclass of L<Rose::HTML::Form::Field::Numeric> that only accepts integer values. It overrides the L<validate()|Rose::HTML::Form::Field/validate> method of its parent class, returning true if the L<internal_value()|Rose::HTML::Form::Field/internal_value> is a valid integer, or setting an error message and returning false otherwise.
139              
140             Use the L<min|/min> and :<max|/max> attributes to control whether the range of valid values.
141              
142             =head1 OBJECT METHODS
143              
144             =over 4
145              
146             =item B<max [INT]>
147              
148             Get or set the maximum acceptable value. If the field's L<internal_value()|Rose::HTML::Form::Field/internal_value> is B<greater than> this value, then the L<validate()|Rose::HTML::Form::Field/validate> method will return false. If undefined, then no limit on the maximum value is enforced.
149              
150             =item B<min [INT]>
151              
152             Get or set the minimum acceptable value. If the field's L<internal_value()|Rose::HTML::Form::Field/internal_value> is B<less than> this value, then the L<validate()|Rose::HTML::Form::Field/validate> method will return false. If undefined, then no limit on the minimum value is enforced.
153              
154             =item B<negative [BOOL]>
155              
156             If BOOL is true or omitted, sets L<max|/max> to C<0>. If BOOL is false, sets L<max|/max> to undef.
157              
158             =item B<positive [BOOL]>
159              
160             If BOOL is true or omitted, sets L<min|/min> to C<0>. If BOOL is false, sets L<min|/min> to undef.
161              
162             =item B<step [ NUM | any ]>
163              
164             Get or set the granularity that is expected (and required). The value may be a number or the keyword C<any>. The default value is C<1>.
165              
166             =back
167              
168             =head1 AUTHOR
169              
170             John C. Siracusa (siracusa@gmail.com)
171              
172             =head1 LICENSE
173              
174             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.