Hello!<div>I&#39;m trying to implement a DropdownWidget for my country list.&nbsp;In my app i&#39;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:
</div><div><br>&nbsp;</div><div>SimpleVocabulary.fromValues( [u&#39;AD&#39;, u&#39;AE&#39;, &#39;AF&#39;, ...] )</div><div><br>&nbsp;</div><div>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:
</div><div><br class="khtml-block-placeholder"></div><div>&lt;option value=&quot;AD&quot;&gt;Andorra&lt;/option&gt;</div><div><br class="khtml-block-placeholder"></div><div>So i send in a different vocabulary.. one that looks like this:
</div><div><br class="khtml-block-placeholder"></div><div>[(u&#39;AD&#39;, u&#39;Andorra&#39;),...]</div><div><br class="khtml-block-placeholder"></div><div>So i coded this:</div><div><br class="khtml-block-placeholder"></div>
<div>---------------------------------</div><div>&nbsp;class CountryDropDown(DropdownWidget):</div><div><br class="khtml-block-placeholder"></div><div>&nbsp;&nbsp; &nbsp; def __init__(self, field, request):</div><div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#This returns a dict: {&#39;countrycode&#39;:&#39;countryname&#39;}
</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; territories = request.locale.displayNames.territories</div><div>&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # Invert the territories dict: keys=names, values=ccodes</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.territories = sorted(territories.items
(), key=itemgetter(1))</div><div><br class="khtml-block-placeholder"></div><div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#not using this any longer..</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; #voc = getUtility(IVocabularyFactory, u&#39;Country codes&#39;)</div><div>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; voc = SimpleVocabulary.fromItems(self.territories)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; super(CountryDropDown, self).__init__(field, voc, request)</div><div>&nbsp;</div><div>&nbsp;&nbsp; &nbsp; def textForValue(self, term):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 
term.value</div><div style="">---------------------------------</div><div style="font-family: arial; font-size: 13px; "><br class="khtml-block-placeholder"></div><div style="font-family: arial; font-size: 13px; ">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.&nbsp;</div><div style="font-family: arial; font-size: 13px; "><br class="khtml-block-placeholder">
</div><div style="font-family: arial; font-size: 13px; ">I want the constraints to be checked in my ordinary vocabulary but i want to use a different one when populating the dropdown..&nbsp;any ideas?</div><div style="font-family: arial; font-size: 13px; ">
<br class="khtml-block-placeholder"></div><div style="font-family: arial; font-size: 13px; "><br class="khtml-block-placeholder"></div><div style="font-family: arial; font-size: 13px; ">----------</div><div style="font-family: arial; font-size: 13px; ">
Instant Foo - Naturally Flavoured</div>