PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/HTML/FormHandler/Field/PasswordConf.pm

http://github.com/gshank/html-formhandler
Perl | 62 lines | 46 code | 15 blank | 1 comment | 3 complexity | d1c311db2ff6de8cd9e424049c3a4505 MD5 | raw file
 1package HTML::FormHandler::Field::PasswordConf;
 2# ABSTRACT: password confirmation
 3
 4use HTML::FormHandler::Moose;
 5extends 'HTML::FormHandler::Field::Text';
 6
 7=head1 DESCRIPTION
 8
 9This field needs to be declared after the related Password field (or more
10precisely it needs to come after the Password field in the list returned by
11the L<HTML::FormHandler/fields> method).
12
13=head2 password_field
14
15Set this attribute to the name of your password field (default 'password')
16
17Customize error message 'pass_conf_not_matched' or 'required'
18
19    has_field '_password' => ( type => 'PasswordConf',
20         messages => { required => 'You must enter the password a second time' },
21    );
22
23=cut
24
25has '+widget'           => ( default => 'Password' );
26has '+password'         => ( default => 1 );
27has '+required'         => ( default => 1 );
28has 'password_field'    => ( isa     => 'Str', is => 'rw', default => 'password' );
29has 'pass_conf_message' => ( isa     => 'Str', is      => 'rw' );
30
31our $class_messages = {
32    required => 'Please enter a password confirmation',
33    pass_conf_not_matched => 'The password confirmation does not match the password',
34};
35
36sub get_class_messages  {
37    my $self = shift;
38    my $messages = {
39        %{ $self->next::method },
40        %$class_messages,
41    };
42    $messages->{pass_conf_not_matched} = $self->pass_conf_message
43        if $self->pass_conf_message;
44    return $messages;
45}
46
47
48sub validate {
49    my $self = shift;
50
51    my $value    = $self->value;
52    my $password = $self->form->field( $self->password_field )->value || '';
53    if ( $password ne $self->value ) {
54        $self->add_error( $self->get_message('pass_conf_not_matched') );
55        return;
56    }
57    return 1;
58}
59
60__PACKAGE__->meta->make_immutable;
61use namespace::autoclean;
621;