|
14 | 14 | ```{todo} |
15 | 15 | Describe how create forms with Plone's default form framework z3c.form. |
16 | 16 | Fields, Widgets, Vocabularies aso are descripted in detail in there own chapter and will be referenced frome examples here. |
17 | | -``` |
| 17 | +``` |
| 18 | + |
| 19 | +Plone uses the [z3c.form](http://pythonhosted.org/z3c.form) library to build its forms, via the [plone.z3cform](http://pypi.python.org/pypi/plone.z3cform) integration package. |
| 20 | + |
| 21 | +For an easy way to structure a form and define its widgets and fields, Plone relies on [plone.autoform](http://pypi.python.org/pypi/plone.autoform), in particular its AutoExtensibleForm base class. |
| 22 | +Which is responsible for processing form hints and setting up [z3c.form](http://pythonhosted.org/z3c.form) widgets and groups (fieldsets). |
| 23 | + |
| 24 | +A form, therefore, is simply a view that uses these libraries, although [plone.autoform](http://pypi.python.org/pypi/plone.autoform) provides some helpful base classes that make it easier to construct forms based on the form schema or {ref}`Dexterity behaviors <backend-behaviors-label>`. |
| 25 | + |
| 26 | + |
| 27 | +(classic-ui-forms-general-forms-label)= |
| 28 | +## General forms |
| 29 | + |
| 30 | +The plonecli provides you with an option to add a form to your Plone package. |
| 31 | + |
| 32 | + |
| 33 | +```shell |
| 34 | +cd collective.awesomeaddon |
| 35 | +plonecli add form |
| 36 | +``` |
| 37 | + |
| 38 | +After using the `plonecli` to add a form, we should have a new sub folder `forms` in our package. |
| 39 | +Here we will find a `configure.zcml` containing the registration of the form, |
| 40 | + |
| 41 | +```xml |
| 42 | +<browser:page |
| 43 | + name="my-form" |
| 44 | + for="*" |
| 45 | + class=".my_form.MyForm" |
| 46 | + permission="cmf.ManagePortal" |
| 47 | + layer="p6.theme5.interfaces.IP6Theme5Layer" |
| 48 | + /> |
| 49 | +``` |
| 50 | + |
| 51 | +and a Python file with the code. |
| 52 | + |
| 53 | +```python |
| 54 | +from plone import schema |
| 55 | +from plone.autoform.form import AutoExtensibleForm |
| 56 | +from z3c.form import button, form |
| 57 | +from zope.interface import Interface |
| 58 | + |
| 59 | + |
| 60 | +class IMyForm(Interface): |
| 61 | + """ Schema Interface for IMyForm |
| 62 | + Define your form fields here. |
| 63 | + """ |
| 64 | + name = schema.TextLine( |
| 65 | + title=u"Your name", |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +class MyForm(AutoExtensibleForm, form.EditForm): |
| 70 | + schema = IMyForm |
| 71 | + ignoreContext = True |
| 72 | + |
| 73 | + label = u"What's your name?" |
| 74 | + description = u"Simple, sample form" |
| 75 | + |
| 76 | + @button.buttonAndHandler(u'Ok') |
| 77 | + def handleApply(self, action): |
| 78 | + data, errors = self.extractData() |
| 79 | + if errors: |
| 80 | + self.status = self.formErrorsMessage |
| 81 | + return |
| 82 | + |
| 83 | + # Do something with valid data here |
| 84 | + |
| 85 | + changes = self.applyChanges(data) |
| 86 | + # Set status on this form page |
| 87 | + # (this status message is not bind to the session and does not go thru redirects) |
| 88 | + if changes: |
| 89 | + self.status = "Settings saved" |
| 90 | + |
| 91 | + @button.buttonAndHandler(u"Cancel") |
| 92 | + def handleCancel(self, action): |
| 93 | + """User canceled. Redirect back to the front page. |
| 94 | + """ |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +(classic-ui-forms-dexterity-add-edit-forms-label)= |
| 100 | +## Dexterity Add- and Edit forms |
| 101 | + |
| 102 | +Dexterity content types are coming with default add and edit forms. |
| 103 | +You can build custom add and edit forms if you need. |
0 commit comments