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 hashes)

Uploaded Source

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