Hi Hermann

> Betreff: Re: AW: AW: [Zope3-Users] z3c.form: Data Manager - 
> getting value via dm.query() instead of dm.get()
> > Hi Herman
> >
> >> Betreff: Re: AW: [Zope3-Users] z3c.form: Data Manager - 
> getting value 
> >> via dm.query() instead of dm.get()
> >
> > [...]
> >
> >> But I would nevertheless suggest to implement some kind of 
> "switch" 
> >> that decides if an error is raised or NOVALUE is returned 
> in case a 
> >> value is missing in the underlying object.
> >
> > I don't understand that.
> >
> > Why whould you like to store a default value if a value is missing. 
> > That's just a write access for storing what default returns anyway.
> Hmmm, perhaps I have to explain my case better:
> Normally, z3c.forms are used to edit/display content objects, 
> which is e.g. a "person object" with attributes such as 
> "name, etc.", which are then mapped to fields/widgets. In 
> case the underlying data object is a dictionary, this 
> dictionary has to provide all key/value pairs as specified in 
> the field.fields().
> In my case, I'm creating a search form. This search form does 
> not store any data into a content object, it just calls a 
> specific search function.
> However, the values in the search form should be remembered, 
> so that if the user comes back to the form, it is already 
> prefilled. Therefore, I simply store the "data" dictionary of 
> the button handler into the session and (mis)use this object 
> as content object:
> def getContent():
>     return session.get('searchformkey', {})
> def handle_search(self, action):
>     data, errors = self.extractData()
>     if errors:
>         self.searched = None
>         self.status = self.formErrorsMessage
>         return
>     self.do_search(data)
>     self.session['searchformkey'] = data
> So, it may happen, that not all fields are filled out, 
> therefore key/value pairs may be missing. If that happens, 
> the widget routine throws an error.
> If I manage to overcome this error (e.g. by replacing the 
> get() through
> query() routine in z3c.form.widget), my form works.
> Maybe, there's a better way to accomplish the above, but I 
> could not think of one...

I understand. Ther must be something wrong.
Take a look at the AddForm implementation this one uses
ignoreContext = True. This forces to not store the values
to the object. Because it doesn't use applyChanges like
the EditForm does.

For a SearchForm, you can use extractData offered from
the form and do what every you need to do with the extracted 
form values. e.g. store them into a session. You can setup 
the widgets with this values later.

Or even better you can define a custom ISession object
which provides what you need. e.g. with useful default
values from the schema field definition. Such a custom 
session object can be used within the EditForm and 
you don't have to use the AddForm concept.

Such a search session object could look like:

class ISearchSession(ISession):
    """Message filter session."""

    searchText = zope.schema.TextLine(
        title=Search text',
        description=u'The search text',

class SearchSessioon(Session):
    _spd = None

    def spd(self):
        return self.__getitem__('SEARCH_SESSION_KEY')
    def searchText():
        def get(self):
            return self.spd.get('searchText', u'')
        def set(self, searchText):
            self.spd['searchText'] = searchText
        return property(get, set)

I allways use such a session implementation because it 
abstracts what you need, uses schema fields, offers 
built in defaults and works well with z3c.form.

Note; I'm still not 100% sure if the get() or query()
usage in applyChanges is right or wrong. I hope to
find time to deep into that in the near future.

But I think nobody will understand the concept if we
implement a form property for use get or query in 

Hope the session concept works for you till someone 
agrees/checks the get/query useage.

Roger Ineichen

> Best Regards,
> Hermann
> --
> GPG key ID: 299893C7 (on keyservers)
> FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7

Zope3-users mailing list

Reply via email to