[Catalyst] Validating single arg id

J. Shirley jshirley at gmail.com
Fri Oct 16 14:34:54 GMT 2009


On Fri, Oct 16, 2009 at 7:19 AM, Matt Whipple <matt at mattwhipple.com> wrote:

>
>>  I would probably avoid potentially hitting the database unnecessarily,
> and only use the exception in the case of complicated validation.  In the
> case of simple validation, why not something simple along the lines of:
>
> sub some_action {
>   my ($self, $c) =3D (shift, shift);
>   #Get type checking out of the way
>   my $id =3D check_id(shift, $c));
>       #Continue on with apparently good data
> };
>
> sub check_id {
>   my ($id, $c) =3D @_;
>   $c->res->status(404) if ( !validate_id($id) );
>   return $id;
> };
>
> sub validate_id {
>   return 1 if looks_like_number(shift);
>   return;
> };
>
>>
>>
>>
If you are using DBIx::Class, just define your schema's default resultset
class to have this method (safe_find) or something (using
'default_resultset_class' in ->load_namespaces)

package MyApp::Schema::ResultSet;

use Moose;
extends 'DBIx::Class::ResultSet';

sub safe_find {
    my ( $self, $id ) =3D @_;
    if ( $id =3D~ /^(\d+)$/ ) {
         return $self->find($1);
    }
    die "I pity the fool";
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20091016/7df4f=
03f/attachment.htm


More information about the Catalyst mailing list