File Coverage

blib/lib/CGI/Application/Plugin/ValidateRM.pm
Criterion Covered Total %
statement 54 57 94.7
branch 10 16 62.5
condition 9 13 69.2
subroutine 8 8 100.0
pod 3 4 75.0
total 84 98 85.7


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::ValidateRM;
2 4     4   149944 use base ('Exporter','AutoLoader');
  4         11  
  4         4769  
3 4     4   12177 use HTML::FillInForm;
  4         22879  
  4         161  
4 4     4   5527 use Data::FormValidator;
  4         173484  
  4         214  
5 4     4   51 use strict;
  4         7  
  4         2481  
6              
7             our @EXPORT = qw(
8             &dfv_results
9             &dfv_error_page
10             &check_rm_error_page
11             &check_rm
12             &validate_rm
13             );
14              
15             our $VERSION = '2.5';
16              
17             sub check_rm {
18 9     9 1 29890 my $self = shift;
19 9   50     45 my $return_rm = shift || die 'missing required return run mode';
20 9   50     35 my $profile_in = shift || die 'missing required profile';
21 9   100     70 my $fif_params = shift || {};
22              
23             # If the profile is not a hash reference,
24             # assume it's a CGI::App method
25 9         17 my $profile;
26 9 100       39 if (ref $profile_in eq 'HASH') {
27 1         3 $profile = $profile_in;
28             }
29             else {
30 8 50       63 if ($self->can($profile_in)) {
31 8         32 $profile = $self->$profile_in();
32             }
33             else {
34 0         0 $profile = eval { $self->$profile_in() };
  0         0  
35 0 0       0 die "Error running profile method '$profile_in': $@" if $@;
36             }
37              
38             }
39              
40 9         202 require Data::FormValidator;
41 9         62 my $dfv = Data::FormValidator->new({}, $self->param('dfv_defaults') );
42 9         347 my $r =$dfv->check($self->query,$profile);
43 9         470017 $self->{'__DFV_RESULT'} = $r;
44              
45             # Pass the params through the object so the user
46             # can just call dfv_error_page() later
47 9         93 $self->{'__DFV_RETURN_RM'} = $return_rm;
48 9         25 $self->{'__DFV_FIF_PARAMS'} = $fif_params;
49              
50 9 100       33 if (wantarray) {
51             # We have to call the function non-traditionally to achieve mix-in happiness.
52 7         27 return $r, dfv_error_page($self);
53             }
54             else {
55 2         10 return $r;
56             }
57             }
58              
59             sub dfv_results {
60 1     1 1 3169 my $self = shift;
61 1 50       9 die "must call check_rm() or validate_rm() first." unless defined $self->{'__DFV_RESULT'};
62 1         9 return $self->{'__DFV_RESULT'};
63             }
64              
65             sub validate_rm {
66 6     6 1 25688 my $self = shift;
67 6         36 my ($r,$err_page) = $self->check_rm(@_);
68 6         44 return (scalar $r->valid,$err_page);
69             }
70              
71             sub dfv_error_page {
72 8     8 0 25 my $self = shift;
73 8         21 my $r = $self->{'__DFV_RESULT'};
74 8         18 my $return_rm = $self->{'__DFV_RETURN_RM'};
75 8   100     47 my $fif_params = $self->param('dfv_fif_defaults') || {};
76              
77             # merge the defaults with the ones given for this fill
78 8         175 $fif_params = {%$fif_params, %{$self->{'__DFV_FIF_PARAMS'}}};
  8         27  
79              
80 8         18 my $err_page = undef;
81 8 50 33     43 if ($r->has_missing or $r->has_invalid) {
82             # If ::Forward has been loaded, act like forward()
83 8         89 my $before_rm = $self->{__CURRENT_RUNMODE};
84 8 50       36 $self->{__CURRENT_RUNMODE} = $return_rm if ($INC{'CGI/Application/Plugin/Forward.pm'});
85              
86 8         40 my $return_page = $self->$return_rm($r->msgs);
87              
88 8         80787 $self->{__CURRENT_RUNMODE} = $before_rm;
89              
90 8 100       45 my $return_pageref = (ref($return_page) eq 'SCALAR')
91             ? $return_page : \$return_page;
92              
93 8   100     41 my $fif_class = $self->param('dfv_fif_class') || 'HTML::FillInForm';
94 8         760 eval "require $fif_class";
95             # Deliberately do _not_ check if the eval succeeded,
96             # since $fif_class might be an inlined class not to be found in @INC.
97 8         77 my $fif = $fif_class->new();
98 8         36204 $err_page = $fif->fill(
99             scalarref => $return_pageref,
100             fobject => $self->query,
101             %$fif_params,
102             );
103             }
104 8         3943 return $err_page;
105             }
106              
107             *check_rm_error_page = \&dfv_error_page;
108             my $avoid_warning = \&check_rm_error_page;
109              
110             1;
111             __END__