Catalyst::Controller::Constraints contains Constraint Signatures for Controller Actions.
SYNOPSIS
package MyApp::Controller::Foo;
...
use base qw(Catalyst::Controller::Constraints);
__PACKAGE__->config(
constraints => {
# allow only digits for type 'Integer'
Integer => qr/^d+$/,
# allow only word chars for type 'Word'
Word => sub { /^w+$/ },
# validate user id and inflate to object
User => {
# check the user id
check => sub {
my ( $self, $c, $id ) = @_;
return $c->is_valid_user_id( $id );
},
# forward to this action if the validation failed
on_fail => 'invalid_user',
# if value is valid, run it through this filter
# afterwards
post_filter => sub {
my ( $self, $c, $id ) = @_;
$c->fetch_user_by_id( $id );
},
}
# inheritance
HighInteger => {
inherit_from => 'Integer',
check => sub { $_ > 22 },
},
# collapse multiple arguments
MyDate => {
# take three integers and return one value
takes => 3,
gives => 1,
# inflate to a datetime object
post_filter => sub {
my ( $self, $c, $y, $m, $d ) = @_;
DateTime->new(
year => $y, month => $m, day => $d );
}
}
}
);
# add two integers, just throws exception on constraint failure
sub add : Local Args(2) Constraints(Integer a, Integer b) {
my ( $self, $c ) = @_;
$c->res->body( $_{a} + $_{b} );
}
# puts the word into the stash, under the key 'foo'
sub stashword : Local Args(1) Constraints( Word foo* ) { }
# user_obj ends as a user object in the stash
sub view_user : Local Args(1) Constraints( User user_obj* ) { }
sub invalid_user : Private {
# handle invalid userid
}
1;
Product's homepage
Requirements:
· Perl