Package: collective.wtforms
Introductioncollective.wtforms is plone integration with theWTFormsform library.Basic Complete Form ExampleThe 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.dataThen 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.FieldsetsYou 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.dataThis will render the form in the standard plone feildsets.Control PanelIt’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.dataWTFormView Class PropertiesformClassThe WTForm class used.RequiredlabelThe form label that shows up in the h1 tagdescriptionform descriptionprefixThe prefix of the form input name values. Defaults towtformbuttonPrefixThe prefix of the button names. Defaults toform.actions.wrapWithFieldsetA boolean that decides if it should wrap the fields in a fieldset. Defaults to TruecsrfProtectProtect the form against csrf attacks. Defaults to TruebuttonsAn iterable of button names. Defaults to (‘Save’, ‘Cancel’)fieldsetsAn iterable of (fieldset name, (field names, …)). Defaults to emptyformThe instance of the WTForm form that is created on rendering the form.dataOverride this property to specify default values to populate the form with. This must return a dictionary.submittedBoolean 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 1mungeForm(form)A method to mess with the form after it is created. This is what can be used to apply dynamic choices.Changelog1.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 descriptionmoved the field description after label, where commonly isadded locales directory, for being able to translate basic default buttonsprovided italian translationenabled the Plone form tabbing feature1.0a4 (2012-08-29)provide some bootstrap class hints1.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 creationadd ability to provide initial data to form values1.0a1 (2011-09-29)Initial release