File Coverage

blib/lib/Rose/HTML/Form/Field/PhoneNumber/US.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 8 62.5
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 38 41 92.6


line stmt bran cond sub pod time code
1             package Rose::HTML::Form::Field::PhoneNumber::US;
2              
3 2     2   887 use strict;
  2         5  
  2         71  
4              
5 2     2   10 use Rose::HTML::Object::Errors qw(:phone);
  2         4  
  2         12  
6              
7 2     2   14 use base 'Rose::HTML::Form::Field::Text';
  2         4  
  2         862  
8              
9             our $VERSION = '0.606';
10              
11             __PACKAGE__->add_required_html_attrs(
12             {
13             maxlength => 14,
14             });
15              
16             sub validate
17             {
18 1     1 1 3 my($self) = shift;
19              
20 1         4 my $number = $self->internal_value;
21              
22 1 50       14 return 1 if($number !~ /\S/);
23              
24 1         5 $number =~ s/\D+//g;
25              
26 1 50       5 return 1 if(length $number == 10);
27              
28 1         14 $self->add_error_id(PHONE_INVALID);
29              
30 1         3 return;
31             }
32              
33             sub inflate_value
34             {
35 14     14 1 31 my($self, $value) = @_;
36              
37 14 50       29 return unless(defined $value);
38              
39 14         79 $value =~ s/\D+//g;
40              
41 14 100       57 if($value =~ /^(\d{3})(\d{3})(\d{4})$/)
42             {
43 12         65 return "$1-$2-$3";
44             }
45              
46 2         6 return $_[1];
47             }
48              
49             *deflate_value = \&inflate_value;
50              
51             if(__PACKAGE__->localizer->auto_load_messages)
52             {
53             __PACKAGE__->localizer->load_all_messages;
54             }
55              
56 2     2   19 use utf8; # The __DATA__ section contains UTF-8 text
  2         5  
  2         11  
57              
58             1;
59              
60             __DATA__
61              
62             [% LOCALE en %]
63              
64             PHONE_INVALID = "Phone number must be 10 digits, including area code."
65              
66             [% LOCALE de %]
67              
68             PHONE_INVALID = "Die Telefon-Nummer muß 10 Stellen enthalten (einschließlich Vorwahl)."
69              
70             [% LOCALE fr %]
71              
72             PHONE_INVALID = "Le numéro de téléphone, indicatif compris, doit avoir 10 chiffres."
73              
74             [% LOCALE bg %]
75              
76             PHONE_INVALID = "Телефонния номер (вкл. кода на областта) не трябва да надвишава 10 цифри."
77              
78             __END__
79              
80             =head1 NAME
81              
82             Rose::HTML::Form::Field::PhoneNumber::US - Text field that accepts only input that contains exactly 10 digits, and coerces valid input into US phone numbers in the form: 123-456-7890
83              
84             =head1 SYNOPSIS
85              
86             $field =
87             Rose::HTML::Form::Field::PhoneNumber::US->new(
88             label => 'Phone',
89             name => 'phone',
90             size => 20);
91              
92             $field->input_value('555-5555');
93              
94             # "Phone number must be 10 digits, including area code"
95             $field->validate or warn $field->error;
96              
97             $field->input_value('(123) 456-7890');
98              
99             print $field->internal_value; # "123-456-7890"
100              
101             print $field->html;
102             ...
103              
104             =head1 DESCRIPTION
105              
106             L<Rose::HTML::Form::Field::PhoneNumber::US> is a subclass of L<Rose::HTML::Form::Field::Text> that only allows values that contain exactly 10 digits, which it coerces into the form "123-456-7890". It overrides the L<validate()|Rose::HTML::Form::Field/validate> and L<inflate_value()|Rose::HTML::Form::Field/inflate_value>, and L<deflate_value()|Rose::HTML::Form::Field/deflate_value> methods of its parent class.
107              
108             This is a good example of a custom field class that constrains the kinds of inputs that it accepts and coerces all valid input and output to a particular format. See L<Rose::HTML::Form::Field::Time> for another example, and a list of more complex examples.
109              
110             =head1 AUTHOR
111              
112             John C. Siracusa (siracusa@gmail.com)
113              
114             =head1 LICENSE
115              
116             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.