File Coverage

blib/lib/Rose/HTML/Form/Field/Time/Hours.pm
Criterion Covered Total %
statement 21 36 58.3
branch 0 10 0.0
condition 0 6 0.0
subroutine 7 8 87.5
pod 2 2 100.0
total 30 62 48.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 6     6   33  
  6         10  
  6         188  
4             use Rose::HTML::Object::Errors qw(:time);
5 6     6   360 use Rose::HTML::Object::Messages qw(:time);
  6         11  
  6         37  
6 6     6   38  
  6         12  
  6         30  
7             use base 'Rose::HTML::Form::Field::Text';
8 6     6   37  
  6         14  
  6         1030  
9             our $VERSION = '0.606';
10              
11             use Rose::Object::MakeMethods::Generic
12             (
13             boolean => 'military',
14 6         39 );
15 6     6   40  
  6         11  
16             __PACKAGE__->add_required_html_attrs(
17             {
18             size => 2,
19             });
20              
21             {
22             my($self) = shift;
23             $self->label_id(FIELD_LABEL_HOUR);
24 23     23 1 52 $self->SUPER::init(@_);
25 23         120 }
26 23         113  
27             {
28             my($self) = shift;
29              
30             my $ok = $self->SUPER::validate(@_);
31 0     0 1   return $ok unless($ok);
32              
33 0           my $value = $self->internal_value;
34 0 0          
35             unless($value =~ /^\d\d?$/)
36 0           {
37             $self->add_error_id(TIME_INVALID_HOUR);
38 0 0         return 0;
39             }
40 0            
41 0           if($self->military)
42             {
43             return 1 if($value >= 0 && $value <= 23);
44 0 0         $self->add_error_id(TIME_INVALID_HOUR);
45             return 0;
46 0 0 0       }
47 0           else
48 0           {
49             return 1 if($value >= 0 && $value <= 12);
50             $self->add_error_id(TIME_INVALID_HOUR);
51             return 0;
52 0 0 0       }
53 0            
54 0           return 1;
55             }
56              
57 0           if(__PACKAGE__->localizer->auto_load_messages)
58             {
59             __PACKAGE__->localizer->load_all_messages;
60             }
61              
62             use utf8; # The __DATA__ section contains UTF-8 text
63              
64             1;
65 6     6   2028  
  6         13  
  6         29  
66              
67             [% LOCALE en %]
68              
69             TIME_INVALID_HOUR = "Invalid hour."
70             FIELD_LABEL_HOUR = "Hour"
71             FIELD_ERROR_LABEL_HOUR = "hour"
72              
73             [% LOCALE de %]
74              
75             TIME_INVALID_HOUR = "Ungültige Stunde."
76             FIELD_LABEL_HOUR = "Stunde"
77             FIELD_ERROR_LABEL_HOUR = "Stunde"
78              
79             [% LOCALE fr %]
80              
81             TIME_INVALID_HOUR = "Heure invalide."
82             FIELD_LABEL_HOUR = "Heure"
83             FIELD_ERROR_LABEL_HOUR = "heure"
84              
85             [% LOCALE bg %]
86              
87             TIME_INVALID_HOUR = "Невалиден час."
88             FIELD_LABEL_HOUR = "Час"
89             FIELD_ERROR_LABEL_HOUR = "час"
90              
91             __END__
92              
93             =head1 NAME
94              
95             Rose::HTML::Form::Field::Time::Hours - Text field that only accepts valid hours.
96              
97             =head1 SYNOPSIS
98              
99             $field =
100             Rose::HTML::Form::Field::Time::Hours->new(
101             label => 'Hours',
102             name => 'hrs');
103              
104             $field->input_value(99);
105             $field->validate; # 0
106              
107             $field->input_value(20);
108             $field->validate; # 0
109              
110             $field->military(1);
111             $field->validate; # 1
112              
113             print $field->html;
114              
115             ...
116              
117             =head1 DESCRIPTION
118              
119             L<Rose::HTML::Form::Field::Time::Hours> is a subclass of L<Rose::HTML::Form::Field::Text> that only accepts valid hours. It supports normal (0-12) and military (0-23) time. The behavior is toggled via the L<military|/military> object method. Leading zeros are optional.
120              
121             =head1 OBJECT METHODS
122              
123             =over 4
124              
125             =item B<military [BOOL]>
126              
127             Get or set the boolean flag that indicates whether or not the field will accept "military time." If true, the hours 0-23 are valid. If false, only 0-12 are valid.
128              
129             =back
130              
131             =head1 AUTHOR
132              
133             John C. Siracusa (siracusa@gmail.com)
134              
135             =head1 LICENSE
136              
137             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.