File Coverage

blib/lib/Gantry/Utils/FormErrors.pm
Criterion Covered Total %
statement 3 28 10.7
branch 0 8 0.0
condition 0 4 0.0
subroutine 1 8 12.5
pod 7 7 100.0
total 11 55 20.0


line stmt bran cond sub pod time code
1             package Gantry::Utils::FormErrors;
2 1     1   1161 use strict;
  1         2  
  1         339  
3              
4             sub new {
5 0     0 1   my ( $class, $opts ) = @_;
6              
7             my $self = {
8             missing => $opts->{ missing } || {},
9             invalid => $opts->{ invalid } || {},
10 0   0       };
      0        
11              
12 0           return bless $self, $class;
13             }
14              
15             sub has_missing {
16 0     0 1   my $self = shift;
17              
18 0 0         return 1 if ( $self->missing );
19              
20 0           return;
21             }
22              
23             sub has_invalid {
24 0     0 1   my $self = shift;
25              
26 0 0         return 1 if ( $self->invalid );
27              
28 0           return;
29             }
30              
31             sub missing {
32 0     0 1   my $self = shift;
33 0           my $candidate = shift;
34              
35 0 0         if ( $candidate ) { return $self->{ missing }{ $candidate }; }
  0            
36 0           else { return keys %{ $self->{ missing } }; }
  0            
37             }
38              
39             sub invalid {
40 0     0 1   my $self = shift;
41 0           my $candidate = shift;
42              
43 0 0         if ( $candidate ) { return $self->{ invalid }{ $candidate }; }
  0            
44 0           else { return keys %{ $self->{ invalid } }; }
  0            
45             }
46              
47             sub get_missing_hash {
48 0     0 1   my $self = shift;
49              
50 0           return $self->{ missing };
51             }
52              
53             sub get_invalid_hash {
54 0     0 1   my $self = shift;
55              
56 0           return $self->{ invalid };
57             }
58              
59             1;
60              
61             =head1 NAME
62              
63             Gantry::Utils::FormErrors - A CRUD form validation error object
64              
65             =head1 SYNOPSIS
66              
67             A typical example:
68              
69             use Gantry::Plugins::CRUD;
70             use Gantry::Utils::FormErrors;
71              
72             my $crud_obj = Gantry::Plugins::CRUD->new(
73             #...
74             validator => \&my_validator,
75             );
76              
77             sub my_validator {
78             my $opts = shift;
79             my $params = $opts->{ params };
80             my $form = $opts->{ form };
81              
82             my %missing;
83             my @errors;
84              
85             if ( not $params->{ password } ) {
86             $missing{ password }++;
87             }
88             if ( $params->{ password } =~ /$params->{ user_name }/ ) {
89             push @errors, 'Password cannot contain user name';
90             }
91             # ... other similar tests
92              
93             my $error_text = join "\n
Error: ", @errors;
94              
95             $form->{ error_text } = $error_text;
96              
97             return Gantry::Utils::FormErrors->new(
98             {
99             missing => \%missing,
100             }
101             );
102             }
103              
104             The rest is handled by the CRUD plugin and the default template (form.tt).
105              
106             =head1 DESCRIPTION
107              
108             This module provides objects which respond to the same API as
109             Data::FormValidator::Results (or at least the parts of that API which
110             Gantry normally uses).
111              
112             Use this module in your Gantry::Plugins::CRUD validator callback.
113              
114             =head1 METHODS
115              
116             =over 4
117              
118             =item new
119              
120             Constructor, expects a hash reference with the following keys (all are
121             optional):
122              
123             missing - a hash reference keyed by missing field names
124             invalid - a hash reference keyed by invalid field names
125              
126             =item has_missing
127              
128             Returns 1 if there are any keys in the missing hash.
129              
130             =item has_invalid
131              
132             Same as has_missing, except that it checks the invalid hash.
133              
134             =item missing
135              
136             If called without arguments, returns number of missing fields. If
137             call with the name of a field, returns 1 if that field is a key in the
138             missing hash and 0 otherwise.
139              
140             =item invalid
141              
142             Same as missing, but checks the invalid hash.
143              
144             =item get_missing_hash
145              
146             Returns the hash reference of missing fields. Keys are field names values
147             are usually 1 (but they must be true).
148              
149             This is useful if two validation routines are cooperating to form the
150             final lists.
151              
152             =item get_invalid_hash
153              
154             Returns the hash reference of invalid fields. Keys are field names values
155             are usually 1 (but they must be true).
156              
157             This is useful if two validation routines are cooperating to form the
158             final lists.
159              
160             =back
161              
162             =head1 AUTHOR
163              
164             Phil Crow, Ephilcrow2000@yayoo.com
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             Copyright (C) 2006 Phil Crow
169              
170             This library is free software; you can redistribute it and/or modify
171             it under the same terms as Perl itself, either Perl version 5.8.6 or,
172             at your option, any later version of Perl 5 you may have available.
173              
174             =cut