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.

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

Ciao
Michele

Ed Singleton wrote:
> On 26/04/06, Michele Cella <[EMAIL PROTECTED]> wrote:
> > Hi Ed,
> >
> > You've a pretty specific need here.
> >
> > If you want to use a Widget I suggest you to encapsulate all things
> > (fields and layout that in this case is fields dependent) inside a
> > widget ready to use.
> >
> > Example:
> >
> > class MyFields(WidgetsList):
> >      name = TextField()
> >      creditcard = TextField()
> >
> > class MyForm(TableForm):
> >     fields = MyFields()
> >     template = """
> >     <form xmlns:py="http://purl.org/kid/ns#";
> >         name="${name}"
> >         action="${action}"
> >         method="${method}"
> >         class="tableform"
> >         py:attrs="form_attrs"
> >     >
> >         <table py:attrs="table_attrs">
> >         <tr>
> >              <td colspan="2"><h1>Personal Details</h1></td>
> >          </tr>
> >          <tr>
> >              <td><label for="name">Name</label></td>
> >              <td>
> >                   <div py:content="display_field_for('name')" />
> >                   <div py:content="error_for('name')" />
> >              </td>
> >          </tr>
> >          <tr>
> >              <td colspan="2"><h1>Bank Details</h1></td>
> >          </tr>
> >          <tr>
> >              <td><label for="creditcard">Credit Card</label></td>
> >              <td>
> >                   <div py:content="display_field_for('creditcard')" />
> >                   <div py:content="error_for('creditcard')" />
> >              </td>
> >          </tr>
> >              <tr>
> >                 <td>&#160;</td>
> >                 <td py:content="submit.display(submit_text)" />
> >             </tr>
> >         </table>
> >     </form>
> >     """
> >
> > to keep in mind, this is only a basic example, since you're defining
> > fields content you just instantiate the Form and use it, you don't need
> > to pass the fields parameter in this case.
> >
> > Also since you know in advance fields name you're going to use you
> > define labels by yourself and don't need to loop every field by just
> > use display_field_for(<field_name_here>) also display field for inside
> > a widget template already takes care of passing additional arguments
> > like value and params to the field.
>
> The problem with this is that I have to write a template everytime I
> need a new form (and I have lots of forms) so I might as well create a
> general one that I can pop headings into.
>
> I've got this far:
>
> class Header(FormField):
>     "A simple header for a form."
>
>     template = """
>     <h1 xmlns:py="http://purl.org/kid/ns#";
>         class="${field_class}"
>         py:content="heading"
>     >Heading
>     </h1>
>     """
>     params = ["attrs", "heading"]
>     attrs = {}
>
> class TableForm(Form):
>     template = """
>     <form xmlns:py="http://purl.org/kid/ns#";
>         name="${name}"
>         action="${action}"
>         method="${method}"
>         class="tableform"
>         py:attrs="form_attrs"
>     >
>         <div py:for="field in hidden_fields"
>             py:replace="field.display(value_for(field), **params_for(field))"
>         />
>         <table>
>             <tr py:for="i, field in enumerate(fields)"
>                 class="${i%2 and 'odd' or 'even'}"
>             >
>                 <td py:if="field.header" py:attrs="colspan:2">
>                     <span py:replace="field.display(**params_for(field))" />
>                 </td>
>                 <td py:if="not field.header" py:attrs="colspan:2">
>                     <label class="fieldlabel" for="${field.field_id}"
> py:content="field.label" />
>                 </td>
>                 <td py:if="not field.header" py:attrs="colspan:2">
>                     <span py:replace="field.display(value_for(field),
> **params_for(field))" />
>                     <span py:if="error_for(field)" class="fielderror"
> py:content="error_for(field)" />
>                     <span py:if="field.help_text" class="fieldhelp"
> py:content="field.help_text" />
>                 </td>
>             </tr>
>             <tr>
>                 <td>&#160;</td>
>                 <td py:content="submit.display(submit_text)" />
>             </tr>
>         </table>
>     </form>
>     """
>     params = ["table_attrs"]
>     table_attrs = {"border": 0,
>                    "cellspacing": 0,
>                    "cellpadding": 2}
>
> But I can't quite get my head around the whole thing.  Once I've
> worked out how to do it once I should be fine, but that first time
> seems quite cryptic (I've read through all the widget source code I
> can find).
> 
> Any clues on widget creation gratefully received.
> 
> Ed


--~--~---------~--~----~------------~-------~--~----~
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