Skip navigation

These captchas are different from most. Basically, using Gantry::Utils::Captcha you define a set of thumbnail images along with some other properties. The util will hand you a random captcha ( be sure to shuffle ) and then it’s up to you to place it on the form. When the form is displayed, the user is required to select the correct text for the current captcha. Below is a code sample where I’m using the captcha util.

 sub do_view {
    my( $self, $id ) = @_;

    my %params = $self->get_param_hash;

    my $sch  = $self->get_schema();
    my $blog = $sch->resultset( 'blog' )->find( $ident );

    my $captcha = Gantry::Utils::Captcha->new( 'mysecret' ); 

    $captcha->add( {
        base_url => $self->doc_rootp() . "/images",
        image    => 'captcha-image-1.jpg',
        key      => 'chair',
        label    => 'looks like a chair to me',
        alt_text => 'sometimes it feels reaally good to sit',
    } );

    $captcha->add( {
        base_url  => $self->doc_rootp() . "/images",
        image     => 'captcha-image-2.jpg',
        key       => 'yoda',
        label     => 'hrm ... yoda you are',
        alt_text  => 'use the force, Luke',
    } );

    $captcha->add( {
        base_url  => $self->doc_rootp() . "/images",
        image     => 'captcha-image-3.jpg',
        key       => 'biking',
        label     => 'biking is fun',
        alt_text  => 'two wheels and a seat',
    } );

    $captcha->shuffle();

    if ( $blog ) {
        $self->stash->view->template( 'blog.tt' );
        $self->stash->view->data( $blog );

        # munger the comment form
        my $form = Apps::Blogger::GEN::Blog::Comment::form( $self );

        my $munger = Gantry::Utils::FormMunger->new( $form );

        $munger->add_field_after( 'body', {
            type    => 'html',
            name    => 'captcha_image',
            label   => 'choose wisely',
            optional => 1,
            html    => $captcha->image_link,
        } );

        $munger->add_field_after( 'captcha_image', {
            class   => 'captcha-image',
            name    => 'captcha',
            type    => 'select',
            label   => ' ',
            options => $captcha->gantry_form_options(),
        } );

        $self->stash->view->form( $form );

        if ( $self->is_post() ) {

            if ( defined $params{cancel} ) {
                $self->relocate( $self->location );
            }

            my $results = Data::FormValidator->check(
                \%params,
                form_profile( $form->{fields} ),
            );

            my $has_errors = 0;

            unless ( $captcha->valid( $params{captcha} ) ) {
                $results->{invalid}{captcha} = "try again";
                $has_errors = 1;
            }

            $has_errors = 1 if ( $results->has_invalid );
            $has_errors = 1 if ( $results->has_missing );

            # Form has errors
            if ( $has_errors ) {
                $self->stash->view->form->results( $results );
            }
            # Form looks good, make update
            else {
                clean_params( \%params, $form->{fields} );

                delete( $params{submit} );
                delete( $params{captcha} ); # be sure to delete

                $params{created} =  $self->date_now;

                my $comment = $sch->resultset( 'comment' )->create(
                    \%params
                );

            }
        }
    }
    else {
        die "blog entry not found\n";
    }
  }
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.