On Fri, Oct 16, 2009 at 7:19 AM, Matt Whipple <m...@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) = (shift, shift);
>   #Get type checking out of the way
>   my $id = check_id(shift, $c));
>       #Continue on with apparently good data
> };
>
> sub check_id {
>   my ($id, $c) = @_;
>   $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 ) = @_;
    if ( $id =~ /^(\d+)$/ ) {
         return $self->find($1);
    }
    die "I pity the fool";
}
_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to