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 implementer

    >>> @implementer(IFieldsForm)
    ... class MyForm(form.Form):
    ...     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
    >>> @implementer(Interface)
    ... class Bar(Implicit):
    ...     __allow_access_to_unprotected_subobjects__ = 1

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

    >>> from zope.interface import Interface
    >>> 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': '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': b'fo\xc3\xb8'})

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

