/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

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