Recaptcha in SQLFORM

224 views
Skip to first unread message

Thadeus Burgess

unread,
Nov 21, 2009, 9:11:24 AM11/21/09
to web...@googlegroups.com
Is it possible to append Recaptcha to a SQLFORM, or SQLFORM.factory.

This would be ideal.

form = SQLFORM(db.table)

if somesettings.recaptcha=True

form.append(Recaptcha(....))

-Thadeus


Thadeus Burgess

unread,
Nov 21, 2009, 9:12:32 AM11/21/09
to web...@googlegroups.com
And access it through custom form with..

{{=form.custom.widget.recaptcha}}

-Thadeus

mr.freeze

unread,
Nov 21, 2009, 10:25:50 AM11/21/09
to web2py-users
Something like this?:

def index():
use_recaptcha = True
recaptcha_public = "..."
recaptcha_private = "..."
captcha = lambda f,v: Recaptcha(request,
recaptcha_public,recaptcha_private) if use_recaptcha else ''
form = SQLFORM.factory(Field('test',requires=IS_NOT_EMPTY()),
Field('test2',widget=captcha)
)
if form.accepts(request.vars,session):
response.flash = "Got it"
return dict(form=form)

Or if you don't need it in form.custom you can just insert it into the
form:

def index():
use_recaptcha = True
recaptcha_public = "..."
recaptcha_private = "..."
captcha = Recaptcha(request, recaptcha_public,recaptcha_private)
form = SQLFORM.factory(Field('test',requires=IS_NOT_EMPTY()),Field
('test2'))
if use_recaptcha:
form[0].insert(-1, TR('', captcha, ''))
if form.accepts(request.vars,session):
response.flash = "Got it"
return dict(form=form)

On Nov 21, 8:12 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> And access it through custom form with..
>
> {{=form.custom.widget.recaptcha}}
>
> -Thadeus
>

Thadeus Burgess

unread,
Nov 21, 2009, 11:49:37 AM11/21/09
to web...@googlegroups.com
Ideally I would be able to create a form using SQLFORM(db.table) and then insert the recaptcha field after the fact. I need to be able to display the field in form.custom. But I may just be dreaming here :)

Using your example, it gives a no_data error

-Thadeus

mr.freeze

unread,
Nov 21, 2009, 12:34:46 PM11/21/09
to web2py-users
The strange thing is that the captcha validation actually succeeds
(you can test by putting an incorrect value in the captcha form and
see if fail properly). I'm not sure why form.accepts is putting
no_data into form.errors. Perhaps a bug?

mr.freeze

unread,
Nov 21, 2009, 12:42:03 PM11/21/09
to web2py-users
It seems that setting a default on the field fixes it:

form = SQLFORM.factory(Field('test',requires=IS_NOT_EMPTY()),
Field('test2',widget=captcha,default='')
)

Thadeus Burgess

unread,
Nov 21, 2009, 12:47:17 PM11/21/09
to web...@googlegroups.com
I believe it is the way that SQLFORM looks for data in request.vars.

It will look for a var with the name of the field...

so

Field('recaptcha', widget=captcha)

It is expecting a request.vars.recaptcha, which does not exist.

You're right, setting default does fix it, sounds like a bug...

So it seems that by setting a default, it is allowing SQLFORM to proceed, even though it is not getting anything from request.vars.

I even tried setting the field name to recaptcha_response_field with no luck, but the default trick works! Lol you're awesome Freeze

-Thadeus

Thadeus Burgess

unread,
Nov 21, 2009, 12:49:04 PM11/21/09
to web...@googlegroups.com
How do you append a Field() to SQLFORM? Does not look like it is possible.

-Thadeus

Thadeus Burgess

unread,
Nov 21, 2009, 12:56:31 PM11/21/09
to web...@googlegroups.com
Mainly because I really get agitated when I have to do silly things such as

if do_captcha:
    form = SQLFORM.factory(db.table.field1, db.table.field2, db.table.field3, Field('anti_spam', widget=captcha, default=''))
else:
    form = SQLFORM(db.table)

-Thadeus

Thadeus Burgess

unread,
Nov 21, 2009, 1:20:05 PM11/21/09
to web...@googlegroups.com
And unfortunately, it seems that using Recaptcha in a plugin will not work.

ajax_trap seems to be keeping anything from happening. Works fine outside of the plugin

It also does not work for the following

LOAD(... ajax=True)
LOAD(... ajax_trap=True)
LOAD(... ajax=False, ajax_trap=False)

-Thadeus

Thadeus Burgess

unread,
Nov 21, 2009, 1:26:23 PM11/21/09
to web...@googlegroups.com
The form submits to the server, and the data ends up getting posted.

However when the page refreshes, it goes blank and freezes with the message "reading api-secure.recaptcha.net"

-Thadeus

mr.freeze

unread,
Nov 21, 2009, 1:26:50 PM11/21/09
to web2py-users
I know what you mean. I don't think it's possible as the form is
built SQLFORM's __init__ function. You might have some luck
dynamically building a collection of fields then passing them like so:

form = SQLFORM.factory(*fields)

On Nov 21, 11:56 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> Mainly because I really get agitated when I have to do silly things such as
>
> if do_captcha:
>     form = SQLFORM.factory(db.table.field1, db.table.field2,
> db.table.field3, Field('anti_spam', widget=captcha, default=''))
> else:
>     form = SQLFORM(db.table)
>
> -Thadeus
>
> On Sat, Nov 21, 2009 at 11:49 AM, Thadeus Burgess <thade...@thadeusb.com>wrote:
>
> > How do you append a Field() to SQLFORM? Does not look like it is possible.
>
> > -Thadeus
>

Thadeus Burgess

unread,
Nov 21, 2009, 1:39:56 PM11/21/09
to web...@googlegroups.com
I will do this in functions, so resources only get allocated if needed.

def captcha_form():
    return SQLFORM.factory(db.table.field1, db.table.field2, Field("anti_spam", widget=captcha, default='')
def normal_form():
    return SQLFORM(db.table)

if docaptcha:
  form = captcha_form()
else
  form = normal_form()

Still having a problem with using recaptcha inside of ajax_trap. It works in plug-ins as long as ajax_trap or ajax are false.

-Thadeus

mdipierro

unread,
Nov 21, 2009, 6:42:36 PM11/21/09
to web2py-users
Although you may be able to do this, captcha is not a widget because
as pointed out it is not a field.
This is the best way.

form[0].append(TR('',captcha, ''))

Massimo
Reply all
Reply to author
Forward
0 new messages