Skip to main content

An optgroup widget for z3c.form.

Project description

Introduction

This package implements a widget that groups select values into optgroups.

Usage

Imagine you have the following schema:

class IMySchema(Interface):
    country = schema.Choice(
        required=True,
        title=u"Country",
        vocabulary='countries',
    )

    subdivision = schema.Choice(
        required=True,
        title=u"State",
        vocabulary='subdivisions',
    )

    region = schema.Choice(
        required=True,
        title=u"County",
        vocabulary='regions',
    )

When you create your vocabularies (e.g. using SimpleVocabulary), instead of adding SimpleTerm items:

...
for country in pycountry.countries:
    terms.append(SimpleTerm(value=country.alpha2, token=country.alpha2,
                            title=country.name))
...

you add OptgroupTerm items:

from z3c.formwidget.optgroup.widget import OptgroupTerm

...
country_list = countries(context)
for item in pycountry.subdivisions:
    parent = country_list.getTermByToken(item.country_code).title
    terms.append(OptgroupTerm(value=item.code, token=item.code,
                              title=item.name, optgroup=parent))
...

In your form, simply assign the OptgroupFieldWidget:

from z3c.formwidget.optgroup.widget import OptgroupFieldWidget

class MyForm(form.Form):
    fields = field.Fields(IMySchema)

    fields['subdivision'].widgetFactory = OptgroupFieldWidget
    fields['region'].widgetFactory = OptgroupFieldWidget

Contributors

Thomas Massmann, Author

Changelog

1.2 (2012-05-01)

  • Fixed wrong html tag for display mode template. [Thomas Massmann]

1.1 (2012-04-26)

  • Always show no value message as first item. [Thomas Massmann]

1.0.1 (2012-04-14)

  • MANIFEST.in was missing some entries. [Thomas Massmann]

1.0 (2012-04-14)

  • Initial release. [Thomas Massmann]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

z3c.formwidget.optgroup-1.2.tar.gz (13.2 kB view details)

Uploaded Source

File details

Details for the file z3c.formwidget.optgroup-1.2.tar.gz.

File metadata

File hashes

Hashes for z3c.formwidget.optgroup-1.2.tar.gz
Algorithm Hash digest
SHA256 f1de0a23bb87d4aa0661eba563052be399884ea46cdffc7663b62594f146d146
MD5 4e40890d4a1ebf930b2078889a69749c
BLAKE2b-256 214a871e97e3530ed010ac2ddb85dd4087105bff4773cd7bb7ff4243fcf4de74

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page