Skip to content

Instantly share code, notes, and snippets.

@kevindawson
Created May 9, 2012 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevindawson/2643667 to your computer and use it in GitHub Desktop.
Save kevindawson/2643667 to your computer and use it in GitHub Desktop.
another e-mail directive for use with Validation::Class::Plugin::FormFields
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