File Coverage

blib/lib/Rose/HTML/Form/Field/Numeric.pm
Criterion Covered Total %
statement 50 51 98.0
branch 26 30 86.6
condition 16 21 76.1
subroutine 9 9 100.0
pod 4 4 100.0
total 105 115 91.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 7     7   93793  
  7         13  
  7         212  
4             use Rose::HTML::Object::Errors qw(:number);
5 7     7   31  
  7         13  
  7         48  
6             use base 'Rose::HTML::Form::Field::Text';
7 7     7   43  
  7         13  
  7         3853  
8             our $VERSION = '0.624';
9              
10             # use Rose::Object::MakeMethods::Generic
11             # (
12             # scalar => [ qw(min max) ],
13             # );
14              
15             __PACKAGE__->default_html_attr_value(size => 6);
16              
17             __PACKAGE__->add_required_html_attrs({
18             type => 'number',
19             });
20              
21             {
22             my($self) = shift;
23              
24 5     5 1 21 if(!@_ || $_[0])
25             {
26 5 100 66     33 $self->min(0);
    50          
27             $self->max(undef);
28 3         20 }
29 3         14 elsif(@_)
30             {
31             $self->min(undef);
32             }
33 2         6 }
34              
35             {
36             my($self) = shift;
37              
38             if(!@_ || $_[0])
39 4     4 1 9 {
40             $self->max(0);
41 4 100 66     30 $self->min(undef);
    50          
42             }
43 2         8 elsif(@_)
44 2         7 {
45             $self->max(undef);
46             }
47             }
48 2         7  
49             {
50             my($self) = shift;
51              
52             my $value = $self->SUPER::internal_value(@_);
53              
54 172     172 1 244 if(defined $value)
55             {
56 172         390 for($value)
57             {
58 172 100       368 s/-\s+/-/ || s/\+\s+//;
59             }
60 161         257 }
61              
62 161 100       474 return (defined $value && $value =~ /\S/) ? $value : undef;
63             }
64              
65             # This is $RE{num}{dec} from Regexp::Common::number
66 172 100 100     812 my $Match = qr{^(?:(?i)(?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))$};
67              
68             {
69             my($self) = shift;
70              
71             my $ok = $self->SUPER::validate(@_);
72             return $ok unless($ok);
73              
74 51     51 1 743 my $value = $self->internal_value;
75             return 1 unless(defined $value && length $value);
76 51         144  
77 51 100       100 my $min = $self->min;
78             my $max = $self->max;
79 50         101  
80 50 100 66     167 my $name = sub { $self->error_label || $self->name };
81              
82 49         128 unless($value =~ $Match)
83 49         131 {
84             if(defined $min && $min >= 0)
85 49 50   16   164 {
  16         48  
86             $self->add_error_id(NUM_INVALID_NUMBER_POSITIVE, { label => $name });
87 49 100       350 }
88             else
89 5 50 33     20 {
90             $self->add_error_id(NUM_INVALID_NUMBER, { label => $name });
91 0         0 }
92              
93             return 0;
94             }
95 5         27  
96             if(defined $min && $value < $min)
97             {
98 5         19 if($min == 0)
99             {
100             $self->add_error_id(NUM_NOT_POSITIVE_NUMBER, { label => $name });
101 44 100 100     154 }
102             else
103 6 100       17 {
104             $self->add_error_id(NUM_BELOW_MIN, { label => $name, value => $min });
105 4         15 }
106             return 0;
107             }
108              
109 2         11 if(defined $max && $value > $max)
110             {
111 6         26 $self->add_error_id(NUM_ABOVE_MAX, { label => $name, value => $max });
112             return 0;
113             }
114 38 100 100     105  
115             return 1;
116 4         20 }
117 4         17  
118             if(__PACKAGE__->localizer->auto_load_messages)
119             {
120 34         151 __PACKAGE__->localizer->load_all_messages;
121             }
122              
123             use utf8; # The __DATA__ section contains UTF-8 text
124              
125             1;
126              
127              
128 7     7   53 [% LOCALE en %]
  7         15  
  7         48  
129              
130             NUM_INVALID_NUMBER = "[label] must be a number."
131             NUM_INVALID_NUMBER_POSITIVE = "[label] must be a positive number."
132             NUM_NOT_POSITIVE_NUMBER = "[label] must be a positive number."
133             NUM_BELOW_MIN = "[label] must be greater than or equal to [value]."
134             NUM_ABOVE_MAX = "[label] must be less than or equal to [value]."
135              
136             [% LOCALE de %]
137              
138             NUM_INVALID_NUMBER = "[label] muß eine Zahl sein."
139             NUM_INVALID_NUMBER_POSITIVE = "[label] muß eine positive Zahl sein."
140             NUM_NOT_POSITIVE_NUMBER = "[label] muß eine positive Zahl sein."
141             NUM_BELOW_MIN = "[label] muß größer als oder gleich [value] sein."
142             NUM_ABOVE_MAX = "[label] muß kleiner oder gleich [value] sein."
143              
144             [% LOCALE fr %]
145              
146             NUM_INVALID_NUMBER = "[label] doit être un nombre."
147             NUM_INVALID_NUMBER_POSITIVE = "[label] doit être un nombre positif."
148             NUM_NOT_POSITIVE_NUMBER = "[label] doit être un nombre positif."
149             NUM_BELOW_MIN = "[label] doit être plus grand ou égal à [value]."
150             NUM_ABOVE_MAX = "[label] doit être plus petit ou égal à [value]."
151              
152             [% LOCALE bg %]
153              
154             NUM_INVALID_NUMBER = "Полето '[label]' трябва да бъде цяло число."
155             NUM_INVALID_NUMBER_POSITIVE = "Полето '[label]' трябва да бъде цяло положително число."
156             NUM_NOT_POSITIVE_NUMBER = "Полето '[label]' трябва да бъде цяло положително число."
157             NUM_BELOW_MIN = "Стойността в '[label]' трябва да бъде по-голяма от [value]."
158             NUM_ABOVE_MAX = "Стойността в '[label]' трябва да бъде по-малка или равна на [value]."
159              
160             __END__
161              
162             =head1 NAME
163              
164             Rose::HTML::Form::Field::Numeric - Text field that only accepts numeric values.
165              
166             =head1 SYNOPSIS
167              
168             $field =
169             Rose::HTML::Form::Field::Numeric->new(
170             label => 'Distance',
171             name => 'distance',
172             maxlength => 6);
173              
174             $field->input_value('abc');
175             $field->validate; # false
176              
177             $field->input_value(123);
178             $field->validate; # true
179              
180             # Set minimum and maximum values
181             $field->min(2);
182             $field->max(100);
183              
184             $field->input_value(123);
185             $field->validate; # false
186              
187             $field->input_value(1);
188             $field->validate; # false
189              
190             $field->input_value(5.5);
191             $field->validate; # true
192              
193             print $field->html;
194             ...
195              
196             =head1 DESCRIPTION
197              
198             L<Rose::HTML::Form::Field::Numeric> is a subclass of L<Rose::HTML::Form::Field::Text> that only accepts numeric 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 number, or setting an error message and returning false otherwise.
199              
200             Use the L<min|/min> and :<max|/max> attributes to control whether the range of valid values.
201              
202             =head1 OBJECT METHODS
203              
204             =over 4
205              
206             =item B<max [NUMERIC]>
207              
208             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.
209              
210             =item B<min [NUMERIC]>
211              
212             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.
213              
214             =item B<negative [BOOL]>
215              
216             If BOOL is true or omitted, sets L<max|/max> to C<0>. If BOOL is false, sets L<max|/max> to undef.
217              
218             =item B<positive [BOOL]>
219              
220             If BOOL is true or omitted, sets L<min|/min> to C<0>. If BOOL is false, sets L<min|/min> to undef.
221              
222             =back
223              
224             =head1 AUTHOR
225              
226             John C. Siracusa (siracusa@gmail.com)
227              
228             =head1 LICENSE
229              
230             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.