[Zope3-Users] Implementing a DropdownWidget for a country-code vocabulary

Jesper Petersen instantfoo at gmail.com
Sat Sep 15 12:08:19 EDT 2007


Hello!I'm trying to implement a DropdownWidget for my country list. In my
app i'd like to have a dropdown menu where a user can choose a country (for
my main content object, a job). My vocabulary is created from a list of
strings, and stored in a local utility:


SimpleVocabulary.fromValues( [u'AD', u'AE', 'AF', ...] )


But in my dropdown widget i want to display the country name for each option
too.. and not just the same value on both the value attribute and inside the
element:

<option value="AD">Andorra</option>

So i send in a different vocabulary.. one that looks like this:

[(u'AD', u'Andorra'),...]

So i coded this:

---------------------------------
 class CountryDropDown(DropdownWidget):

     def __init__(self, field, request):
         #This returns a dict: {'countrycode':'countryname'}
         territories = request.locale.displayNames.territories

         # Invert the territories dict: keys=names, values=ccodes
         self.territories = sorted(territories.items(), key=itemgetter(1))

         #not using this any longer..
         #voc = getUtility(IVocabularyFactory, u'Country codes')

         voc = SimpleVocabulary.fromItems(self.territories)
         super(CountryDropDown, self).__init__(field, voc, request)

     def textForValue(self, term):
         return term.value
---------------------------------

But it seems like when i choose a country, e.g Andorra, the constraint
fails. I think it is because my modified vocabulary that i sent in which it
is now checking constraints against fails.

I want the constraints to be checked in my ordinary vocabulary but i want to
use a different one when populating the dropdown.. any ideas?


----------
Instant Foo - Naturally Flavoured
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope3-users/attachments/20070915/38db7775/attachment.htm


More information about the Zope3-users mailing list