zeam.form.base
==============

Forms are class, registered like views:

   >>> from zeam.form.base.form import Form

   >>> from zope.publisher.browser import TestRequest
   >>> request = TestRequest()

   >>> class Content(object):
   ...     def __repr__(self):
   ...         return '<%s object>' % self.__class__.__name__
   >>> context = Content()

   >>> form1 = Form(context, request)
   >>> form1
   <zeam.form.base.form.Form object at ...>

They implements IForm:

   >>> from zope.interface.verify import verifyObject
   >>> from zeam.form.base import interfaces
   >>> verifyObject(interfaces.IForm, form1)
   True


Data manager
------------

A form (exactly widgets) can access content data via a data
manager. Data manager will transparently help you to access different
type of content, like dictionary or objects, or directly attributes.


Object data manager
~~~~~~~~~~~~~~~~~~~

   >>> from zeam.form.base.datamanager import ObjectDataManager

   >>> class MyContent(Content):
   ...    title = u'Content'
   ...    value = 42
   >>> mycontent = MyContent()

   >>> manager1 = ObjectDataManager(mycontent)
   >>> manager1
   <ObjectDataManager used for <MyContent object>>

It correctly implements its interface:

   >>> verifyObject(interfaces.IDataManager, manager1)
   True

And you can use it to access content value:

   >>> manager1.get('title')
   u'Content'
   >>> manager1.get('value')
   42

Inexisting content value raises KeyError

   >>> manager1.get('foobar')
   Traceback (most recent call last):
     ...
   KeyError: 'foobar'

You can set values as well:

   >>> manager1.set('ready', True)
   >>> mycontent.ready
   True


Dictionary data manager
~~~~~~~~~~~~~~~~~~~~~~~

There is a data manager which is able to work on dictionaries as well:

   >>> from zeam.form.base.datamanager import DictDataManager

   >>> data = {'title': u'Content', 'value': 42}
   >>> manager2 = DictDataManager(data)
   >>> manager2
   <DictDataManager used for {'value': 42, 'title': u'Content'}>

It correctly implements its interface:

   >>> verifyObject(interfaces.IDataManager, manager2)
   True

And you can use it to access content value:

   >>> manager2.get('title')
   u'Content'
   >>> manager2.get('value')
   42

Inexisting content value raises KeyError

   >>> manager2.get('foobar')
   Traceback (most recent call last):
     ...
   KeyError: 'foobar'

You can set values as well:

   >>> manager2.set('ready', True)
   >>> data.get('ready')
   True


Using a data manager on a form
------------------------------

You can use a data manager on every FormData:

   >>> from zeam.form.base.form import cloneFormData
   >>> formdata1 = cloneFormData(form1)

By default you will get a data manager for the context:

   >>> manager3 = formdata1.getContentData()
   >>> manager3
   <ObjectDataManager used for <Content object>>
   >>> manager3.content is context
   True

However you modify it and give directly a content:

   >>> formdata1.setContentData(mycontent)
   >>> formdata1.getContentData()
   <ObjectDataManager used for <MyContent object>>

Or directly a data manager:

   >>> formdata1.setContentData(manager2)
   >>> formdata1.getContentData() is manager2
   True
