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

Andrew Groom andrew at reurbanise.co.nz
Thu Aug 9 22:14:48 EDT 2007


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:

#--------------------------------------------------------------------------------------
# Classes for bulding the search form
#--------------------------------------------------------------------------------------
class UserSearchForm (Form):

     # Vocab for boolean items
     bool_vocab = SimpleVocabulary.fromItems([('Any', ''), ('Yes', 
'true'), ('No', 'false')])

     # Form fields
     form_fields = Fields(Choice( __name__='is_verified',
                            vocabulary=bool_vocab,
                           title=u"Is verified?",
                           description=u"Has the users email address 
been verified ?"),
                    Choice( __name__='is_opted_in',
                            vocabulary=bool_vocab,
                           title=u"Has opted in for emails",
                           description=u"Has the user opted in for 
emails ?"),
                    Choice( __name__='is_old_extension_user',
                            vocabulary=bool_vocab,
                           title=u"Is not using the latest extension",
                               description=u"Is the user using the 
latest extension ?"),
                    Choice( __name__='is_extension_user',
                            vocabulary=bool_vocab,
                           title=u"Is an extension user",
                               description=u"Is the user using the 
extension ?"),
                    )

     # Vocab for email templates
     templates = EmailTemplates()
     template_vocab = SimpleVocabulary.fromItems(templates.get_tuples())

     form_fields += Fields(Choice( __name__='template',
                            vocabulary=template_vocab,
                           title=u"Email template?",
                           readonly=True,
                           description=u"Choose an email template ?" ),
                    )

     prefix = 'search'
     label = 'Search for Users'
     template = ViewPageTemplateFile('templates/searchform.pt')

     actions = form.Actions(
                            form.Action('search', success='process_search'),
                            form.Action('process', 
success='process_action'),
                            )

     _results = None


#--------------------------------------------------------------------------------------
# Dummy search form context object
#--------------------------------------------------------------------------------------
class UserSearchFormData:

     def __init__ (self):
         self.is_old_extension_user = True
         self.is_extension_user = False
         self.is_verified = True
         self.is_opted_in = False


#--------------------------------------------------------------------------------------
# Classes for registering the search form as a content provider
#--------------------------------------------------------------------------------------
class UserSearchFormProvider:
     implements(IContentProvider)
     adapts(Interface, IDefaultBrowserLayer, Interface)

     def __init__(self, context, request, view):
         self.context = context
         self.request = request
         self.__parent__ = view
         self.obj = UserSearchFormData()
         self.form = UserSearchForm(self.obj, self.request)

     def update(self):
         pass

     def render(self):
         import pdb; pdb.set_trace()
         return self.form()

#--------------------------------------------------------------------------------------
# User admin view
#--------------------------------------------------------------------------------------
class UserAdminView (BrowserView):

     def __init__ (self, context, request):
         self.context = context
         self.request = request

         # Set up the form content provider
         provideAdapter(UserSearchFormProvider, 
name="vortexuser.UserSearchFormProvider")


Many thanks for any help.

Cheers, Andrew.
-----------------------------------
Reurbanise - Urban Sustainability
ph: (03) 3528 055, mobile: 0274 992 569
http://www.reurbanise.co.nz


More information about the Zope3-users mailing list