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