File Coverage

blib/lib/Data/MuForm/Field/Email.pm
Criterion Covered Total %
statement 17 22 77.2
branch 4 6 66.6
condition n/a
subroutine 6 8 75.0
pod 0 2 0.0
total 27 38 71.0


line stmt bran cond sub pod time code
1             package Data::MuForm::Field::Email;
2             # ABSTRACT: validates email using Email::Valid
3              
4 2     2   1496 use Moo;
  2         8145  
  2         8  
5             extends 'Data::MuForm::Field::Text';
6 2     2   1767 use Types::Standard -types;
  2         47873  
  2         16  
7              
8 2     2   6189 use Email::Valid;
  2         170104  
  2         480  
9              
10             our $class_messages = {
11             'email_format' => 'Email should be of the format [_1]',
12             };
13              
14             has '+html5_input_type' => ( default => 'email' );
15             has 'email_valid_params' => ( is => 'rw', isa => HashRef );
16             has 'preserve_case' => ( is => 'rw' );
17              
18             sub get_class_messages {
19 0     0 0 0 my $self = shift;
20             return {
21 0         0 %{ $self->next::method },
  0         0  
22             %$class_messages,
23             }
24             }
25              
26             sub build_base_apply {
27             [
28             {
29             transform => sub {
30 4     4   4 my ( $value, $field ) = @_;
31 4 100       15 return $value
32             if $field->preserve_case;
33 2         7 return lc( $value );
34             }
35             },
36             {
37             check => sub {
38 4     4   5 my ( $value, $field ) = @_;
39             my $checked = Email::Valid->address(
40 4 50       4 %{ $field->email_valid_params || {} },
  4         72  
41             -address => $value,
42             );
43 4 50       2930 $field->value($checked)
44             if $checked;
45             },
46             message => sub {
47 0     0   0 my ( $value, $field ) = @_;
48 0         0 return [$field->get_message('email_format'), 'someuser@example.com'];
49             },
50             }
51 2     2 0 67 ]
52             }
53              
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Data::MuForm::Field::Email - validates email using Email::Valid
66              
67             =head1 VERSION
68              
69             version 0.04
70              
71             =head1 DESCRIPTION
72              
73             Validates that the input looks like an email address using L<Email::Valid>.
74             Widget type is 'text'.
75              
76             If form has 'is_html5' flag active it will render <input type="email" ... />
77             instead of type="text"
78              
79             This field has an 'email_valid_params' attribute that accepts a hash
80             reference of extra values passed to L<Email::Valid/address> when
81             validating email addresses.
82              
83             If you want to preserve the case of the email address, set the
84             'preserve_case' attribute.
85              
86             =head1 DEPENDENCIES
87              
88             L<Email::Valid>
89              
90             =head1 AUTHOR
91              
92             Gerda Shank
93              
94             =head1 COPYRIGHT AND LICENSE
95              
96             This software is copyright (c) 2017 by Gerda Shank.
97              
98             This is free software; you can redistribute it and/or modify it under
99             the same terms as the Perl 5 programming language system itself.
100              
101             =cut