| 1 |
NAME |
| 2 |
CGI::Application::Plugin::ValidateRM - Help validate CGI::Application |
| 3 |
run modes using Data::FormValidator |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use CGI::Application::Plugin::ValidateRM; |
| 7 |
|
| 8 |
my $results = $self->check_rm('form_display','_form_profile') || return $self->check_rm_error_page; |
| 9 |
|
| 10 |
|
| 11 |
# Optionally, you can pass additional options to HTML::FillInForm->fill() |
| 12 |
my $results = $self->check_rm('form_display','_form_profile', { fill_password => 0 }) |
| 13 |
|| return $self->check_rm_error_page; |
| 14 |
|
| 15 |
DESCRIPTION |
| 16 |
CGI::Application::Plugin::ValidateRM helps to validate web forms when |
| 17 |
using the CGI::Application framework and the Data::FormValidator module. |
| 18 |
|
| 19 |
check_rm() |
| 20 |
Validates a form displayed in a run mode with a "Data::FormValidator" |
| 21 |
profile, returning the results and possibly an a version of the form |
| 22 |
page with errors marked on the page. |
| 23 |
|
| 24 |
In scalar context, it returns simply the Data::FormValidator::Results |
| 25 |
object which conveniently evaluates to false in a boolean context if |
| 26 |
there were any missing or invalide fields. This is the recommended |
| 27 |
calling convention. |
| 28 |
|
| 29 |
In list context, it returns the results object followed by the error |
| 30 |
page, if any. This was the previous recommended syntax, and was used |
| 31 |
like this: |
| 32 |
|
| 33 |
my ($results,$err_page) = $self->check_rm('form_display','_form_profile'); |
| 34 |
return $err_page if $err_page; |
| 35 |
|
| 36 |
The inputs are as follows: |
| 37 |
|
| 38 |
Return run mode |
| 39 |
This run mode will be used to generate an error page, with the form |
| 40 |
re-filled (using HTML::FillInForm) and error messages in the form. |
| 41 |
This page will be returned as a second output parameter. |
| 42 |
|
| 43 |
The errors will be passed in as a hash reference, which can then be |
| 44 |
handed to a templating system for display. Following the above |
| 45 |
example, the form_display() routine might look like: |
| 46 |
|
| 47 |
sub form_display { |
| 48 |
my $self = shift; |
| 49 |
my $errs = shift; # <-- prepared for form reloading |
| 50 |
my $t = $self->load_tmpl('form_display.html'); |
| 51 |
$t->param($errs) if $errs; # <-- Also necessary. |
| 52 |
# ... |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
The fields should be prepared using Data::FormValidator's built-in |
| 57 |
support for returning error messages as a hash reference. See the |
| 58 |
documentation for "msgs" in the Data::FormValidator::Results |
| 59 |
documentation. |
| 60 |
|
| 61 |
Returning the errors with a prefix, such as "err_" is recommended. |
| 62 |
Using "any_errors" is also recommended to make it easy to display a |
| 63 |
general "we have some errors" message. |
| 64 |
|
| 65 |
HTML::Template users may want to pass "die_on_bad_params=>0" to the |
| 66 |
HTML::Template constructor to prevent the presence of the "err_" |
| 67 |
tokens from triggering an error when the errors are *not* being |
| 68 |
displayed. |
| 69 |
|
| 70 |
Data::FormValidator profile |
| 71 |
This can either be provided as a hash reference, or as the name of a |
| 72 |
CGI::Application method that will return such a hash reference. |
| 73 |
|
| 74 |
HTML::FillInForm options (optional) |
| 75 |
If desired, you can pass additional options to the HTML::FillInForm |
| 76 |
fill() method through a hash reference. See an example above. |
| 77 |
|
| 78 |
Additional Options |
| 79 |
To control things even more, you can set parameters in your |
| 80 |
CGI::Application object itself. |
| 81 |
|
| 82 |
dfv_defaults |
| 83 |
The value of the 'dfv_defaults' param is optionally used to pass |
| 84 |
defaults to the Data::FormValidator new() constructor. |
| 85 |
|
| 86 |
$self->param(dfv_defaults => { filters => ['trim'] }) |
| 87 |
|
| 88 |
By setting this to a hash reference of defaults in your |
| 89 |
"cgiapp_init" routine in your own super-class, you could make it |
| 90 |
easy to share some default settings for Data::FormValidator across |
| 91 |
several forms. Of course, you could also set parameter through an |
| 92 |
instance script via the PARAMS key. |
| 93 |
|
| 94 |
Here's an example that I've used: |
| 95 |
|
| 96 |
sub cgiapp_init { |
| 97 |
my $self = shift; |
| 98 |
|
| 99 |
# Set some defaults for DFV unless they already exist. |
| 100 |
$self->param('dfv_defaults') || |
| 101 |
$self->param('dfv_defaults', { |
| 102 |
missing_optional_valid => 1, |
| 103 |
filters => 'trim', |
| 104 |
msgs => { |
| 105 |
any_errors => 'err__', |
| 106 |
prefix => 'err_', |
| 107 |
invalid => 'Invalid', |
| 108 |
missing => 'Missing', |
| 109 |
format => '<span class="dfv-errors">%s</span>', |
| 110 |
}, |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
Now all my applications that inherit from a super class with this |
| 115 |
"cgiapp_init()" routine and have these defaults, so I don't have to |
| 116 |
add them to every profile. |
| 117 |
|
| 118 |
dfv_fif_class |
| 119 |
By default this plugin uses HTML::FillInForm to fill in the forms on |
| 120 |
the error pages with the given values. This option let's you change |
| 121 |
that so it uses an HTML::FillInForm compatible class (like a |
| 122 |
subclass) to do the same work. |
| 123 |
|
| 124 |
$self->param(dfv_fif_class => 'HTML::FillInForm::SuperDuper'); |
| 125 |
|
| 126 |
dfv_fif_defaults |
| 127 |
The value of the 'dfv_fif_defaults' param is optionally used to pass |
| 128 |
defaults to the HTML::FillInForm "fill()" method. |
| 129 |
|
| 130 |
$self->param(dfv_fif_defaults => {ignore_fields => ['rm']}) |
| 131 |
|
| 132 |
By setting this to a hash reference of defaults in your |
| 133 |
"cgiapp_init" routine in your own super-class, you could make it |
| 134 |
easy to share some default settings for HTML::FillInForm across |
| 135 |
several forms. Of course, you could also set parameter through an |
| 136 |
instance script via the PARAMS key. |
| 137 |
|
| 138 |
CGI::Application::Plugin::Forward support |
| 139 |
Experimental support has been added for |
| 140 |
CGI::Application::Plugin::Forward, which keeps the current run mode up |
| 141 |
to date. This would be useful if you were automatically generating a |
| 142 |
template name based on the run mode name, and you wanted this to work |
| 143 |
with the form run mode used with ::ValidateRM. |
| 144 |
|
| 145 |
If we detect that ::Forward is loaded, we will set the current run mode |
| 146 |
name to be accurate while the error page is being generated, and then |
| 147 |
set it back to the previous value afterwards. There is a caveat: This |
| 148 |
currently only works when the run name name is the same as the |
| 149 |
subroutine name for the form page. If they differ, the current run mode |
| 150 |
name inside of the form page will be inaccurate. If this is a problem |
| 151 |
for you, get in touch to discuss a solution. |
| 152 |
|
| 153 |
check_rm_error_page() |
| 154 |
After check_rm() is called this accessor method can be used to retrieve |
| 155 |
the error page described in the check_rm() docs above. The method has an |
| 156 |
alias named "dfv_error_page()" if you find that more intuitive. |
| 157 |
|
| 158 |
dfv_results() |
| 159 |
$self->dfv_results; |
| 160 |
|
| 161 |
After "check_rm()" or "validate_rm()" has been called, the DFV results |
| 162 |
object can also be accessed through this method. I expect this to be |
| 163 |
most useful to other plugin authors. |
| 164 |
|
| 165 |
validate_rm() |
| 166 |
Works like "check_rm" above, but returns the old style $valid hash |
| 167 |
reference instead of the results object. It's no longer recommended, but |
| 168 |
still supported. |
| 169 |
|
| 170 |
EXAMPLE |
| 171 |
In a CGI::Application module: |
| 172 |
|
| 173 |
# This is the run mode that will be validated. Notice that it accepts |
| 174 |
# some errors to be passed in, and on to the template system. |
| 175 |
sub form_display { |
| 176 |
my $self = shift; |
| 177 |
my $errs = shift; |
| 178 |
|
| 179 |
my $t = $self->load_tmpl('page.html'); |
| 180 |
|
| 181 |
$t->param($errs) if $errs; |
| 182 |
return $t->output; |
| 183 |
} |
| 184 |
|
| 185 |
sub form_process { |
| 186 |
my $self = shift; |
| 187 |
|
| 188 |
use CGI::Application::Plugin::ValidateRM (qw/check_rm/); |
| 189 |
my ($results, $err_page) = $self->check_rm('form_display','_form_profile'); |
| 190 |
return $err_page if $err_page; |
| 191 |
|
| 192 |
#.. do something with DFV $results object now |
| 193 |
|
| 194 |
my $t = $self->load_tmpl('success.html'); |
| 195 |
return $t->output; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
sub _form_profile { |
| 200 |
return { |
| 201 |
required => 'email', |
| 202 |
msgs => { |
| 203 |
any_errors => 'some_errors', |
| 204 |
prefix => 'err_', |
| 205 |
}, |
| 206 |
}; |
| 207 |
} |
| 208 |
|
| 209 |
In page.html: |
| 210 |
|
| 211 |
<!-- tmpl_if some_errors --> |
| 212 |
<h3>Some fields below are missing or invalid</h3> |
| 213 |
<!-- /tmpl_if --> |
| 214 |
<form> |
| 215 |
<input type="text" name="email"> <!-- tmpl_var err_email --> |
| 216 |
</form> |
| 217 |
|
| 218 |
SEE ALSO |
| 219 |
CGI::Application, Data::FormValidator, HTML::FillInForm, perl(1) |
| 220 |
|
| 221 |
AUTHOR |
| 222 |
Mark Stosberg <[email protected]> |
| 223 |
|
| 224 |
MAILING LIST |
| 225 |
If you have any questions, comments, bug reports or feature suggestions, |
| 226 |
post them to the support mailing list! This the Data::FormValidator |
| 227 |
list. To join the mailing list, visit |
| 228 |
<http://lists.sourceforge.net/lists/listinfo/cascade-dataform> |
| 229 |
|
| 230 |
LICENSE |
| 231 |
Copyright (C) 2003-2005 Mark Stosberg <[email protected]> |
| 232 |
|
| 233 |
This module is free software; you can redistribute it and/or modify it |
| 234 |
under the terms of either: |
| 235 |
|
| 236 |
a) the GNU General Public License as published by the Free Software |
| 237 |
Foundation; either version 1, or (at your option) any later version, |
| 238 |
|
| 239 |
or |
| 240 |
|
| 241 |
b) the "Artistic License" |
| 242 |
|
| 243 |
This program is distributed in the hope that it will be useful, but |
| 244 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 245 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU |
| 246 |
General Public License or the Artistic License for more details. |
| 247 |
|
| 248 |
For a copy of the GNU General Public License along with this program; if |
| 249 |
not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite |
| 250 |
330, Boston, MA 02111-1307 USA |
| 251 |
|