[svn-inject] Installing original source of libcgi-application-plugin-validaterm-perl (2.3)
Nicholas Bamber
12 years ago
0 | use Module::Build; | |
1 | my $class = Module::Build->subclass( | |
2 | class => 'My::Builder', | |
3 | code => q{ | |
4 | sub ACTION_dist { | |
5 | my $self = shift; | |
6 | `perldoc -t lib/CGI/Application/Plugin/ValidateRM.pm>README`; | |
7 | $self->SUPER::ACTION_dist; | |
8 | } | |
9 | }, | |
10 | ); | |
11 | ||
12 | ||
13 | $class->new( | |
14 | module_name => 'CGI::Application::Plugin::ValidateRM', | |
15 | license => 'perl', | |
16 | requires => { | |
17 | 'CGI::Application' => 3.0, | |
18 | 'Data::FormValidator' => 3.70, # for boolean overloading. | |
19 | 'HTML::FillInForm' => 1.07, # Fixes test-suite failure | |
20 | }, | |
21 | create_makefile_pl => 'traditional', | |
22 | sign=>0, | |
23 | )->create_build_script; | |
24 |
0 | Revision history for Perl extension CGI::Application::Plugin::ValidateRM. | |
1 | ||
2 | 2.3 Wed Oct 22 23:05:29 EDT 2008 | |
3 | - Fix broken test in t/forward.t (#35056) | |
4 | No code changes. | |
5 | ||
6 | 2.2 Tue Apr 29 10:44:57 EDT 2008 | |
7 | - Fix test failure by requiring at least HTML::FillInForm 1.07 (RT#35056) | |
8 | No code changes. | |
9 | ||
10 | 2.1 Sun Jul 31 21:17:23 EST 2005 | |
11 | ||
12 | - Added experimental support for ::Forward; | |
13 | ||
14 | 2.00 Tue Jul 19 22:01:35 EST 2005 | |
15 | ||
16 | - Added new syntax for calling check_rm() in scalar context, returning just | |
17 | the $result object. | |
18 | ||
19 | - Added accessor method for the error page, named check_rm_error_page() and aliased | |
20 | as dfv_error_page(). | |
21 | ||
22 | - Added dfv_results() accessor method, primarily of interest to other plug-in authors. | |
23 | ||
24 | - Switched back to exporting by default, but you can still explicitly | |
25 | import if you want to. | |
26 | ||
27 | 1.22 Sun Nov 28 22:24:03 EST 2004 | |
28 | (No code changes) | |
29 | - Update SIGNATURE to use key that hopefully works. | |
30 | ||
31 | 1.21 Mon Sep 6 09:01:30 EST 2004 | |
32 | - Added example of using dfv_defaults | |
33 | ||
34 | 1.20 ?? | |
35 | - renamed from ::ValidateRM to ::Plugin::ValidateRM | |
36 | - changed export policy to not export by default. | |
37 | ||
38 | 1.12 Sat May 08 2004 | |
39 | - Options can now be passed through to HTML::FillInForm (Gabor Szabo) | |
40 | - tests cleaned up to be more sensible | |
41 | ||
42 | 1.11 Thu Feb 26 2004 | |
43 | - Document appropriate support mailing list | |
44 | ||
45 | 1.10 Mon Dec 01 2003 | |
46 | - CGI::Carp is no longer used. (Steve Hay) | |
47 | ||
48 | - Module::Build is no longer required for installation | |
49 | ||
50 | - support has been added for returning scalar refs from | |
51 | the run mode being validated. (Steve Hay) | |
52 | ||
53 | - use of eval / die has been minimized a bit. (Steve Hay) | |
54 | ||
55 | 1.07 Wed Oct 15 2003 | |
56 | - Documentation improvements (William McKee) | |
57 | ||
58 | 1.06 Fri Aug 01 2003 | |
59 | - Syntax fix to work with Perl 5.005. (Fred Kleindenst) | |
60 | ||
61 | 1.05 Sun Jun 22 2003 | |
62 | - Improved examples in documentation (Mike Fragassi) | |
63 | No functionality changes. | |
64 | ||
65 | 1.04 Tue May 27 2003 | |
66 | - Fixed Build.PL syntax problem (Mike Fragassi) | |
67 | ||
68 | 1.03 Sun May 11 2003 | |
69 | - Added check_rm, which returns the results as a | |
70 | Data::FormValidator::Results object. | |
71 | ||
72 | - New Feature: Support for supplying defaults to the Data::FormValidator | |
73 | new() constructor by using the 'dfv_defaults' parameter. | |
74 | ||
75 | - New Feature: The profile can now be provided as the name of | |
76 | a CGI::Application method which returns the appropriate hash reference. | |
77 | ||
78 | - Also note that non-backwards compatible changes were made to D::FV's msgs | |
79 | interface. Although it doesn't affect the interface of this module, | |
80 | non-backwards compatible changes were made to accomodate it. | |
81 | ||
82 | 1.00 Sun Apr 20 | |
83 | - New Feature: added support for Data::FormValidator's new custom error | |
84 | message facility. This version is /not/ backwards compatible. | |
85 | Some has the functionality of this module is now available in | |
86 | Data::FormValidator. | |
87 | ||
88 | 0.02 Sat Apr 12 | |
89 | - Bug Fix: options passed through vrm param weren't being respected | |
90 | (Neil Mansilla) | |
91 | ||
92 | 0.01 Sat Mar 29 07:46:31 2003 | |
93 | - original version; created by h2xs 1.19 | |
94 |
0 | Build.PL | |
1 | Changes | |
2 | MANIFEST This list of files | |
3 | MANIFEST.SKIP | |
4 | META.yml | |
5 | Makefile.PL | |
6 | README | |
7 | lib/CGI/Application/Plugin/ValidateRM.pm | |
8 | t/01_basic.t | |
9 | t/01_display.html | |
10 | t/99_pod.t | |
11 | t/TestApp1.pm | |
12 | t/forward.t |
0 | # This is a list of regular expressions of files to skip when rebuilding | |
1 | # the MANIFEST file. See the documention for the ExtUtils::Manifest module for more detail. -mls | |
2 | \bCVS\b | |
3 | ^MANIFEST\.bak$ | |
4 | ^Makefile$ | |
5 | ~$ | |
6 | \.old$ | |
7 | \.bak$ | |
8 | ^blib/ | |
9 | ^pm_to_blib$ | |
10 | ^MakeMaker-\d | |
11 | .tar.gz$ | |
12 | ^notes | |
13 | ^releases | |
14 | \.tgz$ | |
15 | \.tmp$ | |
16 | \.swp$ | |
17 | \.swm$ | |
18 | \.swn$ | |
19 | \.swo$ | |
20 | \.patch$ | |
21 | \.orig$ | |
22 | \.diff$ | |
23 | _darcs | |
24 | _build | |
25 | ^Build$ | |
26 | ||
27 |
0 | --- | |
1 | name: CGI-Application-Plugin-ValidateRM | |
2 | version: 2.3 | |
3 | author: | |
4 | - 'Mark Stosberg <mark@summersault.com>' | |
5 | abstract: Help validate CGI::Application run modes using Data::FormValidator | |
6 | license: perl | |
7 | resources: | |
8 | license: http://dev.perl.org/licenses/ | |
9 | requires: | |
10 | CGI::Application: 3 | |
11 | Data::FormValidator: 3.7 | |
12 | HTML::FillInForm: 1.07 | |
13 | provides: | |
14 | CGI::Application::Plugin::ValidateRM: | |
15 | file: lib/CGI/Application/Plugin/ValidateRM.pm | |
16 | version: 2.3 | |
17 | generated_by: Module::Build version 0.2806 | |
18 | meta-spec: | |
19 | url: http://module-build.sourceforge.net/META-spec-v1.2.html | |
20 | version: 1.2 |
0 | # Note: this file was auto-generated by Module::Build::Compat version 0.03 | |
1 | use ExtUtils::MakeMaker; | |
2 | WriteMakefile | |
3 | ( | |
4 | 'PL_FILES' => {}, | |
5 | 'INSTALLDIRS' => 'site', | |
6 | 'NAME' => 'CGI::Application::Plugin::ValidateRM', | |
7 | 'EXE_FILES' => [], | |
8 | 'VERSION_FROM' => 'lib/CGI/Application/Plugin/ValidateRM.pm', | |
9 | 'PREREQ_PM' => { | |
10 | 'CGI::Application' => '3', | |
11 | 'HTML::FillInForm' => '1.07', | |
12 | 'Data::FormValidator' => '3.7' | |
13 | } | |
14 | ) | |
15 | ; |
0 | NAME | |
1 | CGI::Application::Plugin::ValidateRM - Help validate CGI::Application | |
2 | run modes using Data::FormValidator | |
3 | ||
4 | SYNOPSIS | |
5 | use CGI::Application::Plugin::ValidateRM; | |
6 | ||
7 | my $results = $self->check_rm('form_display','_form_profile') || return $self->check_rm_error_page; | |
8 | ||
9 | # Optionally, you can pass additional options to HTML::FillInForm->fill() | |
10 | my $results = $self->check_rm('form_display','_form_profile', { fill_password => 0 }) | |
11 | || return $self->check_rm_error_page; | |
12 | ||
13 | DESCRIPTION | |
14 | CGI::Application::Plugin::ValidateRM helps to validate web forms when | |
15 | using the CGI::Application framework and the Data::FormValidator module. | |
16 | ||
17 | check_rm() | |
18 | Validates a form displayed in a run mode with a "Data::FormValidator" | |
19 | profile, returning the results and possibly an a version of the form | |
20 | page with errors marked on the page. | |
21 | ||
22 | In scalar context, it returns simply the Data::FormValidator::Results | |
23 | object which conveniently evaluates to false in a boolean context if | |
24 | there were any missing or invalide fields. This is the recommended | |
25 | calling convention. | |
26 | ||
27 | In list context, it returns the results object followed by the error | |
28 | page, if any. This was the previous recommended syntax, and was used | |
29 | like this: | |
30 | ||
31 | my ($results,$err_page) = $self->check_rm('form_display','_form_profile'); | |
32 | return $err_page if $err_page; | |
33 | ||
34 | The inputs are as follows: | |
35 | ||
36 | Return run mode | |
37 | This run mode will be used to generate an error page, with the form | |
38 | re-filled (using HTML::FillInForm) and error messages in the form. | |
39 | This page will be returned as a second output parameter. | |
40 | ||
41 | The errors will be passed in as a hash reference, which can then be | |
42 | handed to a templating system for display. Following the above | |
43 | example, the form_display() routine might look like: | |
44 | ||
45 | sub form_display { | |
46 | my $self = shift; | |
47 | my $errs = shift; # <-- prepared for form reloading | |
48 | my $t = $self->load_tmpl('form_display.html'); | |
49 | $t->param($errs) if $errs; # <-- Also necessary. | |
50 | # ... | |
51 | ||
52 | } | |
53 | ||
54 | The fields should be prepared using Data::FormValidator's built-in | |
55 | support for returning error messages as a hash reference. See the | |
56 | documentation for "msgs" in the Data::FormValidator::Results | |
57 | documentation. | |
58 | ||
59 | Returning the errors with a prefix, such as "err_" is recommended. | |
60 | Using "any_errors" is also recommended to make it easy to display a | |
61 | general "we have some errors" message. | |
62 | ||
63 | HTML::Template users may want to pass "die_on_bad_params=>0" to the | |
64 | HTML::Template constructor to prevent the presence of the "err_" | |
65 | tokens from triggering an error when the errors are *not* being | |
66 | displayed. | |
67 | ||
68 | Data::FormValidator profile | |
69 | This can either be provided as a hash reference, or as the name of a | |
70 | CGI::Application method that will return such a hash reference. | |
71 | ||
72 | HTML::FillInForm options (optional) | |
73 | If desired, you can pass additional options to the HTML::FillInForm | |
74 | "fill" method through a hash reference. See an example above. | |
75 | ||
76 | Additionally, the value of the 'dfv_defaults' param from the calling | |
77 | object is optionally used to pass defaults to the "new()" constructor. | |
78 | ||
79 | $self->param('dfv_defaults') | |
80 | ||
81 | By setting this to a hash reference of defaults in your "cgiapp_init" | |
82 | routine in your own super-class, you could make it easy to share some | |
83 | default settings for Data::FormValidator across several forms. Of | |
84 | course, you could also set parameter through an instance script via the | |
85 | PARAMS key. | |
86 | ||
87 | Here's an example that I've used: | |
88 | ||
89 | sub cgiapp_init { | |
90 | my $self = shift; | |
91 | ||
92 | # Set some defaults for DFV unless they already exist. | |
93 | $self->param('dfv_defaults') || | |
94 | $self->param('dfv_defaults', { | |
95 | missing_optional_valid => 1, | |
96 | filters => 'trim', | |
97 | msgs => { | |
98 | any_errors => 'err__', | |
99 | prefix => 'err_', | |
100 | invalid => 'Invalid', | |
101 | missing => 'Missing', | |
102 | format => '<span class="dfv-errors">%s</span>', | |
103 | }, | |
104 | }); | |
105 | } | |
106 | ||
107 | Now all my applications that inherit from a super class with this | |
108 | "cgiapp_init()" routine and have these defaults, so I don't have to add | |
109 | them to every profile. | |
110 | ||
111 | CGI::Application::Plugin::Forward support | |
112 | Experimental support has been added for | |
113 | CGI::Application::Plugin::Forward, which keeps the current run mode up | |
114 | to date. This would be useful if you were automatically generating a | |
115 | template name based on the run mode name, and you wanted this to work | |
116 | with the form run mode used with ::ValidateRM. | |
117 | ||
118 | If we detect that ::Forward is loaded, we will set the current run mode | |
119 | name to be accurate while the error page is being generated, and then | |
120 | set it back to the previous value afterwards. There is a caveat: This | |
121 | currently only works when the run name name is the same as the | |
122 | subroutine name for the form page. If they differ, the current run mode | |
123 | name inside of the form page will be inaccurate. If this is a problem | |
124 | for you, get in touch to discuss a solution. | |
125 | ||
126 | check_rm_error_page() | |
127 | After check_rm() is called this accessor method can be used to retrieve | |
128 | the error page described in the check_rm() docs above. The method has an | |
129 | alias named "dfv_error_page()" if you find that more intuitive. | |
130 | ||
131 | dfv_results() | |
132 | $self->dfv_results; | |
133 | ||
134 | After "check_rm()" or "validate_rm()" has been called, the DFV results | |
135 | object can also be accessed through this method. I expect this to be | |
136 | most useful to other plugin authors. | |
137 | ||
138 | validate_rm() | |
139 | Works like "check_rm" above, but returns the old style $valid hash | |
140 | reference instead of the results object. It's no longer recommended, but | |
141 | still supported. | |
142 | ||
143 | EXAMPLE | |
144 | In a CGI::Application module: | |
145 | ||
146 | # This is the run mode that will be validated. Notice that it accepts | |
147 | # some errors to be passed in, and on to the template system. | |
148 | sub form_display { | |
149 | my $self = shift; | |
150 | my $errs = shift; | |
151 | ||
152 | my $t = $self->load_tmpl('page.html'); | |
153 | ||
154 | $t->param($errs) if $errs; | |
155 | return $t->output; | |
156 | } | |
157 | ||
158 | sub form_process { | |
159 | my $self = shift; | |
160 | ||
161 | use CGI::Application::Plugin::ValidateRM (qw/check_rm/); | |
162 | my ($results, $err_page) = $self->check_rm('form_display','_form_profile'); | |
163 | return $err_page if $err_page; | |
164 | ||
165 | #.. do something with DFV $results object now | |
166 | ||
167 | my $t = $self->load_tmpl('success.html'); | |
168 | return $t->output; | |
169 | ||
170 | } | |
171 | ||
172 | sub _form_profile { | |
173 | return { | |
174 | required => 'email', | |
175 | msgs => { | |
176 | any_errors => 'some_errors', | |
177 | prefix => 'err_', | |
178 | }, | |
179 | }; | |
180 | } | |
181 | ||
182 | In page.html: | |
183 | ||
184 | <!-- tmpl_if some_errors --> | |
185 | <h3>Some fields below are missing or invalid</h3> | |
186 | <!-- /tmpl_if --> | |
187 | <form> | |
188 | <input type="text" name="email"> <!-- tmpl_var err_email --> | |
189 | </form> | |
190 | ||
191 | SEE ALSO | |
192 | CGI::Application, Data::FormValidator, HTML::FillInForm, perl(1) | |
193 | ||
194 | AUTHOR | |
195 | Mark Stosberg <mark@summersault.com> | |
196 | ||
197 | MAILING LIST | |
198 | If you have any questions, comments, bug reports or feature suggestions, | |
199 | post them to the support mailing list! This the Data::FormValidator | |
200 | list. To join the mailing list, visit | |
201 | <http://lists.sourceforge.net/lists/listinfo/cascade-dataform> | |
202 | ||
203 | LICENSE | |
204 | Copyright (C) 2003-2005 Mark Stosberg <mark@summersault.com> | |
205 | ||
206 | This module is free software; you can redistribute it and/or modify it | |
207 | under the terms of either: | |
208 | ||
209 | a) the GNU General Public License as published by the Free Software | |
210 | Foundation; either version 1, or (at your option) any later version, | |
211 | ||
212 | or | |
213 | ||
214 | b) the "Artistic License" | |
215 | ||
216 | This program is distributed in the hope that it will be useful, but | |
217 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
218 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU | |
219 | General Public License or the Artistic License for more details. | |
220 | ||
221 | For a copy of the GNU General Public License along with this program; if | |
222 | not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite | |
223 | 330, Boston, MA 02111-1307 USA | |
224 |
0 | package CGI::Application::Plugin::ValidateRM; | |
1 | use HTML::FillInForm; | |
2 | use Data::FormValidator; | |
3 | use strict; | |
4 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | |
5 | ||
6 | require Exporter; | |
7 | ||
8 | @ISA = qw(Exporter AutoLoader); | |
9 | # Items to export into callers namespace by default. Note: do not export | |
10 | # names by default without a very good reason. Use EXPORT_OK instead. | |
11 | # Do not simply export all your public functions/methods/constants. | |
12 | @EXPORT = qw( | |
13 | &dfv_results | |
14 | &dfv_error_page | |
15 | &check_rm_error_page | |
16 | &check_rm | |
17 | &validate_rm | |
18 | ); | |
19 | ||
20 | @EXPORT_OK = qw( | |
21 | ); | |
22 | ||
23 | $VERSION = '2.3'; | |
24 | ||
25 | sub check_rm { | |
26 | my $self = shift; | |
27 | my $return_rm = shift || die 'missing required return run mode'; | |
28 | my $profile_in = shift || die 'missing required profile'; | |
29 | my $fif_params = shift || {}; | |
30 | ||
31 | # If the profile is not a hash reference, | |
32 | # assume it's a CGI::App method | |
33 | my $profile; | |
34 | if (ref $profile_in eq 'HASH') { | |
35 | $profile = $profile_in; | |
36 | } | |
37 | else { | |
38 | if ($self->can($profile_in)) { | |
39 | $profile = $self->$profile_in(); | |
40 | } | |
41 | else { | |
42 | $profile = eval { $self->$profile_in() }; | |
43 | die "Error running profile method '$profile_in': $@" if $@; | |
44 | } | |
45 | ||
46 | } | |
47 | ||
48 | require Data::FormValidator; | |
49 | my $dfv = Data::FormValidator->new({}, $self->param('dfv_defaults') ); | |
50 | my $r =$dfv->check($self->query,$profile); | |
51 | $self->{'__DFV_RESULT'} = $r; | |
52 | ||
53 | # Pass the params through the object so the user | |
54 | # can just call dfv_error_page() later | |
55 | $self->{'__DFV_RETURN_RM'} = $return_rm; | |
56 | $self->{'__DFV_FIF_PARAMS'} = $fif_params; | |
57 | ||
58 | if (wantarray) { | |
59 | # We have to call the function non-traditionally to achieve mix-in happiness. | |
60 | return $r, dfv_error_page($self); | |
61 | } | |
62 | else { | |
63 | return $r; | |
64 | } | |
65 | } | |
66 | ||
67 | sub dfv_results { | |
68 | my $self = shift; | |
69 | die "must call check_rm() or validate_rm() first." unless defined $self->{'__DFV_RESULT'}; | |
70 | return $self->{'__DFV_RESULT'}; | |
71 | } | |
72 | ||
73 | sub validate_rm { | |
74 | my $self = shift; | |
75 | my ($r,$err_page) = $self->check_rm(@_); | |
76 | return (scalar $r->valid,$err_page); | |
77 | } | |
78 | ||
79 | sub dfv_error_page { | |
80 | my $self = shift; | |
81 | my $r = $self->{'__DFV_RESULT'}; | |
82 | my $return_rm = $self->{'__DFV_RETURN_RM'}; | |
83 | my $fif_params = $self->{'__DFV_FIF_PARAMS'}; | |
84 | ||
85 | my $err_page = undef; | |
86 | if ($r->has_missing or $r->has_invalid) { | |
87 | # If ::Forward has been loaded, act like forward() | |
88 | my $before_rm = $self->{__CURRENT_RUNMODE}; | |
89 | $self->{__CURRENT_RUNMODE} = $return_rm if ($INC{'CGI/Application/Plugin/Forward.pm'}); | |
90 | ||
91 | my $return_page = $self->$return_rm($r->msgs); | |
92 | ||
93 | $self->{__CURRENT_RUNMODE} = $before_rm; | |
94 | ||
95 | my $return_pageref = (ref($return_page) eq 'SCALAR') | |
96 | ? $return_page : \$return_page; | |
97 | require HTML::FillInForm; | |
98 | my $fif = new HTML::FillInForm; | |
99 | $err_page = $fif->fill( | |
100 | scalarref => $return_pageref, | |
101 | fobject => $self->query, | |
102 | %$fif_params, | |
103 | ); | |
104 | } | |
105 | return $err_page; | |
106 | } | |
107 | ||
108 | *check_rm_error_page = \&dfv_error_page; | |
109 | my $avoid_warning = \&check_rm_error_page; | |
110 | ||
111 | 1; | |
112 | __END__ | |
113 | ||
114 | =head1 NAME | |
115 | ||
116 | CGI::Application::Plugin::ValidateRM - Help validate CGI::Application run modes using Data::FormValidator | |
117 | ||
118 | =head1 SYNOPSIS | |
119 | ||
120 | use CGI::Application::Plugin::ValidateRM; | |
121 | ||
122 | my $results = $self->check_rm('form_display','_form_profile') || return $self->check_rm_error_page; | |
123 | ||
124 | ||
125 | # Optionally, you can pass additional options to HTML::FillInForm->fill() | |
126 | my $results = $self->check_rm('form_display','_form_profile', { fill_password => 0 }) | |
127 | || return $self->check_rm_error_page; | |
128 | ||
129 | =head1 DESCRIPTION | |
130 | ||
131 | CGI::Application::Plugin::ValidateRM helps to validate web forms when using the | |
132 | CGI::Application framework and the Data::FormValidator module. | |
133 | ||
134 | =head2 check_rm() | |
135 | ||
136 | Validates a form displayed in a run mode with a C<Data::FormValidator> profile, returning | |
137 | the results and possibly an a version of the form page with errors marked on the page. | |
138 | ||
139 | In scalar context, it returns simply the Data::FormValidator::Results object | |
140 | which conveniently evaluates to false in a boolean context if there were any missing | |
141 | or invalide fields. This is the recommended calling convention. | |
142 | ||
143 | In list context, it returns the results object followed by the error page, if any. | |
144 | This was the previous recommended syntax, and was used like this: | |
145 | ||
146 | my ($results,$err_page) = $self->check_rm('form_display','_form_profile'); | |
147 | return $err_page if $err_page; | |
148 | ||
149 | The inputs are as follows: | |
150 | ||
151 | =over | |
152 | ||
153 | =item Return run mode | |
154 | ||
155 | This run mode will be used to generate an error page, with the form re-filled | |
156 | (using L<HTML::FillInForm>) and error messages in the form. This page will be | |
157 | returned as a second output parameter. | |
158 | ||
159 | The errors will be passed in as a hash reference, which can then be handed to a | |
160 | templating system for display. Following the above example, the form_display() routine might look like: | |
161 | ||
162 | sub form_display { | |
163 | my $self = shift; | |
164 | my $errs = shift; # <-- prepared for form reloading | |
165 | my $t = $self->load_tmpl('form_display.html'); | |
166 | $t->param($errs) if $errs; # <-- Also necessary. | |
167 | # ... | |
168 | ||
169 | } | |
170 | ||
171 | The fields should be prepared using Data::FormValidator's | |
172 | built-in support for returning error messages as a hash reference. | |
173 | See the documentation for C<msgs> in the L<Data::FormValidator::Results> | |
174 | documentation. | |
175 | ||
176 | Returning the errors with a prefix, such as "err_" is recommended. Using | |
177 | C<any_errors> is also recommended to make it easy to display a general "we have | |
178 | some errors" message. | |
179 | ||
180 | HTML::Template users may want to pass C<die_on_bad_params=E<gt>0> to the | |
181 | HTML::Template constructor to prevent the presence of the "err_" tokens from | |
182 | triggering an error when the errors are I<not> being displayed. | |
183 | ||
184 | =item Data::FormValidator profile | |
185 | ||
186 | This can either be provided as a hash reference, or as the name | |
187 | of a CGI::Application method that will return such a hash reference. | |
188 | ||
189 | =item HTML::FillInForm options (optional) | |
190 | ||
191 | If desired, you can pass additional options to the HTML::FillInForm C<fill> | |
192 | method through a hash reference. See an example above. | |
193 | ||
194 | =back | |
195 | ||
196 | Additionally, the value of the 'dfv_defaults' param from the calling | |
197 | object is optionally used to pass defaults to the C<new()> constructor. | |
198 | ||
199 | $self->param('dfv_defaults') | |
200 | ||
201 | By setting this to a hash reference of defaults in your C<cgiapp_init> routine | |
202 | in your own super-class, you could make it easy to share some default settings for | |
203 | Data::FormValidator across several forms. Of course, you could also set parameter | |
204 | through an instance script via the PARAMS key. | |
205 | ||
206 | Here's an example that I've used: | |
207 | ||
208 | sub cgiapp_init { | |
209 | my $self = shift; | |
210 | ||
211 | # Set some defaults for DFV unless they already exist. | |
212 | $self->param('dfv_defaults') || | |
213 | $self->param('dfv_defaults', { | |
214 | missing_optional_valid => 1, | |
215 | filters => 'trim', | |
216 | msgs => { | |
217 | any_errors => 'err__', | |
218 | prefix => 'err_', | |
219 | invalid => 'Invalid', | |
220 | missing => 'Missing', | |
221 | format => '<span class="dfv-errors">%s</span>', | |
222 | }, | |
223 | }); | |
224 | } | |
225 | ||
226 | Now all my applications that inherit from a super class with this | |
227 | C<cgiapp_init()> routine and have these defaults, so I don't have | |
228 | to add them to every profile. | |
229 | ||
230 | =head2 CGI::Application::Plugin::Forward support | |
231 | ||
232 | Experimental support has been added for CGI::Application::Plugin::Forward, | |
233 | which keeps the current run mode up to date. This would be useful if you | |
234 | were automatically generating a template name based on the run mode name, | |
235 | and you wanted this to work with the form run mode used with ::ValidateRM. | |
236 | ||
237 | If we detect that ::Forward is loaded, we will set the current run mode name to | |
238 | be accurate while the error page is being generated, and then set it back to | |
239 | the previous value afterwards. There is a caveat: This currently only works | |
240 | when the run name name is the same as the subroutine name for the form page. | |
241 | If they differ, the current run mode name inside of the form page will be | |
242 | inaccurate. If this is a problem for you, get in touch to discuss a solution. | |
243 | ||
244 | =head2 check_rm_error_page() | |
245 | ||
246 | After check_rm() is called this accessor method can be used to retrieve the | |
247 | error page described in the check_rm() docs above. The method has an alias | |
248 | named C<dfv_error_page()> if you find that more intuitive. | |
249 | ||
250 | =head2 dfv_results() | |
251 | ||
252 | $self->dfv_results; | |
253 | ||
254 | After C<check_rm()> or C<validate_rm()> has been called, the DFV results object | |
255 | can also be accessed through this method. I expect this to be most useful to | |
256 | other plugin authors. | |
257 | ||
258 | =head2 validate_rm() | |
259 | ||
260 | Works like C<check_rm> above, but returns the old style C<$valid> hash | |
261 | reference instead of the results object. It's no longer recommended, but still | |
262 | supported. | |
263 | ||
264 | =head1 EXAMPLE | |
265 | ||
266 | In a CGI::Application module: | |
267 | ||
268 | # This is the run mode that will be validated. Notice that it accepts | |
269 | # some errors to be passed in, and on to the template system. | |
270 | sub form_display { | |
271 | my $self = shift; | |
272 | my $errs = shift; | |
273 | ||
274 | my $t = $self->load_tmpl('page.html'); | |
275 | ||
276 | $t->param($errs) if $errs; | |
277 | return $t->output; | |
278 | } | |
279 | ||
280 | sub form_process { | |
281 | my $self = shift; | |
282 | ||
283 | use CGI::Application::Plugin::ValidateRM (qw/check_rm/); | |
284 | my ($results, $err_page) = $self->check_rm('form_display','_form_profile'); | |
285 | return $err_page if $err_page; | |
286 | ||
287 | #.. do something with DFV $results object now | |
288 | ||
289 | my $t = $self->load_tmpl('success.html'); | |
290 | return $t->output; | |
291 | ||
292 | } | |
293 | ||
294 | sub _form_profile { | |
295 | return { | |
296 | required => 'email', | |
297 | msgs => { | |
298 | any_errors => 'some_errors', | |
299 | prefix => 'err_', | |
300 | }, | |
301 | }; | |
302 | } | |
303 | ||
304 | In page.html: | |
305 | ||
306 | <!-- tmpl_if some_errors --> | |
307 | <h3>Some fields below are missing or invalid</h3> | |
308 | <!-- /tmpl_if --> | |
309 | <form> | |
310 | <input type="text" name="email"> <!-- tmpl_var err_email --> | |
311 | </form> | |
312 | ||
313 | ||
314 | =head1 SEE ALSO | |
315 | ||
316 | L<CGI::Application>, L<Data::FormValidator>, L<HTML::FillInForm>, perl(1) | |
317 | ||
318 | =head1 AUTHOR | |
319 | ||
320 | Mark Stosberg <mark@summersault.com> | |
321 | ||
322 | =head1 MAILING LIST | |
323 | ||
324 | If you have any questions, comments, bug reports or feature suggestions, | |
325 | post them to the support mailing list! This the Data::FormValidator list. | |
326 | To join the mailing list, visit L<http://lists.sourceforge.net/lists/listinfo/cascade-dataform> | |
327 | ||
328 | =head1 LICENSE | |
329 | ||
330 | Copyright (C) 2003-2005 Mark Stosberg <mark@summersault.com> | |
331 | ||
332 | This module is free software; you can redistribute it and/or modify it | |
333 | under the terms of either: | |
334 | ||
335 | a) the GNU General Public License as published by the Free Software | |
336 | Foundation; either version 1, or (at your option) any later version, | |
337 | ||
338 | or | |
339 | ||
340 | b) the "Artistic License" | |
341 | ||
342 | This program is distributed in the hope that it will be useful, | |
343 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
344 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either | |
345 | the GNU General Public License or the Artistic License for more details. | |
346 | ||
347 | For a copy of the GNU General Public License along with this program; if not, | |
348 | write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
349 | Boston, MA 02111-1307 USA | |
350 | ||
351 | ||
352 | =cut | |
353 |
0 | use Test::More tests => 14; | |
1 | BEGIN { use_ok('CGI::Application::Plugin::ValidateRM') }; | |
2 | ||
3 | use lib './t'; | |
4 | use strict; | |
5 | ||
6 | $ENV{CGI_APP_RETURN_ONLY} = 1; | |
7 | ||
8 | use CGI; | |
9 | use TestApp1; | |
10 | my $t1_obj = TestApp1->new(QUERY=>CGI->new("email=broken;rm=form_process")); | |
11 | my $t1_output = $t1_obj->run(); | |
12 | ||
13 | like($t1_output, qr/Some fields below/, 'err__'); | |
14 | ||
15 | like($t1_output, qr/name="email".*Invalid/, 'basic invalid'); | |
16 | ||
17 | like($t1_output,qr/name="phone".*Missing/, 'basic missing'); | |
18 | ||
19 | my $t2_obj = TestApp1->new(QUERY=>CGI->new("email=broken;rm=form_process_with_ref")); | |
20 | my $t2_output = $t2_obj->run(); | |
21 | ||
22 | like($t2_output, qr/Some fields below/, 'err__'); | |
23 | ||
24 | like($t2_output, qr/name="email".*Invalid/, 'basic invalid'); | |
25 | ||
26 | like($t2_output,qr/name="phone".*Missing/, 'basic missing'); | |
27 | ||
28 | my $t3_obj = TestApp1->new(QUERY=>CGI->new("email=broken;passwd=anything;rm=form_process_with_fif_opts")); | |
29 | my $t3_output = $t3_obj->run(); | |
30 | ||
31 | like($t3_output,qr/name="phone".*Missing/, 't3 basic missing'); | |
32 | unlike($t3_output, qr/anything/, 'passing options to HTML::FillInForm works'); | |
33 | ||
34 | ||
35 | { | |
36 | use UNIVERSAL (qw/can/); | |
37 | ok( $t3_obj->can('dfv_results'), "has dfv_results method" ); | |
38 | ok( $t3_obj->can('dfv_error_page'), "has dfv_error_page method" ); | |
39 | ok( $t3_obj->can('check_rm_error_page'), "has check_rm_error_page method" ); | |
40 | ok( defined $t3_obj->dfv_results->invalid('email'), "content of DFV method is as expected" ); | |
41 | ||
42 | } | |
43 | ||
44 | { | |
45 | my $valid = $t3_obj->check_rm('form_display', '_form_profile', ); | |
46 | ok($valid->can('invalid'), "calling check_rm in scalar context returns just the DFV obj."); | |
47 | ||
48 | ||
49 | } |
0 | <!-- tmpl_if err__ --><h3>Some fields below are missing or invalid</h3><!-- /tmpl_if --> | |
1 | <form> | |
2 | <input type="text" name="email"> <!-- tmpl_var err_email --><BR> | |
3 | <input type="text" name="phone"> <!-- tmpl_var err_phone --><BR> | |
4 | ||
5 | <input type="password" name="passwd"> <!-- tmpl_var err_passwd --><BR> | |
6 | </form> |
0 | use Test::More; | |
1 | ||
2 | use File::Spec; | |
3 | use File::Find; | |
4 | use strict; | |
5 | ||
6 | eval { | |
7 | require Test::Pod; | |
8 | Test::Pod->import; | |
9 | }; | |
10 | ||
11 | my @files; | |
12 | ||
13 | if ($@) { | |
14 | plan skip_all => "Test::Pod required for testing POD"; | |
15 | } | |
16 | else { | |
17 | my $blib = File::Spec->catfile(qw(blib lib)); | |
18 | find(\&wanted, $blib); | |
19 | plan tests => scalar @files; | |
20 | foreach my $file (@files) { | |
21 | pod_file_ok($file); | |
22 | } | |
23 | } | |
24 | ||
25 | sub wanted { | |
26 | push @files, $File::Find::name if /\.p(l|m|od)$/; | |
27 | } |
0 | package TestApp1; | |
1 | ||
2 | use strict; | |
3 | ||
4 | use CGI::Application; | |
5 | @TestApp1::ISA = qw(CGI::Application); | |
6 | ||
7 | sub setup { | |
8 | my $self = shift; | |
9 | ||
10 | $self->run_modes([qw/ | |
11 | form_display | |
12 | form_process | |
13 | ||
14 | form_display_with_ref | |
15 | form_process_with_ref | |
16 | ||
17 | form_process_with_fif_opts | |
18 | /]); | |
19 | } | |
20 | ||
21 | # This is the run mode that will be validated. Notice that it accepts | |
22 | # some errors to be passed in, and on to the template system. | |
23 | sub form_display { | |
24 | my $self = shift; | |
25 | my $errs = shift; | |
26 | ||
27 | my $t = $self->load_tmpl('t/01_display.html', die_on_bad_params=>0); | |
28 | ||
29 | $t->param($errs) if $errs; | |
30 | return $t->output; | |
31 | } | |
32 | ||
33 | # This is another run mode that will be validated. Similar, but this one | |
34 | # returns a reference to the output rather than returning the output itself. | |
35 | sub form_display_with_ref { | |
36 | my $self = shift; | |
37 | my $errs = shift; | |
38 | ||
39 | my $t = $self->load_tmpl('t/01_display.html', | |
40 | die_on_bad_params=>0, | |
41 | ); | |
42 | ||
43 | $t->param($errs) if $errs; | |
44 | return \$t->output; | |
45 | } | |
46 | ||
47 | sub form_process { | |
48 | my $self = shift; | |
49 | ||
50 | use CGI::Application::Plugin::ValidateRM; | |
51 | ||
52 | my ($results, $err_page) = $self->validate_rm('form_display', '_form_profile' ); | |
53 | return $err_page if $err_page; | |
54 | ||
55 | return 'success'; | |
56 | } | |
57 | ||
58 | sub form_process_with_ref { | |
59 | my $self = shift; | |
60 | ||
61 | my ($results, $err_page) = $self->validate_rm('form_display_with_ref', '_form_profile' ); | |
62 | return $err_page if $err_page; | |
63 | ||
64 | return 'success'; | |
65 | } | |
66 | ||
67 | sub form_process_with_fif_opts { | |
68 | my $self = shift; | |
69 | ||
70 | my $results = $self->check_rm('form_display', '_form_profile', { fill_password => 0 } ) | |
71 | || return $self->dfv_error_page; | |
72 | ||
73 | return 'success'; | |
74 | } | |
75 | ||
76 | ||
77 | ||
78 | sub _form_profile { | |
79 | return { | |
80 | required => [qw/email phone/], | |
81 | optional => [qw/passwd/], | |
82 | constraints => { | |
83 | email => 'email', | |
84 | }, | |
85 | msgs => { | |
86 | any_errors => 'err__', | |
87 | prefix => 'err_', | |
88 | }, | |
89 | }; | |
90 | } | |
91 | ||
92 |
0 | use Test::More 'no_plan'; | |
1 | use strict; | |
2 | ||
3 | $ENV{CGI_APP_RETURN_ONLY} = 1; | |
4 | ||
5 | { | |
6 | package MyForward::Test; | |
7 | use base 'CGI::Application'; | |
8 | use CGI::Application::Plugin::ValidateRM; | |
9 | use Test::More; | |
10 | ||
11 | sub setup { | |
12 | my $self = shift; | |
13 | $self->start_mode('legacy_process'); | |
14 | $self->run_modes([qw/legacy_process legacy_form/]); } | |
15 | ||
16 | sub legacy_form { | |
17 | my $self = shift; | |
18 | ||
19 | ok( | |
20 | (not exists $INC{'CGI/Application/Plugin/Forward.pm'}) | |
21 | , "reality check: Forward plugin is not loaded yet."); | |
22 | ||
23 | is($self->get_current_runmode | |
24 | , 'legacy_process' | |
25 | , "if ::Forward is not loaded, current_rm is not updated"); | |
26 | $self->header_type('none'); | |
27 | return "legacy form output"; | |
28 | } | |
29 | ||
30 | sub legacy_process { | |
31 | my $self = shift; | |
32 | my ($results, $err_page) = $self->check_rm('legacy_form', { required => 'fail' }); | |
33 | return $err_page if $err_page; | |
34 | return 'legacy process completed'; | |
35 | } | |
36 | ||
37 | my $app = MyForward::Test->new(); | |
38 | my $out = $app->run(); | |
39 | is($out, 'legacy form output', "form is returned"); | |
40 | } | |
41 | ||
42 | SKIP: { | |
43 | package MyForward::TestForward; | |
44 | use base 'CGI::Application'; | |
45 | use CGI::Application::Plugin::ValidateRM; | |
46 | use Test::More; | |
47 | ||
48 | eval { require CGI::Application::Plugin::Forward; }; | |
49 | if ($@) { | |
50 | skip "CGI::Application::Plugin::Forward required", 2; | |
51 | } | |
52 | ||
53 | sub setup { | |
54 | my $self = shift; | |
55 | $self->start_mode('forward_process'); | |
56 | $self->run_modes([qw/forward_process forward_form/]); | |
57 | } | |
58 | ||
59 | sub forward_form { | |
60 | my $self = shift; | |
61 | is($self->get_current_runmode, 'forward_form', | |
62 | "if ::Forward is loaded, current_rm is updated"); | |
63 | $self->header_type('none'); | |
64 | return "forward form output"; | |
65 | } | |
66 | ||
67 | sub forward_process { | |
68 | my $self = shift; | |
69 | my ($results, $err_page) = $self->check_rm('forward_form', { required => 'fail' }); | |
70 | return $err_page if $err_page; | |
71 | } | |
72 | ||
73 | my $app = MyForward::TestForward->new; | |
74 | my $out = $app->run; | |
75 | is($out, 'forward form output', "form is returned"); | |
76 | } |