/lib/HTML/FormHandler/Field/Email.pm
Perl | 82 lines | 66 code | 15 blank | 1 comment | 3 complexity | 92038571added6686d6ba898cde01a67 MD5 | raw file
1package HTML::FormHandler::Field::Email; 2# ABSTRACT: validates email using Email::Valid 3 4use HTML::FormHandler::Moose; 5extends 'HTML::FormHandler::Field::Text'; 6use Email::Valid; 7 8our $class_messages = { 9 'email_format' => 'Email should be of the format [_1]', 10}; 11has '+html5_type_attr' => ( default => 'email' ); 12 13has 'email_valid_params' => ( 14 is => 'rw', 15 isa => 'HashRef', 16); 17 18has 'preserve_case' => ( 19 is => 'rw', 20 isa => 'Bool', 21); 22 23sub get_class_messages { 24 my $self = shift; 25 return { 26 %{ $self->next::method }, 27 %$class_messages, 28 } 29} 30 31apply( 32 [ 33 { 34 transform => sub { 35 my ( $value, $field ) = @_; 36 return $value 37 if $field->preserve_case; 38 return lc( $value ); 39 } 40 }, 41 { 42 check => sub { 43 my ( $value, $field ) = @_; 44 my $checked = Email::Valid->address( 45 %{ $field->email_valid_params || {} }, 46 -address => $value, 47 ); 48 $field->value($checked) 49 if $checked; 50 }, 51 message => sub { 52 my ( $value, $field ) = @_; 53 return [$field->get_message('email_format'), 'someuser@example.com']; 54 }, 55 } 56 ] 57); 58 59=head1 DESCRIPTION 60 61Validates that the input looks like an email address using L<Email::Valid>. 62Widget type is 'text'. 63 64If form has 'is_html5' flag active it will render <input type="email" ... /> 65instead of type="text" 66 67This field has an 'email_valid_params' attribute that accepts a hash 68reference of extra values passed to L<Email::Valid/address> when 69validating email addresses. 70 71If you want to preserve the case of the email address, set the 72'preserve_case' attribute. 73 74=head1 DEPENDENCIES 75 76L<Email::Valid> 77 78=cut 79 80__PACKAGE__->meta->make_immutable; 81use namespace::autoclean; 821;