Subject: | Email-Valid fixups ignored |
Usually Email::Valid performs basic fixups to the email address before
checking it.
So the following line should print 'test@example.com' and not 'test @
example . com'
perl -MEmail::Valid -E 'my $address = Email::Valid->address("test @
example . com"); say $address'
When using the Email field type in HTML-FormHandler the return value of
Email::Valid however is ignored, thus returning "test @ example . com"
which of course is not a valid email address. The attached patch fixes
this issue and adds some basic tests.
Subject: | email_valid.patch |
commit a6af9022690693e3553ea3349b20ac028b2febf5
Author: Maros Kollar <maros@k-1.com>
Date: Tue Feb 14 16:54:34 2012 +0100
Honour Email-Valid fixups
diff --git a/lib/HTML/FormHandler/Field/Email.pm b/lib/HTML/FormHandler/Field/Email.pm
index a4aaf85..c085d79 100644
--- a/lib/HTML/FormHandler/Field/Email.pm
+++ b/lib/HTML/FormHandler/Field/Email.pm
@@ -25,7 +25,12 @@ apply(
transform => sub { lc( $_[0] ) }
},
{
- check => sub { Email::Valid->address( $_[0] ) },
+ check => sub {
+ my ( $value, $field ) = @_;
+ my $checked = Email::Valid->address( $value );
+ $field->value($checked)
+ if $checked;
+ },
message => sub {
my ( $value, $field ) = @_;
return [$field->get_message('email_format'), 'someuser@example.com'];
diff --git a/t/fields/fields.t b/t/fields/fields.t
index ce1f675..853debf 100644
--- a/t/fields/fields.t
+++ b/t/fields/fields.t
@@ -76,6 +76,11 @@ $field->validate_field;
ok( !$field->has_errors, 'Test for errors 2' );
is( $field->value, lc($Address), 'is value input string' );
+$field->_set_input( 'test @ example . com' );
+$field->validate_field;
+ok( !$field->has_errors, 'Test for errors 3' );
+is( $field->value, 'test@example.com', 'is email-valid corrected input string' );
+
# hidden
$class = 'HTML::FormHandler::Field::Hidden';