Data-FormValidator-Constraints-HTTP
view release on metacpan or search on metacpan
lib/Data/FormValidator/Constraints/HTTP.pm view on Meta::CPAN
constraint methods for validating HTTP request methods. For example,
it may be desirable to consider a form invalid until the request method
is POST.
=head1 INTEGRATION WITH THE CATALYST WEB FRAMEWORK
I have found this technique of making forms invalid unless the request
method is POST to be rather useful within the Catalyst web framework,
using the FormValidator and FillInForm plugins.
The FillInForm plugin will automatically fill in an HTML form with the
values located in $c->request->parameters *AS LONG AS THE CURRENT FORM
IS INVALID*. We can use this behaviour to make our lives simpler. By
placing the HTTP method constraint method in our validation profile,
we can be guaranteed that FillInForm will engage if the method is not
POST (it may still engage even if the method *IS* POST, depending on
the form validation profile and the client's provided input).
package My::App;
use Catalyst qw( Static::Simple FillInForm FormValidator );
1;
...
package My::App::Controller::Root;
use base qw( Catalyst::Controller );
sub auto : Private {
my ($self, $c) = @_;
# The HTTP request method must be placed into the request
# parameters in order for the FormValidator plugin to check it.
# This can easily be done in the root controller's "auto"
# action to avoid this in the various controllers.
# Just another tip to make the code cleaner. :)
$c->request->parameter( method => $c->request->method );
1;
}
1;
...
package My::App::Controller::Foo;
use Data::FormValidator::Constraints::HTTP qw( POST );
sub update : Local {
my ($self, $c, $foo) = @_;
$foo = $c->model('Schema::Foo')->find( $foo );
$c->form(
required => [ qw( method name author ) ],
constraint_methods => {
method => POST,
name => FV_min_length( 6 ),
# ... yadda, yadda, yadda
},
);
if ($c->form->success) {
# you can be sure this will only be reached if the request
# method is POST and the rest of the request parameters
# have successfully passed the rest of your form validation
# profile.
$foo->update_from_form( $c->form );
}
else {
# By setting the parameters in this manner, FillInForm will
# automatically fill in the HTML form using the current
# object values, being overridden by any request parameters
# already specified. Meaning, if $foo get a field called
# 'title' and its value had already been set, FillInForm
# will place that value into the HTML form being presented
# to the client. However, if the request parameters include
# a value for 'title', *THAT* value gets placed in the
# HTML form.
$c->request->parameters({
$foo->get_columns,
%{ $c->request->parameters },
});
}
}
1;
=head1 METHODS
=head2 http_method ( $method )
Returns a constraint method to determine whether or not a method is
equal to the provided variable.
=head2 DELETE ( )
=head2 GET ( )
=head2 OPTIONS ( )
=head2 POST ( )
=head2 PUT ( )
=head2 TRACE ( )
( run in 2.484 seconds using v1.01-cache-2.11-cpan-5735350b133 )