Permalink
Browse files

Adding form_options option to FormView for arbitrary configuration of…

… the form instance
  • Loading branch information...
1 parent 8befc62 commit be1029f2b40d1a09e0ad6af8579d04f15bb8811c @davidjb committed Aug 17, 2012
Showing with 43 additions and 2 deletions.
  1. +8 −0 docs/index.rst
  2. +7 −1 pyramid_deform/__init__.py
  3. +28 −1 pyramid_deform/tests.py
View
@@ -82,6 +82,7 @@ You can then write a ``PageEditView`` using
class PageEditView(FormView):
schema = PageSchema()
buttons = ('save',)
+ form_options = {'formid': 'pyramid-deform'}
def save_success(self, appstruct):
context = self.request.context
@@ -104,6 +105,13 @@ it's a required field.
We use the ``appstruct`` method to pre-fill the form with values from
the page object that we edit (i.e. ``context``).
+We also provide a ``form_options`` dict -- this structure can contain
+any options to be passed as keyword arguments to the form class' ``__init__``
+method. In the case above, we customise the ID for the form using the
+``formid`` option but could change the ``action``, ``method``,
+and more. For more details, see
+http://deform.readthedocs.org/en/latest/api.html#deform.Form.
+
The ``PageEditView`` is registered like any other Pyramid view. Maybe
like this:
@@ -55,6 +55,11 @@ class FormView(object):
#: Provide your schema in your derived class.
schema = None
+ #: Structure of options to pass as keyword arguments when instantiating
+ #: the form instance of :attr:`form_class`. Any options that can be
+ #: passed to this class' ``__init__`` can be provided here.
+ form_options = {}
+
def __init__(self, request):
self.request = request
@@ -89,7 +94,8 @@ def __call__(self):
ajax_options = getattr(self, 'ajax_options', '{}')
self.schema = self.schema.bind(**self.get_bind_data())
form = self.form_class(self.schema, buttons=self.buttons,
- use_ajax=use_ajax, ajax_options=ajax_options)
+ use_ajax=use_ajax, ajax_options=ajax_options,
+ **self.form_options)
self.before(form)
reqts = form.get_widget_resources()
result = None
View
@@ -10,6 +10,7 @@
import unittest
import shutil
import tempfile
+import types
from mock import patch
from mock import Mock
from pyramid import testing
@@ -120,6 +121,27 @@ def test__call__binds_schema_with_get_bind_data(self):
# note: DummySchema sets kw to the bind data
self.assertEqual(schema.kw, inst.get_bind_data())
+ def test_form_options_applied(self):
+ schema = DummySchema()
+ request = DummyRequest()
+ inst = self._makeOne(request)
+ inst.schema = schema
+ inst.form_class = DummyForm
+ form_options = {'formid': 'custom-id',
+ 'action': 'custom-action',
+ 'method': 'GET',
+ 'arbitrary-option': ''}
+ inst.form_options = form_options
+
+ def check_form(self, form):
+ self.form = form
+ inst.before = types.MethodType(check_form, inst)
+ inst()
+ form = inst.form
+ # All options should end up on the form, overriding any defaults
+ for key, value in form_options.iteritems():
+ self.assertEqual(getattr(form, key), value)
+
class TestFormWizardView(unittest.TestCase):
def _makeOne(self, wizard):
from pyramid_deform import FormWizardView
@@ -610,11 +632,16 @@ def test___getitem___found(self):
self.assertEqual(inst['a'], {})
class DummyForm(object):
- def __init__(self, schema, buttons=None, use_ajax=False, ajax_options=''):
+ def __init__(self, schema, buttons=None, use_ajax=False, ajax_options='',
+ formid='deform', action='', method='POST', **kw):
self.schema = schema
self.buttons = buttons
self.use_ajax = use_ajax
self.ajax_options = ajax_options
+ self.formid = formid
+ self.action = action
+ self.method = method
+ self.__dict__.update(kw)
def get_widget_resources(self):
return {'js':(), 'css':()}

0 comments on commit be1029f

Please sign in to comment.