File Coverage

blib/lib/HTML/Template/Associate/FormValidator.pm
Criterion Covered Total %
statement 82 82 100.0
branch 10 16 62.5
condition n/a
subroutine 21 21 100.0
pod 2 2 100.0
total 115 121 95.0


line stmt bran cond sub pod time code
1             package HTML::Template::Associate::FormValidator;
2 1     1   6 use strict;
  1         4  
  1         34  
3              
4             BEGIN {
5 1     1   5 use Exporter ();
  1         2  
  1         27  
6 1     1   6 require HTML::Template::Associate;
7 1     1   5 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         2  
  1         145  
8 1         2 $VERSION = '2.01';
9 1         24 @ISA = qw ( HTML::Template::Associate Exporter);
10             #Give a hoot don't pollute, do not export more than needed by default
11 1         3 @EXPORT = qw ();
12 1         2 @EXPORT_OK = qw ();
13 1         21 %EXPORT_TAGS = ();
14             }
15              
16 1     1   8 use constant CHECK_TYPE => 'Data::FormValidator::Results';
  1         166  
  1         75  
17 1     1   5 use constant ERROR_WRONG_TYPE => 'This class does not deal with that kind of result object';
  1         2  
  1         42  
18 1     1   5 use constant TMPL_PREFIX_VALID => 'VALID_';
  1         2  
  1         38  
19 1     1   4 use constant TMPL_PREFIX_MISSING => 'MISSING_';
  1         2  
  1         39  
20 1     1   8 use constant TMPL_PREFIX_INVALID => 'INVALID_';
  1         1  
  1         39  
21 1     1   4 use constant TMPL_PREFIX_UNKNOWN => 'UNKNOWN_';
  1         2  
  1         44  
22 1     1   5 use constant TMPL_PREFIX_MSGS => 'MSGS_';
  1         2  
  1         43  
23 1     1   5 use constant TMPL_POSTFIX_LOOP => 'FIELDS';
  1         2  
  1         40  
24 1     1   6 use constant METHOD_VALID => 'valid';
  1         2  
  1         59  
25 1     1   6 use constant METHOD_MISSING => 'missing';
  1         2  
  1         44  
26 1     1   4 use constant METHOD_INVALID => 'invalid';
  1         2  
  1         41  
27 1     1   5 use constant METHOD_UNKNOWN => 'unknown';
  1         1  
  1         45  
28 1     1   5 use constant METHOD_MSGS => 'msgs';
  1         2  
  1         41  
29 1     1   5 use constant TMPL_LOOP_FIELDNAME => 'FIELD_NAME';
  1         1  
  1         61  
30 1     1   5 use constant TMPL_LOOP_FIELDVALUE => 'FIELD_VALUE';
  1         2  
  1         584  
