File Coverage

blib/lib/Rose/HTML/Form/Field/Email.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 37 38 97.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 3     3   104619  
  3         12  
  3         103  
4             use Email::Valid;
5 3     3   925  
  3         166797  
  3         250  
6             use Rose::HTML::Object::Errors qw(:email);
7 3     3   443  
  3         14  
  3         77  
8             use base 'Rose::HTML::Form::Field::Text';
9 3     3   35  
  3         19  
  3         1072  
10             our $VERSION = '0.606';
11              
12             {
13             my($self) = shift;
14              
15 10     10 1 20 my $ok = $self->SUPER::validate(@_);
16             return $ok unless($ok);
17 10         49  
18 10 100       26 my $value = $self->internal_value;
19             return 1 unless(length $value);
20 8         29  
21 8 50       23 $ok = Email::Valid->address($value);
22              
23 8         48 unless($ok)
24             {
25 8 100       5433 $self->add_error_id(EMAIL_INVALID);
26             return 0;
27 3         19 }
28 3         16  
29             return 1;
30             }
31 5         20  
32             if(__PACKAGE__->localizer->auto_load_messages)
33             {
34             __PACKAGE__->localizer->load_all_messages;
35             }
36              
37             use utf8; # The __DATA__ section contains UTF-8 text
38              
39 3     3   34 1;
  3         20  
  3         91  
40              
41              
42             [% LOCALE en %]
43              
44             EMAIL_INVALID = "Invalid email address."
45              
46             [% LOCALE de %]
47              
48             # oder "Ungültige Email."
49             EMAIL_INVALID = "Ungültige Email-Adresse."
50              
51             [% LOCALE fr %]
52              
53             EMAIL_INVALID = "Adresse e-mail invalide."
54              
55             [% LOCALE bg %]
56              
57             EMAIL_INVALID = "Невалиден email адрес."
58              
59             __END__
60              
61             =head1 NAME
62              
63             Rose::HTML::Form::Field::Email - Text field that only accepts valid email addresses.
64              
65             =head1 SYNOPSIS
66              
67             $field =
68             Rose::HTML::Form::Field::Email->new(
69             label => 'Email',
70             name => 'email',
71             size => 30,
72             maxlength => 255);
73              
74             if($field->validate)
75             {
76             $email = $field->internal_value;
77             }
78             else
79             {
80             # Handle invalid email addresses
81             }
82              
83             print $field->html;
84              
85             ...
86              
87             =head1 DESCRIPTION
88              
89             L<Rose::HTML::Form::Field::Email> is a subclass of L<Rose::HTML::Form::Field::Text> that uses L<Email::Valid> to allow only valid email addresses as input. 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 email address, or setting an error message and returning false otherwise.
90              
91             This is a good example of a custom field class that simply constrains the kinds of inputs that it accepts, but does not inflate/deflate values or aggregate other fields.
92              
93             =head1 SEE ALSO
94              
95             Other examples of custom fields:
96              
97             =over 4
98              
99             =item L<Rose::HTML::Form::Field::Time>
100              
101             Uses inflate/deflate to coerce input into a fixed format.
102              
103             =item L<Rose::HTML::Form::Field::DateTime>
104              
105             Uses inflate/deflate to convert input to a L<DateTime> object.
106              
107             =item L<Rose::HTML::Form::Field::DateTime::Range>
108              
109             A compound field whose internal value consists of more than one object.
110              
111             =item L<Rose::HTML::Form::Field::PhoneNumber::US::Split>
112              
113             A simple compound field that coalesces multiple subfields into a single value.
114              
115             =item L<Rose::HTML::Form::Field::DateTime::Split::MonthDayYear>
116              
117             A compound field that uses inflate/deflate convert input from multiple subfields into a L<DateTime> object.
118              
119             =item L<Rose::HTML::Form::Field::DateTime::Split::MDYHMS>
120              
121             A compound field that includes other compound fields and uses inflate/deflate convert input from multiple subfields into a L<DateTime> object.
122              
123             =back
124              
125             =head1 AUTHOR
126              
127             John C. Siracusa (siracusa@gmail.com)
128              
129             =head1 LICENSE
130              
131             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.