Created
May 9, 2012 10:47
-
-
Save kevindawson/2643667 to your computer and use it in GitHub Desktop.
another e-mail directive for use with Validation::Class::Plugin::FormFields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Email::Valid; | |
use Try::Tiny; | |
use Validation::Class; | |
load {plugins => ['FormFields'],}; | |
directive 'is_email2' => sub { | |
my ($dir, $value, $field, $self) = @_; | |
my $address; | |
try { | |
my $address = Email::Valid->address( | |
-address => $value, #check for address | |
-tldcheck => 1, #check for top level domain | |
-mxcheck => 1, # check dns for mx record | |
-allow_ip => 1, # default on, [1.2.3.4] or 1.2.3.4 | |
-fqdn => 1, # default on, fully qualified domain name | |
); | |
unless ($address) { | |
given ($Email::Valid::Details) { | |
when (/rfc822/) { | |
$field->{errors}->add( | |
'This is not a valid e-mail address (Fails rfc822 specification!)' | |
); | |
return 0; | |
} | |
when (/tldcheck/) { | |
$field->{errors}->add('This is not a valid top level domain!'); | |
return 0; | |
} | |
when (/mxcheck/) { | |
$field->{errors}->add('I can not find a valid mx record!'); | |
return 0; | |
} | |
when (/fqdn/) { | |
$field->{errors}->add('Failed - fully qualified domain name!'); | |
return 0; | |
} | |
#p $Email::Valid::Details; | |
my $handle = $field->{label} || $field->{name}; | |
$field->{errors}->add("$handle dose not compute, error!"); | |
return 0; | |
} | |
} | |
# BOWTIE <bowtie @ cpan.org> -> bowtie@cpan.org | |
$self->set_value($field->{name} => $address); | |
return 1; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment