Skip to main content

Plone wtforms integration

Project description

Introduction

collective.wtforms is plone integration with the WTForms form library.

Basic Complete Form Example

The basic form is constructed like so:

from wtforms import Form, TextField
from wtforms import validators
class MyForm(Form):
    one = TextField("Field One", [validators.required()])
    two = TextField("Field Two")
    three = TextField("Field Three")

from collective.wtforms.views import WTFormView
class MyFormView(WTFormView):
    formClass = MyForm
    buttons = ('Create', 'Cancel')

    def submit(self, button):
        if button == 'Create' and self.validate():
            # do fun stuff here
            self.context.value = self.form.one.data

Then wire up the form with zcml:

<browser:page
    name="my-form"
    for="*"
    class=".pathto.MyFormView"
    permission="zope2.View"
/>

You can also override the template used and then do:

<form tal:replace="structure view/renderForm" />

To render the form anywhere in your view.

Fieldsets

You can specific which fields go with which fieldsets easily also:

class MyFormView(WTFormView):
    formClass = MyForm
    buttons = ('Create', 'Cancel')
    fieldsets = (
        ('Fieldset One', ('one', 'two')),
        ('Fieldset Two', ('three',))
    )

    def submit(self, button):
        if button == 'Create' and self.validate():
            # do fun stuff here
            self.context.value = self.form.one.data

This will render the form in the standard plone feildsets.

Control Panel

It’s also possible to easily construct a control panel form:

from collective.wtforms.views import WTFormControlPanelView
class MyFormView(WTFormControlPanelView):
    formClass = MyForm
    buttons = ('Save', 'Cancel')

    def submit(self, button):
        if button == 'Save' and self.validate():
            # do fun stuff here
            self.context.value = self.form.one.data

WTFormView Class Properties

formClass

The WTForm class used. Required

label

The form label that shows up in the h1 tag

description

form description

prefix

The prefix of the form input name values. Defaults to wtform

buttonPrefix

The prefix of the button names. Defaults to form.actions.

wrapWithFieldset

A boolean that decides if it should wrap the fields in a fieldset. Defaults to True

csrfProtect

Protect the form against csrf attacks. Defaults to True

buttons

An iterable of button names. Defaults to (‘Save’, ‘Cancel’)

fieldsets

An iterable of (fieldset name, (field names, …)). Defaults to empty

form

The instance of the WTForm form that is created on rendering the form.

data

Override this property to specify default values to populate the form with. This must return a dictionary.

submitted

Boolean if the form was submitted or not.

renderField(field)

Method to render a specific field.

renderForm()

Method to render the entire form.

validate()

Check csrf protection and validates the form.

submit(button)

The method you must override to handle the form sumission and run validation. Return a value to override rendering the template(self.index()). For instance, if you do a redirect, there is no need to render the page also:

def submit(self, button):
    if button == 'Save' and self.validate():
        self.context.value = self.form.one.data
        self.request.response.redirect(self.context.absolute_url())
        return 1
mungeForm(form)

A method to mess with the form after it is created. This is what can be used to apply dynamic choices.

Changelog

1.0a6 (2012-11-26)

  • handle spaces in button names [vangheem]

1.0a5 (2012-11-26)

  • handle unicode data [vangheem]

  • do not use and display dummy default form title and description

  • moved the field description after label, where commonly is

  • added locales directory, for being able to translate basic default buttons

  • provided italian translation

  • enabled the Plone form tabbing feature

1.0a4 (2012-08-29)

  • provide some bootstrap class hints

1.0a3 (2011-10-11)

  • fix issue where initial form data wouldn’t work in some instances.

1.0a2 (2011-10-06)

  • Fixed control panel template used.

  • add ability to easily mess with form after creation

  • add ability to provide initial data to form values

1.0a1 (2011-09-29)

  • Initial release

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

collective.wtforms-1.0a6.zip (30.7 kB view details)

Uploaded Source

File details

Details for the file collective.wtforms-1.0a6.zip.

File metadata

File hashes

Hashes for collective.wtforms-1.0a6.zip
Algorithm Hash digest
SHA256 8c6caa66541d2d9b88afedcd691100cc5e106122d8de239ef0726c6a34b446d2
MD5 a8e7bb4890450e8c71d2c8b414ff0217
BLAKE2b-256 b7d8e4b4cebb36c58cb2dcf4f34c41aeda25d93ec37a95fb512ae7a204859343

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