| 1 | package Data::FormValidator::EmailValid; | ||||||||||
| 2 | |||||||||||
| 3 | ############################################################################### | ||||||||||
| 4 | # Required inclusions. | ||||||||||
| 5 | ############################################################################### | ||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Email::Valid; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | ############################################################################### | ||||||||||
| 11 | # Make our methods exportable | ||||||||||
| 12 | ############################################################################### | ||||||||||
| 13 | 2 | ▶ | use base qw( Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | our @EXPORT_OK = qw( | |||||||||
| 15 | FV_email_filter | ||||||||||
| 16 | FV_email | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | ############################################################################### | ||||||||||
| 20 | # Version number. | ||||||||||
| 21 | ############################################################################### | ||||||||||
| 22 | 2 | our $VERSION = '0.09'; | |||||||||
| 23 | |||||||||||
| 24 | ############################################################################### | ||||||||||
| 25 | # Subroutine: FV_email_filter(%options) | ||||||||||
| 26 | # Parameters: %options - Options for Email::Valid | ||||||||||
| 27 | ############################################################################### | ||||||||||
| 28 | # Filter method which cleans up the given value and returns valid e-mail | ||||||||||
| 29 | # addresses (or nothing, if the value isn't a valid e-mail address). | ||||||||||
| 30 | # | ||||||||||
| 31 | # "Valid" is deemed to mean "looks like an e-mail"; no other tests are done to | ||||||||||
| 32 | # ensure that a valid MX exists or that the address is actually deliverable. | ||||||||||
| 33 | # | ||||||||||
| 34 | # This filter method automatically converts all e-mail addresses to lower-case. | ||||||||||
| 35 | # This behaviour can be disabled by passing through an 'lc=>0' option. | ||||||||||
| 36 | # | ||||||||||
| 37 | # You may also pass through any additional 'Email::Valid' '%options' that you | ||||||||||
| 38 | # want to use; they're handed straight through to 'Email::Valid'. | ||||||||||
| 39 | ############################################################################### | ||||||||||
| 40 | 8 | ▶ | sub FV_email_filter { | ||||||||
|
Subroutine
FV_email_filter: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 41 | 8 | my %options = @_; | |||||||||
| 42 | # check if we should mangle the e-mail to all lower-case (default yes) | ||||||||||
| 43 | 8 | my $mangle_lc = 1; | |||||||||
| 44 | 8 | ▶ | $mangle_lc = delete $options{'lc'} if (exists $options{'lc'}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | # return filter closure | ||||||||||
| 46 | 8 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 47 | 8 | my $email = shift; | |||||||||
| 48 | 8 | ▶ | return Email::Valid->address( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | '-address' => $mangle_lc ? lc($email) : $email, | ||||||||||
| 50 | '-fudge' => 1, | ||||||||||
| 51 | '-mxcheck' => 0, | ||||||||||
| 52 | %options, | ||||||||||
| 53 | ); | ||||||||||
| 54 | 8 | }; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | ############################################################################### | ||||||||||
| 58 | # Subroutine: FV_email(%options) | ||||||||||
| 59 | # Parameters: %options - Options for Email::Valid | ||||||||||
| 60 | ############################################################################### | ||||||||||
| 61 | # Constraint method which checks to see if the value being constrained is a | ||||||||||
| 62 | # valid e-mail address or not. Returns true if the e-mail address is valid, | ||||||||||
| 63 | # false otherwise. | ||||||||||
| 64 | # | ||||||||||
| 65 | # This differs from the "email" constraint that comes with | ||||||||||
| 66 | # 'Data::FormValidator' in that we not only check to make sure that the e-mail | ||||||||||
| 67 | # looks valid, but also that a valid MX record exists for the address. No | ||||||||||
| 68 | # other checks are done to ensure that the address is actually deliverable, | ||||||||||
| 69 | # however. | ||||||||||
| 70 | # | ||||||||||
| 71 | # You can also pass through any additional 'Email::Valid' '%options' that you | ||||||||||
| 72 | # want to use; they're handed straight through to 'Email::Valid'. | ||||||||||
| 73 | ############################################################################### | ||||||||||
| 74 | 2 | ▶ | sub FV_email { | ||||||||
|
Subroutine
FV_email: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 75 | 2 | my %options = @_; | |||||||||
| 76 | 2 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 77 | 2 | my $dfv = shift; | |||||||||
| 78 | # get the value we're constraining | ||||||||||
| 79 | 2 | my $val = $dfv->get_current_constraint_value(); | |||||||||
| 80 | # check for valid e-mail address | ||||||||||
| 81 | 2 | my $rc = Email::Valid->address( | |||||||||
| 82 | '-address' => $val, | ||||||||||
| 83 | '-mxcheck' => 1, | ||||||||||
| 84 | %options, | ||||||||||
| 85 | ); | ||||||||||
| 86 | 2 | return defined $rc; | |||||||||
| 87 | 2 | }; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 2 | 1; | |||||||||
| 91 | |||||||||||
| 92 | =for stopwords MX | ||||||||||
| 93 | |||||||||||
| 94 | =head1 NAME | ||||||||||
| 95 | |||||||||||
| 96 | Data::FormValidator::EmailValid - Data::FormValidator e-mail address constraint/filter | ||||||||||
| 97 | |||||||||||
| 98 | =head1 SYNOPSIS | ||||||||||
| 99 | |||||||||||
| 100 | use Data::FormValidator::EmailValid qw(FV_email_filter FV_email); | ||||||||||
| 101 | |||||||||||
| 102 | my $results = Data::FormValidator->check( | ||||||||||
| 103 | { | ||||||||||
| 104 | 'email' => 'Graham TerMarsch <cpan@howlingfrog.com>', | ||||||||||
| 105 | }, | ||||||||||
| 106 | { | ||||||||||
| 107 | 'required' => [qw( email )], | ||||||||||
| 108 | 'field_filters' => { | ||||||||||
| 109 | 'email' => FV_email_filter(), | ||||||||||
| 110 | }, | ||||||||||
| 111 | 'constraint_methods' => { | ||||||||||
| 112 | 'email' => FV_email(), | ||||||||||
| 113 | }, | ||||||||||
| 114 | }, | ||||||||||
| 115 | ); | ||||||||||
| 116 | |||||||||||
| 117 | =head1 DESCRIPTION | ||||||||||
| 118 | |||||||||||
| 119 | C<Data::FormValidator::EmailValid> implements a constraint and filter for use | ||||||||||
| 120 | with C<Data::FormValidator> that do e-mail address validation/verification | ||||||||||
| 121 | using C<Email::Valid>. | ||||||||||
| 122 | |||||||||||
| 123 | Although I generally find that I'm using the filter and constraint together, | ||||||||||
| 124 | they've been separated so that you could use just one or the other (e.g. you | ||||||||||
| 125 | may want to constrain on valid e-mail addresses without actually cleaning up or | ||||||||||
| 126 | filtering any of the data provided to you by the user). | ||||||||||
| 127 | |||||||||||
| 128 | =head1 METHODS | ||||||||||
| 129 | |||||||||||
| 130 | =over | ||||||||||
| 131 | |||||||||||
| 132 | =item FV_email_filter(%options) | ||||||||||
| 133 | |||||||||||
| 134 | Filter method which cleans up the given value and returns valid e-mail | ||||||||||
| 135 | addresses (or nothing, if the value isn't a valid e-mail address). | ||||||||||
| 136 | |||||||||||
| 137 | "Valid" is deemed to mean "looks like an e-mail"; no other tests are done | ||||||||||
| 138 | to ensure that a valid MX exists or that the address is actually | ||||||||||
| 139 | deliverable. | ||||||||||
| 140 | |||||||||||
| 141 | This filter method automatically converts all e-mail addresses to | ||||||||||
| 142 | lower-case. This behaviour can be disabled by passing through an | ||||||||||
| 143 | C<lc=E<gt>0> option. | ||||||||||
| 144 | |||||||||||
| 145 | You may also pass through any additional C<Email::Valid> C<%options> that | ||||||||||
| 146 | you want to use; they're handed straight through to C<Email::Valid>. | ||||||||||
| 147 | |||||||||||
| 148 | =item FV_email(%options) | ||||||||||
| 149 | |||||||||||
| 150 | Constraint method which checks to see if the value being constrained is a | ||||||||||
| 151 | valid e-mail address or not. Returns true if the e-mail address is valid, | ||||||||||
| 152 | false otherwise. | ||||||||||
| 153 | |||||||||||
| 154 | This differs from the "email" constraint that comes with | ||||||||||
| 155 | C<Data::FormValidator> in that we not only check to make sure that the | ||||||||||
| 156 | e-mail looks valid, but also that a valid MX record exists for the address. | ||||||||||
| 157 | No other checks are done to ensure that the address is actually | ||||||||||
| 158 | deliverable, however. | ||||||||||
| 159 | |||||||||||
| 160 | You can also pass through any additional C<Email::Valid> C<%options> that | ||||||||||
| 161 | you want to use; they're handed straight through to C<Email::Valid>. | ||||||||||
| 162 | |||||||||||
| 163 | =back | ||||||||||
| 164 | |||||||||||
| 165 | =head1 AUTHOR | ||||||||||
| 166 | |||||||||||
| 167 | Graham TerMarsch (cpan@howlingfrog.com) | ||||||||||
| 168 | |||||||||||
| 169 | =head1 COPYRIGHT | ||||||||||
| 170 | |||||||||||
| 171 | Copyright (C) 2007, Graham TerMarsch. All Rights Reserved. | ||||||||||
| 172 | |||||||||||
| 173 | This is free software; you can redistribute it and/or modify it under the same | ||||||||||
| 174 | license as Perl itself. | ||||||||||
| 175 | |||||||||||
| 176 | =head1 SEE ALSO | ||||||||||
| 177 | |||||||||||
| 178 | L<Data::FormValidator>, | ||||||||||
| 179 | L<Email::Valid>. | ||||||||||
| 180 | |||||||||||
| 181 | =cut | ||||||||||