Catalyst::Plugin::StructuredParameters - Plug to add the structured parameter request trait plus proxy methods
package MyApp; use Catalyst 'StructuredParameters'; MyApp->setup; package MyApp::Controller::Root; sub body :Local { my ($self, $c) = @_; my %clean = $c->structured_body ->permitted(['person'], +{'email' => []}) ->namespace(['person']) ->permitted( 'name', 'age', 'address' => ['street' => ['number', 'zip'], +{'credit_cards' => [ 'number', 'exp' => [qw/year month day/], ]}, )->to_hash; ## Do something with the sanitized body parameters } ## Don't forget to add code to handle any exceptions sub end :Action { my ($self, $c) = @_; if(my $error = $c->last_error) { $c->clear_errors; ## Clear the error stack unless you want the default Catalyst error if($c->isa_strong_parameter_exception($error)) { ## Something here like return a Bad Request 4xx view or similar. } } } ## Alternatively handle with L<CatalystX::Errors> (don't forget to add the plugin to your ## application class.) sub end :Action Does(RenderErrors) { }
You should review Catalyst::TraitFor::Request::StructuredParameters for a more detailed SYNOPSIS and explanation of how all this works.
This plugin will add in the Catalyst::TraitFor::Request::StructuredParameters request class trait and proxy some of its methods to the context. You might find this a bit less typing.
All the main documentation is in Catalyst::TraitFor::Request::StructuredParameters.
NOTE: This plugin only works with For Catalyst v5.90090 or greater. If you must use an older version of Catalyst you'll need to use the workaround described in the SYNOPSIS of Catalyst::TraitFor::Request::StructuredParameters.
This role defines the following methods:
These just proxy to the same methods under the Catalyst::Request object.
This is just a convenience method that returns true if a possible exception is both a blessed object and ISA Catalyst::Exception::StructuredParameter. Since you need to add checking for this everytime I added this method to save a bit of trouble.
See Catalyst::TraitFor::Request::StructuredParameters
Catalyst, Catalyst::TraitFor::Request::StructuredParameters
To install Catalyst::TraitFor::Request::StructuredParameters, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::TraitFor::Request::StructuredParameters
CPAN shell
perl -MCPAN -e shell install Catalyst::TraitFor::Request::StructuredParameters
For more information on module installation, please visit the detailed CPAN module installation guide.