[Zope3-Users] Using zope.formlib for a search form

Hermann Himmelbauer dusty at qwer.tk
Fri Aug 10 02:38:44 EDT 2007


Am Freitag, 10. August 2007 04:14 schrieb Andrew Groom:
> Hi All,
>
> I've been battling with formlib for the last three days now and, while
> I've learnt a lot, it's nearly driven me crazy. I'm trying to use it to
> create simple search form, i.e., a form that does input validation,
> remembers the values you entered, etc., but is not tied to a persisted
> object.
>
> I've got the form rendering and processing requests, but I'm completely
> stuck on getting the form to remember the values I just entered after a
> form post.
>
> Has anyone got any suggestions ? Here's what I've got so far, the latest
> attempt being to use a dummy object to provide the context, which also
> doesn't seem to work:

Yes, I do the same and use a self-made class, called "ActionForm" for that, 
it's quite simple:

class ActionForm(Formframe, form.Form):
    """ Generic form for context-less patterns """

    def updateWidgets(self):
        self.widgets = getMultiAdapter(
            (self, self.request, self.getContent()), IWidgets)
        self.widgets.ignoreContext = True
        self.widgets.update()

Then I have a persisten object, that acts as an entry point to my view, and 
which may provide various methods, e.g.:

class SearchData(persistent.Persistent):
   implements(ISearchData)

   def search_data(self, **kd):
       # Here the data is retrieved, in my case from a relational database
       return data

And then there's the view:

class ViewSerachKunde(ActionForm):
    formErrorsMessage = _('There were some errors.')

    template = ViewPageTemplateFile('pt/searchdata.pt')
   
    fields = field.Fields(IViewSearchData)

    # If data is found, it's stored in a view-object-attribute
    found_data = None

    @button.buttonAndHandler('Search', name='search')
        data, errors = self.extractData()
            if errors:
                self.status = self.formErrorsMessage
                return
            self.found_data = self.context.search_data(**data)

And then in my template file I have something like this (please note that I 
use a custom library for displaying fields (formsnippet):

<--! This displayes the search-widgets -->
<span tal:repeat="widget view/widgets/values">
  <span tal:replace="structure widget/snippets/div" />
</span>

<--! And this displays the found data -->
<div tal:condition="view/found_data">
  <h2>Found Data:</h2>
    <span tal:replace="structure view/found_data"/>
</div>

Best Regards,
Hermann

-- 
x1 at aon.at
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