how to reuse form definition

13 views
Skip to first unread message

Jose Soares

unread,
Jan 29, 2007, 11:47:46 AM1/29/07
to turbo...@googlegroups.com
Hi,

I have defined two forms; one to add a new record and another to update
data.
I don't want to define two forms where only one fields is different
between them.
I'm trying this...

class EditFields(widgets.WidgetsList):
codice = widgets.HiddenField(name='codice')
codice_cia = widgets.TextField(name='codice_cia',label='Codice
CIA',
nome = widgets.TextField(name='nome', validator=NotEmpty)
salva =
widgets.SubmitButton(name='salva',label='',default='salva')
exit = widgets.Button(name='exit', default='esci',
label='', attrs = dict(onclick="history.back()

class AddFields(EditFields):
codice = widgets.TextField(name='codice')


but the Add Form shows only one field (codice) but Edit Form shows all
fields.
Is there a way to do that?

Thank you,
jo

Marco Mariani

unread,
Jan 29, 2007, 8:12:26 AM1/29/07
to turbo...@googlegroups.com
Jose Soares wrote:
> Hi,
>
> I have defined two forms; one to add a new record and another to update
> data.
>
I don't see form definitions in your code, there are field lists.

I suppose somewhere in the form creation you call

fields=EditFields()

fields=AddFields()


Replace the latter with fields=EditFields()+AddFields() (WidgetsList
are lists, not compound widgets or anything magic)

and replace AddFields(EditFields) with AddFields(widgets.WidgetsList)


This should be a FAQ...

Jose Soares

unread,
Jan 29, 2007, 2:31:26 PM1/29/07
to turbo...@googlegroups.com
Hi Marco,

Your example is very interesting but in my case it doesn't work, because
I don't want to add some items, I want to replace some items instead.

Take a look at field 'codice', in EditFields it is a HiddenField while
in AddFields it is a TextField
....

class EditFields(widgets.WidgetsList):
codice = widgets.HiddenField(name='codice')

...

class AddFields(EditFields):
codice = widgets.TextField(name='codice')

jo

Marco Mariani ha scritto:

gasolin

unread,
Jan 29, 2007, 10:11:02 AM1/29/07
to TurboGears
You could use one widget form to handle both new & edit interface

http://docs.turbogears.org/1.0/CRUDTemplate

Marco Mariani

unread,
Jan 29, 2007, 10:43:31 AM1/29/07
to turbo...@googlegroups.com
Jose Soares wrote:
> Your example is very interesting but in my case it doesn't work, because
> I don't want to add some items, I want to replace some items instead.
>
So you factor out CommonFields() and compose the widget lists as you wish.
It has to work.

jose

unread,
Jan 30, 2007, 3:39:42 AM1/30/07
to turbo...@googlegroups.com
Marco Mariani wrote:

You are right, Marco, and it works fine. Thank you!

jo

Reply all
Reply to author
Forward
0 new messages