Using validators with existing forms

10 views
Skip to first unread message

voltron

unread,
Feb 14, 2008, 8:41:34 AM2/14/08
to web2py Web Framework
Is it possible to use validators with existing forms? Can I just
create a form instance and assign request.vars values to it?

Thanks

Massimo Di Pierro

unread,
Feb 14, 2008, 9:49:35 AM2/14/08
to web...@googlegroups.com
If I understand the question you have two options,

define the form its own validation outside an controller (ugly) or
put the form in its own iframe or div with its own controller, like
you login in google checkout and other apps.

Massimo

voltron

unread,
Feb 14, 2008, 11:10:53 AM2/14/08
to web2py Web Framework
Oh, what I meant was existing HTML forms, sometimes one hast to work
with HTML pages created by a designer, so in these cases it would be
cool just to plug in the validation

Massimo Di Pierro

unread,
Feb 14, 2008, 12:33:05 PM2/14/08
to web...@googlegroups.com
You can, just make a form with the input fields you need and use

form.accepts(request.vars,formname=None)

do not pass session.

Massimo

voltron

unread,
Feb 14, 2008, 12:39:51 PM2/14/08
to web2py Web Framework
Thanks

voltron

unread,
Feb 15, 2008, 7:48:09 AM2/15/08
to web2py Web Framework
One last thing, how can I extract the form method, this produced a
"none":

form.vars.method

Or can it only be retrived from the request object? If yes, how?

Thanks

mdipierro

unread,
Feb 15, 2008, 9:32:11 AM2/15/08
to web2py Web Framework
the method is an attribute of the form object.

You set is with

form=FORM(....,_method='GET') # default is post

and retrieve it by

method=form.attributes['_method']

Same with _id, _class, and any other attribute.

Massimo

DenesL

unread,
Mar 12, 2008, 8:00:22 PM3/12/08
to web2py Web Framework
Hi voltron, did you already try using an existing form as suggested?

voltron

unread,
Mar 13, 2008, 4:02:46 AM3/13/08
to web2py Web Framework
Yes, it works very well :-)

DenesL

unread,
Mar 13, 2008, 12:04:05 PM3/13/08
to web2py Web Framework
Did you have to do anything to your existing form to make it work?.
Do you use it to update records or just display them?
Thanks.

voltron

unread,
Mar 13, 2008, 12:16:33 PM3/13/08
to web2py Web Framework
I did not alter the exiting form, I have used this method for updating
too. The only thing you would have to watch out for is passing no
session when trying to validate the form

DenesL

unread,
Mar 13, 2008, 2:17:22 PM3/13/08
to web2py Web Framework
In my tests request.vars must contain the hidden value _formname even
for formname=None, otherwise the accepts fails.
If you are not using the generated form then you must include this
field in your existing form so it will be in request.vars when the
user submits it.
Maybe you have a different setup.

Massimo Di Pierro

unread,
Mar 13, 2008, 2:23:34 PM3/13/08
to web...@googlegroups.com
This is not supposed to be the case. If it is it is a bug.

voltron

unread,
Mar 13, 2008, 2:28:13 PM3/13/08
to web2py Web Framework
Hmm, thats strange, how many forms do yuóu have on the same page? It
works for me

DenesL

unread,
Mar 13, 2008, 2:54:26 PM3/13/08
to web2py Web Framework
Number of forms is not a factor.

voltron

unread,
Mar 13, 2008, 3:23:21 PM3/13/08
to web2py Web Framework
then it might be best to show your forms, the existing form and the
form validation code. I just used existing forms that the designers
present me

DenesL

unread,
Mar 22, 2008, 12:39:35 AM3/22/08
to web2py Web Framework
Massimo, I sent you an example a while ago showing you that the hidden
_formname is required, did you receive it?.

Voltron, let me get this straight: in order for an external form to
pass the accepts test the HTML form field names must match with the
SQLField names, so you or your designers must make sure of this.
Right?.

Massimo Di Pierro

unread,
Mar 22, 2008, 1:01:58 AM3/22/08
to web...@googlegroups.com
please send it again. I do not recall. Here is my test:

$ python shell.py test
(InteractiveConsole)
>>> form=FORM(INPUT(_name='test'))
>>> form.accepts(request.vars,formname=None)
True
>>> form.xml()
'<form enctype="multipart/form-data" method="post"><input name="test"/
></form>'

No hidden formname field.

Massimo

voltron

unread,
Mar 22, 2008, 1:00:08 PM3/22/08
to web2py Web Framework
Hi DenesL, yes , otherwise it would not work.

DenesL

unread,
Mar 22, 2008, 8:29:48 PM3/22/08
to web2py Web Framework
Ah! I am using SQLFORM:

>>> form=SQLFORM(db.xx)
>>> form.accepts(request.vars,formname=None)
False
>>> form.xml()
...<input value="&lt;table xx&gt;" type="hidden" name="_formname"/></
form>


On Mar 22, 12:01 am, Massimo Di Pierro <mdipie...@cs.depaul.edu>
wrote:

Massimo Di Pierro

unread,
Mar 22, 2008, 9:23:17 PM3/22/08
to web...@googlegroups.com
yes then formname is required!

Massimo

On Mar 22, 2008, at 7:29 PM, DenesL wrote:

&lt;table xx&gt;"


cj

unread,
Mar 24, 2008, 6:35:53 PM3/24/08
to web2py Web Framework
DenesL,

Your right you do need formname=None for it to work even with the HTML
FORM helper. It must be a bug.....

Massimo Di Pierro

unread,
Mar 24, 2008, 6:50:44 PM3/24/08
to web...@googlegroups.com
I will look into this it is always a good idea to give a form name
anyway.
are you setting formname=None in accept?

Massimo

cj

unread,
Mar 24, 2008, 6:57:57 PM3/24/08
to web2py Web Framework
Yes....if i don't and name it in the form/view it doesn't work. It
only works if i do:

add_user.accepts(request.vars,formname=None)

Massimo Di Pierro

unread,
Mar 24, 2008, 7:08:41 PM3/24/08
to web...@googlegroups.com
that's how it is supposed to be.

Massimo

Reply all
Reply to author
Forward
0 new messages