On 02/05/2006, at 13:52, Ed Singleton wrote:

>
> On 27/04/06, Michele Cella <[EMAIL PROTECTED]> wrote:
>>
>> Hi Ed,
>>
>> What about using a FieldSet to group related fields togheter? with  
>> CSS
>> you can still a FieldSet as much as you need I think.
>
> I guess so, but I really would like to work out how to create my own
> form fields.  Is there no explanation of how the widgets work
> internally?

Try help(turbogears.widgets.base). There's some info there in the  
docstrings.
>
>> Keep in mind that when you use a FieldSet, that's a  
>> CompoundWidget, you
>> receive as input a dictionary with a key for every field of the field
>> set, so in end from the form you receive a nested dictionary.
>>
>> To prepolute such a form you need to pass a nested dictionary of the
>> same structure at display/render as a value.
>>
>> Example:
>>
>> class PersonalDetailsFields(WidgetsList):
>>     name = TextField()
>>
>> class BankDetailsFields(WidgetsList):
>>     credit_card = TextField()
>>
>> class FormFields(WidgetsList):
>>     personal_details = FieldSet(fields=PersonalDetailsFields())
>>     credit_card = FieldSet(fields=BankDetailsFields())
>>
>> as input you receive two arguments personal_details and credit_card,
>> they are two dicts:
>>
>> credit_card = {'name': 'michele'}
>> personal_details = {'credit_card': '1231231234'}
>>
>> If you need to prepolutate such a form pass a value dict like this  
>> one:
>>
>> value = dict(credit_card={'name': 'michele'},
>>                     personal_details={'credit_card': '1231231234'})
>>
>> and then:
>>
>> form.display(value=your_values_dict)
>>
>> Hope this helps, I think this is the best way since a FieldSet really
>> helps here, or you can just cook your own CompoundFormField that
>> provides headings, you just need a quick look at how the FieldSet is
>> done, the template is easily accessible in this way:
>>
>>>>> from turbogears.widgets import FieldSet
>>>>> print FieldSet.template
>
> Thanks for this.  It was enough info for me to do a rough draft of
> what I want.  It's untested and needs more stuff adding, but it's
> basically what I want.  (Though I really want to know how I add extra
> parameters and stuff).  It's the RepeatingFieldSet with a different
> template (uses the legend for the header content):

Adding extra parameteres for the widget is as easy as listing them at  
"params":

class TableWithHeaders(RepeatingFormField):
        ....
        params = ["header"]
        header = "Default header"

Every parameter listed at params will magically flow from the class  
attribute to the template (via update_params) allowing you to  
override it at __init__ , in upfate_params or display, example:

t = TableWithHeaders()
assert "Default Header" in t.render()

t = TableWithHeaders(header="Init overriden")
assert "Init overriden" in t.render()

assert "Display overriden" in t.render(header="Display overriden")

class TableWithHeaders2(RepeatingFormField):
        ....
        params = ["header"]
        header = "Default header"
        def update_params(self, params):
                params["header"] = params["header"] + "is a header"

t = TableWithHeaders()
assert "Default Header is a header" in t.render()

You can also have a callable and it will be automatically called on  
each display to get fresh values.

class TableWithHeaders2(RepeatingFormField):
        ....
        params = ["header"]
        header = lambda: "Its %s" % datetime.now()


Hope this helps,
Alberto

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to