Hello!<div>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:
</div><div><br> </div><div>SimpleVocabulary.fromValues( [u'AD', u'AE', 'AF', ...] )</div><div><br> </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><option value="AD">Andorra</option></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'AD', u'Andorra'),...]</div><div><br class="khtml-block-placeholder"></div><div>So i coded this:</div><div><br class="khtml-block-placeholder"></div>
<div>---------------------------------</div><div> class CountryDropDown(DropdownWidget):</div><div><br class="khtml-block-placeholder"></div><div> def __init__(self, field, request):</div><div> #This returns a dict: {'countrycode':'countryname'}
</div><div> territories = request.locale.displayNames.territories</div><div> </div><div> # Invert the territories dict: keys=names, values=ccodes</div><div> self.territories = sorted(territories.items
(), key=itemgetter(1))</div><div><br class="khtml-block-placeholder"></div><div> #not using this any longer..</div><div> #voc = getUtility(IVocabularyFactory, u'Country codes')</div><div> </div>
<div> voc = SimpleVocabulary.fromItems(self.territories)</div><div> super(CountryDropDown, self).__init__(field, voc, request)</div><div> </div><div> def textForValue(self, term):</div><div> 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. </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.. 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>