Debian Codegraph @

lib/HTML/FormHandler/Field/Password.pm

raw
1 package HTML::FormHandler::Field::Password;
2 # ABSTRACT: password field
3 $HTML::FormHandler::Field::Password::VERSION = '0.410002';
4 use HTML::FormHandler::Moose;
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 my $self = shift;
21 my $messages = {
22 %{ $self->next::method },
23 %$class_messages,
24 };
25 $messages->{required} = $self->required_message
26 if $self->required_message;
27 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 my $self = shift;
42
43 $self->noupdate(0);
44 return unless $self->next::method;
45
46 my $value = $self->value;
47 if ( $self->form && $self->ne_username ) {
48 my $username = $self->form->get_param( $self->ne_username );
49 return $self->add_error( $self->get_message('password_ne_username'), $self->ne_username )
50 if $username && $username eq $value;
51 }
52 return 1;
53 }
54
55 __PACKAGE__->meta->make_immutable;
56 use namespace::autoclean;
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.410002
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