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