[Zope3-Users] z3c.forms: Datamanger that combines values of multiple widgets

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Jun 19 19:27:25 EDT 2007


On Tuesday 12 June 2007 11:45, Hermann Himmelbauer wrote:
> I've read the doctests of z3c.forms and have to say that the package is
> really impressive. Many things look a lot cleaner and more customizeable
> than formlib. Thanks!
>
> However, what I don't know is how to read/write values of multiple widgets
> into one context attribute. A common example of this would be a date, which
> is represented by three TextWidgets (rendered as input fields) that hold
> the year, month and day, which is stored into one context attribute of type
> datetime.
>
> What would be the best way to accomplish that?

This is the easy case. You should write a special widget that simply renders 
three HTML input tags.

> The reverse seems to be possible, e.g. a datamanger that splits a widget
> value into chunks, which are then stored into seperate context attributes,
> e.g. via a datamanger like "context.user, context.host =
> 'asdf at email.com'.split('@')".

This is actually the tricky case. The datamanager will not work here, because 
it is registered for one field-widget pair. I just discussed this with Roger 
a bit, and I agree with his opinion that one widget can only write to one 
field. So the solution lies in your content component:

class IPerson(object):

  user = ''
  host = ''

  @apply
  def email():
    def get(self):
      return self.user + '@' + self.host
    def set(self, value):
      self.user, self.host = value.split('@')
    return property(get, set)

Regards,
Stephan
-- 
Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training


More information about the Zope3-users mailing list