Input parameter processing
==========================

z3c.form expects the inputs from the request to be decoded from byte strings
to unicode strings. This package ensures that this happens in Zope 2.

First, we'll create a request.

    >>> from zope.interface import alsoProvides
    >>> from zope.publisher.browser import TestRequest
    >>> from zope.annotation.interfaces import IAttributeAnnotatable
    >>> from z3c.form.interfaces import IFormLayer

    >>> def make_request(form={}):
    ...     request = TestRequest()
    ...     request.form.update(form)
    ...     alsoProvides(request, IFormLayer)
    ...     alsoProvides(request, IAttributeAnnotatable)
    ...     return request

Then we create and register a simple form that expects unicode text input:

    >>> from zope import interface, schema
    >>> from z3c.form import form, field, button
    >>> from plone.z3cform.layout import FormWrapper

    >>> class MySchema(interface.Interface):
    ...     text = schema.TextLine(title=u"Text")

    >>> from z3c.form.interfaces import IFieldsForm
    >>> from zope.interface import implements

    >>> class MyForm(form.Form):
    ...     implements(IFieldsForm)
    ...     fields = field.Fields(MySchema)
    ...     ignoreContext = True # don't use context to get widget data
    ...
    ...     @button.buttonAndHandler(u'Apply')
    ...     def handleApply(self, action):
    ...         data, errors = self.extractData()
    ...         print repr(data['text'])

    >>> from zope.component import provideAdapter
    >>> from zope.publisher.interfaces.browser import IBrowserRequest
    >>> from zope.interface import Interface

    >>> provideAdapter(adapts=(Interface, IBrowserRequest),
    ...                provides=Interface,
    ...                factory=MyForm,
    ...                name=u"test-form")

For our context, we define a class that inherits from Acquisition.

    >>> from Acquisition import Implicit
    >>> class Bar(Implicit):
    ...     __allow_access_to_unprotected_subobjects__ = 1
    ...     implements(Interface)

Let's now look this up with some form data that is not encoded:

    >>> from zope.interface import Interface, implements
    >>> from zope.component import getMultiAdapter
    
    >>> context = Bar()
    >>> request = make_request(form={'form.widgets.text': 'foo'})

    >>> testForm = getMultiAdapter((context, request), name=u"test-form")
    >>> testForm.update()
    >>> data, errors = testForm.extractData()
    >>> data
    {'text': u'foo'}
    >>> errors
    ()

Let's now try with some encoded data. The default encoding is utf-8.

    >>> context = Bar()
    >>> request = make_request(form={'form.widgets.text': 'fo\xc3\xb8'})

    >>> testForm = getMultiAdapter((context, request), name=u"test-form")
    >>> testForm.update()
    >>> data, errors = testForm.extractData()
    >>> data
    {'text': u'fo\xf8'}
    >>> errors
    ()

