[Product-Developers] portlet custom validation

msmith64 at naz.edu msmith64 at naz.edu
Wed Jan 13 23:30:16 UTC 2010


I'm working on a product that includes several custom portlets, and for most of them validation is trivial: Does it have a value, or not? Today, however, I needed to customize validation for a portlet based on whether or not a value was a valid URL to a valid RSS 1, 2 or ATOM feed. That turned out to be surprisingly difficult, and I wonder if I've stumbled upon a bug.

I followed this documentation, because the portlet uses formlib: http://plone.org/documentation/manual/developer-manual/forms/using-zope.formlib/adding-validation

# I created an exception class to raise the appropriate error:
class InvalidFeed(ValidationError):
  "Invalid Feed"

# ... a validation function to test the actual case
# (This version is simplified)
def validateFeed(value):
  try:
    assert value, value
  except AssertionError:
    raise InvalidFeed(value)
  return True

# ... and the portlet Interface 
class IBlogsPortlet(rss.IRSSPortlet):
  url = TextLine(
    title=u'URL',
    description=u'URL of the feed',
    required=True,
    constraint=validateFeed,
  )
  ...

What's unusual about this is that function validateFeed(value=u'') gets called when bin/instance fg is run, and of course it throws an error because value=u'' is not valid, which makes it impossible to start Zope/Plone. Why would that be?

Best wishes,
Michael A. Smith
Web & Digital / Academic Technologies Manager
Nazareth College




More information about the Product-Developers mailing list