{{=form.custom.begin}} - How set an ID to form

782 views
Skip to first unread message

arnaud

unread,
Feb 19, 2010, 3:05:55 AM2/19/10
to web2py-users
Hi,
I'm newbie with python and Web2Py. My problem : I want customize my
register form.

This is my code

#model
auth_table = db.define_table(auth.settings.table_user_name,
Field('first_name', length=128, default=''),
Field('last_name', length=128, default=''),
Field('email', length=128, default='', unique=True),
Field('password', 'password',length=256,readable=False,
label='Password'),
Field('registration_key', length=128, default= '',writable=False,
readable=False),
Field('orgUrl','string'),
Field('orgName','string'),
Field('orgAddress1','string',widget=patternNormal),
Field('orgAddress2'),
Field('orgZipCode'),
Field('orgCity1'),
Field('orgCountry','string'),
Field('orgDescription','text'),
Field('orgActivityDomain','string'),
Field('orgLogo','upload'),
Field('acceptCgi','boolean'),
Field('acceptMailing','boolean'),
Field('locale',default='fr_FR',writable=False,readable=False)
)

#controller
def register():
return dict(form=auth.register())

#view
{{=form.custom.begin}}
{{=form.custom.widget.first_name}}
...
{{=form.custom.end}}

{{=form.custom.begin}} serialize this code : <form method="post"
enctype="multipart/form-data" action="">

I want to set an ID to this form. What's the best way for achieve my
goal ?

Thanks

mdipierro

unread,
Feb 19, 2010, 7:41:10 AM2/19/10
to web2py-users
What do you mean by setting an ID?

Thadeus Burgess

unread,
Feb 19, 2010, 12:03:07 PM2/19/10
to web...@googlegroups.com
he wants a HTML CSS id on the form.

-Thadeus

> --
> You received this message because you are subscribed to the Google Groups "web2py-users" group.
> To post to this group, send email to web...@googlegroups.com.
> To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
>
>

mdipierro

unread,
Feb 19, 2010, 12:31:50 PM2/19/10
to web2py-users
aha

form=SQLFORM(.....,_id='thisformid')

or

form['_id'] = 'thisformid'

the latter works with crud forms too. Remember that

class SQLFORM(FORM): ... is a helper.

On Feb 19, 11:03 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> he wants a HTML CSS id on the form.
>
> -Thadeus
>

Thadeus Burgess

unread,
Feb 19, 2010, 12:42:30 PM2/19/10
to web...@googlegroups.com
Yes but with crud, or in his case auth.register_form()

-Thadeus

mdipierro

unread,
Feb 19, 2010, 12:44:45 PM2/19/10
to web2py-users
It is the same

def register():
form=auth.register_form()
form['_id'] = 'my_form_id'
return dict(form=form)

On Feb 19, 11:42 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> Yes but with crud, or in his case auth.register_form()
>
> -Thadeus
>

arnaud

unread,
Feb 22, 2010, 3:09:33 PM2/22/10
to web2py-users
Hi Massimo,

when I test your code (platform : windows / web2py 1-754), it's return
the following error :

Traceback (most recent call last):
File "gluon/restricted.py", line 173, in restricted
File "C:/dev/web2py1754/applications/sites/controllers/user.py",
line 21, in <module>
File "gluon/globals.py", line 96, in <lambda>
File "C:/dev/web2py1754/applications/sites/controllers/user.py",
line 8, in register
AttributeError: 'Auth' object has no attribute 'register_form'

mdipierro

unread,
Feb 23, 2010, 1:50:35 AM2/23/10
to web2py-users
Sorry by bad

def register():
form=auth.registe()


form['_id'] = 'my_form_id'
return dict(form=form)

mdipierro

unread,
Feb 23, 2010, 1:50:43 AM2/23/10
to web2py-users
def register():
form=auth.register()

form['_id'] = 'my_form_id'
return dict(form=form)

arnaud

unread,
Feb 23, 2010, 3:07:07 PM2/23/10
to web2py-users
Thanks Massimo, but this code has no effect. No ID is set to form.
SQLform is maybe the good solution.

mdipierro

unread,
Feb 23, 2010, 7:10:18 PM2/23/10
to web2py-users
It does work:

$ python web2py.py -S welcome -M


>>> form=auth.register()
>>> form['_id'] = 'my_form_id'

>>> print form.xml()
<form action="" enctype="multipart/form-data" id="my_form_id"
method="post">....</form>

perhaps I misunderstood the question and you expect something
different?

arnaud

unread,
Feb 24, 2010, 9:50:36 AM2/24/10
to web2py-users
Massimo,

no misundestood. I expect a css ID in my form.

I have run the same test on welcome app. That's work fine.

But in my app, no ID for my form.

It's perhaps a "bug" in view with the tag {{=form.custom.begin}}.

DenesL

unread,
Feb 24, 2010, 10:38:18 AM2/24/10
to web2py-users

The problem is that form.custom.begin is evaluated when the form is
created.
Changing '_id' after it has been created does not recalculate
form.custom.begin, but you can do so manually:

form['_id']='my_form_id'
form.custom.begin=XML("<%s %s>") %(form.tag,form._xml()[0]))

Denes.

mdipierro

unread,
Feb 24, 2010, 9:00:04 PM2/24/10
to web2py-users
You are right. Perhaps it should be re-initialized inside accept? What
do you think?

DenesL

unread,
Feb 24, 2010, 10:48:25 PM2/24/10
to web2py-users
The id is only one case.
You would have to recreate the whole thing to be sure everything is
updated but that makes it slower.

Ideally you would do these things at form creation or by setting
defaults beforehand ala field defaults.
Doing it at form creation means allowing **attributes in any function
that creates a form.

Thadeus Burgess

unread,
Feb 24, 2010, 10:55:33 PM2/24/10
to web...@googlegroups.com
And hence the inherent flaw in SQLFORM :)

Though I can't complain since I have not devoted time to redesigning
the system as I so very much would like to do.

-Thadeus

Thadeus Burgess

unread,
Feb 24, 2010, 10:56:21 PM2/24/10
to web...@googlegroups.com
and for the record SQLFORM freaking rocks. It just needs to be
redesigned with customization in mind.

-Thadeus

DenesL

unread,
Feb 24, 2010, 11:41:36 PM2/24/10
to web2py-users
What do you mean?
SQLFORM is a FORM so you can pass **attributes.

form=SQLFORM(db.person,_id='myid')


On Feb 24, 10:56 pm, Thadeus Burgess <thade...@thadeusb.com> wrote:
> and for the record SQLFORM freaking rocks. It just needs to be
> redesigned with customization in mind.
>
> -Thadeus
>

> On Wed, Feb 24, 2010 at 9:55 PM, Thadeus Burgess <thade...@thadeusb.com> wrote:
> > And hence the inherent flaw in SQLFORM :)
>
> > Though I can't complain since I have not devoted time to redesigning
> > the system as I so very much would like to do.
>
> > -Thadeus
>

arnaud

unread,
Mar 10, 2010, 9:45:58 AM3/10/10
to web2py-users
Sorry for the delay. Your Solution works fine for me.

Thanks a lot

> > >>> > The problem is thatform.custom.beginis evaluated when the form is

Reply all
Reply to author
Forward
0 new messages