File Coverage

blib/lib/HTML/FormHandler/Field/Password.pm
Criterion Covered Total %
statement 19 19 100.0
branch 7 8 87.5
condition 4 6 66.6
subroutine 4 4 100.0
pod 1 2 50.0
total 35 39 89.7


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Field::Password;
2             # ABSTRACT: password field
3             $HTML::FormHandler::Field::Password::VERSION = '0.40068';
4 6     6   4577 use HTML::FormHandler::Moose;
  6         15  
  6         65  
5             extends 'HTML::FormHandler::Field::Text';
6              
7              
8             has '+widget' => ( default => 'Password' );
9             has '+password' => ( default => 1 );
10             has 'ne_username' => ( isa => 'Str', is => 'rw' );
11             has '+type_attr' => ( default => 'password' );
12             has '+html5_type_attr' => ( default => 'password' );
13              
14             our $class_messages = {
15             'required' => 'Please enter a password in this field',
16             'password_ne_username' => 'Password must not match [_1]',
17             };
18              
19             sub get_class_messages {
20 3     3 0 7 my $self = shift;
21             my $messages = {
22 3         7 %{ $self->next::method },
  3         16  
23             %$class_messages,
24             };
25 3 50       102 $messages->{required} = $self->required_message
26             if $self->required_message;
27 3         50 return $messages;
28             }
29              
30              
31             after 'validate_field' => sub {
32             my $self = shift;
33              
34             if ( !$self->required && !( defined( $self->value ) && length( $self->value ) ) ) {
35             $self->noupdate(1);
36             $self->clear_errors;
37             }
38             };
39              
40             sub validate {
41 8     8 1 19 my $self = shift;
42              
43 8         216 $self->noupdate(0);
44 8 100       43 return unless $self->next::method;
45              
46 7         21 my $value = $self->value;
47 7 100 66     175 if ( $self->form && $self->ne_username ) {
48 4         99 my $username = $self->form->get_param( $self->ne_username );
49 4 100 66     39 return $self->add_error( $self->get_message('password_ne_username'), $self->ne_username )
50             if $username && $username eq $value;
51             }
52 6         19 return 1;
53             }
54              
55             __PACKAGE__->meta->make_immutable;
56 6     6   17199 use namespace::autoclean;
  6         17  
  6         113  
57             1;
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             HTML::FormHandler::Field::Password - password field
68              
69             =head1 VERSION
70              
71             version 0.40068
72              
73             =head1 DESCRIPTION
74              
75             The password field has a default minimum length of 6, which can be
76             easily changed:
77              
78             has_field 'password' => ( type => 'Password', minlength => 7 );
79              
80             It does not come with additional default checks, since password
81             requirements vary so widely. There are a few constraints in the
82             L<HTML::FormHandler::Types> modules which could be used with this
83             field: NoSpaces, WordChars, NotAllDigits.
84             These constraints can be used in the field definitions 'apply':
85              
86             use HTML::FormHandler::Types ('NoSpaces', 'WordChars', 'NotAllDigits' );
87             ...
88             has_field 'password' => ( type => 'Password',
89             apply => [ NoSpaces, WordChars, NotAllDigits ],
90             );
91              
92             You can add your own constraints in addition, of course.
93              
94             If a password field is not required, then the field will be marked 'noupdate',
95             to prevent a null from being saved into the database.
96              
97             =head2 ne_username
98              
99             Set this attribute to the name of your username field (default 'username')
100             if you want to check that the password is not the same as the username.
101             Does not check by default.
102              
103             =head1 AUTHOR
104              
105             FormHandler Contributors - see HTML::FormHandler
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is copyright (c) 2017 by Gerda Shank.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =cut