[Zope3-Users] z3c.form - how to set single default values from another object?

Hermann Himmelbauer dusty at qwer.tk
Wed Feb 20 07:10:57 EST 2008

Hi, I have here an add form which is based on an interface like this:

class IMyObj(Interface):
  value1 = TextLine()
  value2 = TextLine()
In my add form, I need to display a default value for "value2". This default 
value is stored in another object, however, no default value should be given 
for value1.

I'm desperatingly trying to implement this via z3c.form through various ways, 
but none seems to work. I had a look at "widget.py", line 73 (the update 
method where the value is retrieved) and found the following:

1.1) From Request: I could solve my problem like the following in my update() 

self.request.form['form.widgets.value2'] = 'mydefault'

However, I don't think it's a nice solution to modify the request for a 
default value.

1.2.1) Via a datamanger: Seems to be not possible as I can't explicitly 
set "ignoreContext = False" for a single widget. Therefore this fails as I 
don't have a datamanager for value1. Moreover it seems the datamanger is not 
a named adapter:

value = zope.component.getMultiAdapter(
                    (self.context, self.field), interfaces.IDataManager).get()

Therefore in my case, where value1/value2 are both TextLines and have the same 
widget, it's not possible to register the datamanager for value2 only.

1.2.2) Via the field default value: Maybe possible by cloning the field in the 
update() method of my form and setting a default value there, however, this 
looks lie a _very_ bad style to me.

1.3) From an object attribute (don't really understand what this Step is for): 
Seems also not possible as there's once again no way to register a named 
adapter here and therefore the absence of "value1" as object attribute will 
lead to an error:

adapter = zope.component.queryMultiAdapter(
                (self.context, self.request, self.form, self.field, self),
                interfaces.IValue, name='default')

So, either I missed somehow a good solution, or my pattern is not addressed in 
z3c.form. In this case, I'd suggest to handle this case in two ways:

1) Add e.g. Step 1.2.3, which queries after a named adapter where the name is 
the field name. This way, I'd be able to register an adapter that retrieves 
the default value from somewhere else.

2) Provide a way to tell single widgets (and not only the widget manager) to 
ignore/not ignore the context. This way, I could simply use the getContent() 
method in my view, although there's once again the same problem if the 
default values don't come from a single object (as my getContent routine 
cannot decide which object to return).

I don't know if these two solutions would conflict with other parts of the 
framework, maybe there's an even better solution?

Best Regards,

hermann at qwer.tk
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7

More information about the Zope3-users mailing list