HTML-FormHandlerX-Field-reCAPTCHA
view release on metacpan or search on metacpan
HTML::FormHandler
SYNOPSIS
The following is example usage.
In your HTML::FormHandler subclass, "MyApp::HTML::Forms::MyForm":
has_field 'recaptcha' => (
type=>'reCAPTCHA',
public_key=>'[YOUR PUBLIC KEY]',
private_key=>'[YOUR PRIVATE KEY]',
recaptcha_message => "You're failed to prove your Humanity!",
required=>1,
);
Example Catalyst controller:
my $form = MyApp::HTML::Forms::MyForm->new;
my $params = $c->request->body_parameters;
if(my $result = $form->process(params=>$params) {
## The Form is totally valid. Go ahead with whatever is next.
FIELD OPTIONS
We support the following additional field options, over what is
inherited from HTML::FormHandler::Field
public_key
The public key you get when you create an account on
http://recaptcha.net/
private_key
The private key you get when you create an account on
http://recaptcha.net/
use_ssl
control the 'use_ssl' option in Captcha::reCAPTCHA when calling
'get_html'.
recaptcha_options
reCAPTCHA'. This error message is in addition to any other constraints
you add, such as 'required'.
Please note that the recaptcha control also displays an error message
internal to itself.
FORM METHODS
The following methods or attributes can be set in the form which
contains the recapcha field.
$name_public_key or $name_private_key
"$name" is the name you gave to the reCAPTCHA field (the word directy
after the "has_field" command.
You may wish to set your public key from a method or attribute contained
from within the form. This would make it easier to have one form class
and use configuration tools, such as what Catalyst offers, to set the
pubic key. For example:
## In my form "MyApp::Form::MyForm
has ['MY_recaptcha_public_key', 'MY_recapcha_private_key'] => (
is=>'ro', isa=>'Str', required=>1,
);
has_field 'MY_recaptcha' => (
type=>'reCAPTCHA',
recaptcha_message => "You're failed to prove your Humanity!",
required=>1,
);
Then you might construct this in a Catalyst::Controller:
my $form = MyApp::Form::MyForm->new(
MY_recaptcha_public_key => $self->controller_public_key,
MY_recaptcha_private_key => $self->controller_private_key,
);
## 'process', etc.
Then your controller could populate the attributes
'controller_public_key' and 'controller_private_key' from your global
Catalyst configuration, allowing you to use one set of keys in
development and another for production, or even use different keys for
different forms if you wish.
SEE ALSO
The following modules or resources may be of interest.
HTML::FormHandler, Captch::reCAPTCHA
AUTHOR
lib/HTML/FormHandlerX/Field/reCAPTCHA.pm view on Meta::CPAN
use Moose;
extends 'HTML::FormHandler::Field';
our $VERSION = '0.04';
our $AUTHORITY = 'cpan:JJNAPIORK';
has '+widget' => ( default => 'reCAPTCHA' );
has '+input_param' => ( default => 'recaptcha_response_field' );
has [qw/public_key private_key/] => (is=>'rw', isa=>'Str', lazy_build=>1);
has 'use_ssl' => (is=>'rw', isa=>'Bool', required=>1, default=>0);
has 'remote_address' => (is=>'rw', isa=>'Str', lazy_build=>1);
has 'recaptcha_options' => (is=>'rw', isa=>'HashRef', required=>1, default=>sub{ +{} });
has 'recaptcha_message' => (is=>'rw', isa=>'Str', default=>'Error validating reCAPTCHA');
has 'recaptcha_instance' => (is=>'ro', init_arg=>undef, lazy_build=>1);
has 'encrypter' => (is=>'ro', init_arg=>undef, lazy_build=>1,
handles=>[qw/encrypt_hex decrypt_hex/]);
sub _build_public_key {
my $self = shift @_;
my $form = $self->form;
my $method = $self->name.'_public_key';
if ($form->can($method)) {
return $form->$method;
} else {
die "You either have to set the 'public_key' field option or defined a $method method in your form!";
}
}
sub _build_private_key {
my $self = shift @_;
my $form = $self->form;
my $method = $self->name.'_private_key';
if ($form->can($method)) {
return $form->$method;
} else {
die "You either have to set the 'private_key' field option or defined a $method method in your form!";
}
}
sub _build_encrypter {
my $self = shift @_;
my $key = pack("H16",$self->private_key);
return Crypt::CBC->new(-key=>$key,-cipher=>"Blowfish");
}
sub _build_remote_address {
$ENV{REMOTE_ADDR};
}
sub _build_recaptcha_instance {
Captcha::reCAPTCHA->new();
}
sub prepare_private_recaptcha_args {
my $self = shift @_;
return (
$self->private_key,
$self->prepare_recaptcha_args,
);
}
sub prepare_recaptcha_args {
my $self = shift @_;
return (
$self->remote_address,
$self->form->params->{'recaptcha_challenge_field'},
$self->form->params->{'recaptcha_response_field'},
lib/HTML/FormHandlerX/Field/reCAPTCHA.pm view on Meta::CPAN
=head1 SYNOPSIS
The following is example usage.
In your L<HTML::FormHandler> subclass, "MyApp::HTML::Forms::MyForm":
has_field 'recaptcha' => (
type=>'reCAPTCHA',
public_key=>'[YOUR PUBLIC KEY]',
private_key=>'[YOUR PRIVATE KEY]',
recaptcha_message => "You're failed to prove your Humanity!",
required=>1,
);
Example L<Catalyst> controller:
my $form = MyApp::HTML::Forms::MyForm->new;
my $params = $c->request->body_parameters;
if(my $result = $form->process(params=>$params) {
## The Form is totally valid. Go ahead with whatever is next.
lib/HTML/FormHandlerX/Field/reCAPTCHA.pm view on Meta::CPAN
=head1 FIELD OPTIONS
We support the following additional field options, over what is inherited from
L<HTML::FormHandler::Field>
=head2 public_key
The public key you get when you create an account on L<http://recaptcha.net/>
=head2 private_key
The private key you get when you create an account on L<http://recaptcha.net/>
=head2 use_ssl
control the 'use_ssl' option in L<Captcha::reCAPTCHA> when calling 'get_html'.
=head2 recaptcha_options
control the 'options' option in L<Captcha::reCAPTCHA> when calling 'get_html'.
lib/HTML/FormHandlerX/Field/reCAPTCHA.pm view on Meta::CPAN
'required'.
Please note that the recaptcha control also displays an error message internal
to itself.
=head1 FORM METHODS
The following methods or attributes can be set in the form which contains the
recapcha field.
=head2 $name_public_key or $name_private_key
"$name" is the name you gave to the reCAPTCHA field (the word directy after the
"has_field" command.
You may wish to set your public key from a method or attribute contained from
within the form. This would make it easier to have one form class and use
configuration tools, such as what L<Catalyst> offers, to set the pubic key.
For example:
## In my form "MyApp::Form::MyForm
has ['MY_recaptcha_public_key', 'MY_recapcha_private_key'] => (
is=>'ro', isa=>'Str', required=>1,
);
has_field 'MY_recaptcha' => (
type=>'reCAPTCHA',
recaptcha_message => "You're failed to prove your Humanity!",
required=>1,
);
Then you might construct this in a L<Catalyst::Controller>:
my $form = MyApp::Form::MyForm->new(
MY_recaptcha_public_key => $self->controller_public_key,
MY_recaptcha_private_key => $self->controller_private_key,
);
## 'process', etc.
Then your controller could populate the attributes 'controller_public_key' and
'controller_private_key' from your global L<Catalyst> configuration, allowing
you to use one set of keys in development and another for production, or even
use different keys for different forms if you wish.
=head1 SEE ALSO
The following modules or resources may be of interest.
L<HTML::FormHandler>, L<Captch::reCAPTCHA>
=head1 AUTHOR
package Test::HTML::FormHandlerX::Field::reCAPTCHA;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has_field 'recaptcha' => (
type=>'reCAPTCHA',
public_key=>'public',
private_key=>'private',
required=>1,
);
use Test::More;
ok my $form = Test::HTML::FormHandlerX::Field::reCAPTCHA->new,
'Created Form';
done_testing;
t/recaptcha_field.t view on Meta::CPAN
with 'HTML::FormHandlerX::Widget::Field::reCAPTCHA';
has '+is_html5' => (default=>1);
has_field 'recaptcha' => (
type=>'reCAPTCHA',
recaptcha_message => "Non hai dimostrato di essere un umano!",
required=>1,
);
has ['recaptcha_public_key','recaptcha_private_key'] => (is => 'rw', isa=>'Str', required=>1);
no HTML::FormHandler::Moose;
package ::main;
#use_ok('Test::reCAPTCHA');
my $public_key = 'zio pino';
my $private_key = 'zio can';
my $form = Test::reCAPTCHA->new(recaptcha_public_key=>$public_key,recaptcha_private_key=>$private_key);
ok($form, 'get form');
$form->process(params => {});
ok($form->field('recaptcha')->render, 'OK recaptcha');
my $expected = q(
<div>
<label for="recaptcha">Recaptcha</label>
( run in 0.234 second using v1.01-cache-2.11-cpan-4d50c553e7e )