31              
32             ########################################### main pod documentation begin ##
33              
34             =head1 NAME
35              
36             HTML::Template::Associate::FormValidator - HTML::Template::Associate Data::FormValidator plugin
37              
38             =head1 SYNOPSIS
39              
40             This class is not intended to be used directly but rather through a
41             HTML::Template::Associate. It provides concrete class functionality, it
42             will take Data::FormValidator::Results object and reconstruct data structure
43             to one appropriate for use by the HTML::Template.
44            
45             use CGI qw/:standard/;
46             use Data::FormValidator;
47             use HTML::Template;
48             use HTML::Template::Associate;
49            
50             my $cgi = CGI->new;
51             #for testing purposes we can add some input to our cgi object
52             $cgi->param( 'fullname', 'John Doe' );
53             $cgi->param( 'phone', 6041112222 );
54             $cgi->param( 'email', 'invalid@email' );
55            
56             my $input_profile = {
57             optional => [ qw( company fax country ) ],
58             required => [ qw( fullname phone email address city state zipcode ) ],
59             constraints => {
60             email => 'email',
61             fax => 'american_phone',
62             phone => 'american_phone',
63             zipcode => '/^\s*\d{5}(?:[-]\d{4})?\s*$/',
64             state => "state",
65             },
66             defaults => { country => "Canada" },
67             msgs => {
68             prefix=> 'error_',
69             missing => 'Not Here!',
70             invalid => 'Problematic!',
71             invalid_seperator => '
',
72             format => 'ERROR: %s',
73             any_errors => 'some_errors',
74             }
75             };
76            
77             my $validator = Data::FormValidator->new;
78             my $results = $validator->check ( scalar $cgi->Vars, $input_profile );
79            
80             my $associate = HTML::Template::Associate->new( {
81             target => 'FormValidator',
82             results => $results,
83             extra_arguments => [ $validator ] #not needed but just illustrated
84             } );
85            
86             my $template = HTML::Template->new(
87             filename => 'test.tmpl',
88             associate => [ $cgi, $associate ]
89             );
90            
91             print $template->output;
92            
93             #and in our test.tmpl file we could have
94            
95             Valid Fields:
96            
97             Field Name:
98             Field Value:
99            
100            
101             Missing Fields:
102            
103             Field Name:
104             Field Value:
105            
106            
107            
108             Phone: you supplied is invalid.
109            
110            
111            
112             City name is missing, please fix this.
113            
114            
115            
117            
118             I think is very big country.
119            
120            
121            
122             Message Fields:
123            
124            
125             Field Name:
126             Field Value:
127            
128            
129            
130             Our default error message set in the profiling code is:
131            
132            
133              
134             The following will become available to your associate object/template:
135            
136             Key / Perl / Template
137            
138             Arrays / Loops
139            
140             VALID_FIELDS / $associate->param(q{VALID_FIELDS}); /
141             MISSING_FIELDS / $associate->param(q{MISSING_FIELDS}); /
142             INVALID_FIELDS / $associate->param(q{INVALID_FIELDS}); /
143             UNKNOWN_FIELDS / $associate->param(q{UNKNOWN_FIELDS}); /
144             MSGS_FIELDS / $associate->param(q{MSGS_FIELDS}); /
145            
146             Variables
147            
148             VALID_ParamA / $associate->param(q{VALID_ParamA}); /
149             MISSING_ParamB / $associate->param(q{MISSING_ParamB}); /
150             INVALID_ParamC / $associate->param(q{INVALID_ParamC}); /
151             UNKNOWN_ParamD / $associate->param(q{UNKNOWN_ParamD}); /
152             MSGS_prefix_ParamE / $associate->param(q{MSGS_prefix_ParamE}); /
153            
154             Inside Array / Loops we have the following structure:
155            
156             Perl
157            
158             VALID_FIELDS => [ { FIELD_NAME => X }, FIELD_VALUE => Y }, ... ]
159            
160             Template
161            
162            
163            
164            
165            
166            
167             For further explanation on what the VALID,MISSING,INVALID,UNKNOWN AND MSGS are
168             please refer to Data::FormValidator::Results. Please note that MSGS
169             works somewhat diffrently then others and corresponds to $results->msgs([$config])
170             interface.
171              
172             =head1 DESCRIPTION
173              
174             Map Data::FormValidator::Results object into a form suitable for use by HTML::Template
175              
176             =head1 USAGE
177              
178             See above.
179              
180             =head1 BUGS
181              
182             If you find any please report to author.
183              
184             =head1 SUPPORT
185              
186             See License.
187              
188             =head1 AUTHOR
189              
190             Alex Pavlovic
191             alex.pavlovic@taskforce-1.com
192             http://www.taskforce-1.com
193              
194             =head1 COPYRIGHT
195              
196             This program is free software; you can redistribute
197             it and/or modify it under the same terms as Perl itself.
198            
199             The full text of the license can be found in the
200             LICENSE file included with this module.
201              
202              
203             =head1 SEE ALSO
204              
205             HTML::Template::Associate HTML::Template::Associate::DBI perl(1).
206              
207             =cut
208              
209             ############################################# main pod documentation end ##
210              
211              
212             ################################################ subroutine header begin ##
213              
214             =head2 init
215              
216             Usage : $associate->init ( $results, $extra_arguments );
217             Purpose : Initiliazes the object
218             Returns : concrete object instance
219             Argument : Data::FormValidator::Results instance and extra hash of arguments passed to factory
220             Comments : Factory class will call this method automatically during concrete object construction
221             : Error is thrown depending whether the passed in results object is of correct type
222              
223             See Also : HTML::Template::Associate Data::FormValidator::Results
224              
225             =cut
226              
227             ################################################## subroutine header end ##
228              
229              
230             sub init {
231 1     1 1 3 my ( $self, $params ) = @_;
232 1         2 my $results = $params->{results};
233 1 50       19 $self->error ( ERROR_WRONG_TYPE ) unless $results->isa(CHECK_TYPE);
234 1         3 $self->runloop ( $results, METHOD_VALID, TMPL_PREFIX_VALID );
235 1 50       4 $self->runloop ( $results, METHOD_MISSING, TMPL_PREFIX_MISSING ) if $results->has_missing;
236 1 50       5 $self->runloop ( $results, METHOD_INVALID, TMPL_PREFIX_INVALID ) if $results->has_invalid;
237 1 50       11 $self->runloop ( $results, METHOD_UNKNOWN, TMPL_PREFIX_UNKNOWN ) if $results->has_unknown;
238 1 50       10 $self->runloop ( $results, METHOD_MSGS, TMPL_PREFIX_MSGS ) if keys %{$results->msgs};
  1         7  
239 1         5 return $self;
240             }
241              
242             ################################################ subroutine header begin ##
243              
244             =head2 runloop
245              
246             Usage : used internally to assign various prefixes/names to variables and loops
247              
248             =cut
249              
250             ################################################## subroutine header end ##
251              
252             sub runloop {
253 3     3 1 171 my ( $self, $results, $method, $field_prefix ) = @_;
254 1         14 my @fields = ref $results->$method eq q{ARRAY} ?
255 3 100       14 @{ $results->$method } : keys %{ $results->$method };
  2         126  
256 3         130 for my $field ( @fields ) {
257 16 100       56 my $field_value = ref $results->$method eq q{ARRAY} ?
258             $results->$method ( $field ) : $results->$method->{$field};
259            
260 16 50       2023 $field_value = ref $field_value eq q{ARRAY} ?
261             join q{,}, @$field_value : $field_value;
262            
263 16         158 $self->param ( $field_prefix . $field, $field_value );
264 16         26 my $loop_name = $field_prefix . TMPL_POSTFIX_LOOP;
265 16         19 push @{ $self->{PARAMS}->{$loop_name} }, {
  16         196  
266             &TMPL_LOOP_FIELDNAME => $field,
267             &TMPL_LOOP_FIELDVALUE => $field_value
268             };
269             }
270 3         7 return $self;
271             }
272              
273             1; #this line is important and will help the module return a true value
274             __END__