Hi Max, Actually I can't look more deeply into your code anyway I can tell you why _is_option_selected works in this way and why it should work in this way.
Let's say you have a MultipleSelectField like this: MultipleSelectField(name="languages", options=[(1, "Python"), (2, "Java"), (3, "Ruby")]) as you can see we are using int as values, hence the guessed validator is Int(). Ok, now you submit a form that contains such a field with some values selected, the form has an error hence it's redisplayed and the field grabs the value previously selected by the user (we want this behavior) from the request, since those values are coming from the web they always are strings, hence we have something like this for instance: languages = ['1', '3'] if _is_options_selected doesn't coerce those values to be coherent with the values we expect from this field (that as I said are Int(s)) they will never match and hence we will redisplay the field with nothing selected while the user actually selected something. I hope this helps you understand the logic behind that, your from_python method should return a string (this is just what you need in the web world) while to_python should return the same type you're passing as options ids. Ciao Michele PS Is there a good reason you reverted patch #785 here: http://trac.turbogears.org/turbogears/changeset/1340 when you already committed it to both branch in: http://trac.turbogears.org/turbogears/changeset/1242 http://trac.turbogears.org/turbogears/changeset/1243 ??? :D Max Ischenko wrote: > Hi, > > I have a TableForm widget which uses MultipleSelectField. > > After recent updates I discovered that it no longer displays currently > selected options properly. It used to work OK so I'm not sure whether the > error is in my code or there is (introduced) bug in widgets code. > > The problem, as I see it, is that _is_option_selected compares "raw" input > values with "converted" options values and those never match. > > E.g.: data['readers'] are user ids; options list contains user ids as well. > But then _is_option_selected converts (with .to_python) data['readers'] into > proper User instances and compares them with ids from the options list > > Here is my code, hopefully someone can help me to figure out what's wrong. > > form = widgets.TableForm('manage', fields=[ > ... > widgets.MultipleSelectField('readers', label='Readers', > validator=ModelReferenceValidator(User, if_empty=0), > options=list_regular_users), > ]) > > # options value list > def list_regular_users(): > users = [...] > options = [(u.id, str(u)) for u in users] > return options > > # controller excerpt: > data['readers'] = [r.grantee_user.id for r in rules if (...) ] > > # my validator: > > class ModelReferenceValidator(validators.FancyValidator): > def __init__(self, modelClass, *args, **kw): > validators.FancyValidator.__init__(self, *args, **kw) > self.modelClass = modelClass > def _to_python(self, value, state): > if isinstance(value, self.modelClass): > return value > try: > pk = long(value) > return self.modelClass.get(pk) > except (ValueError, TypeError): > raise validators.Invalid, \ > (self.message('integer', state), value, state) > except SQLObjectNotFound: > typename = self.modelClass.__name__ > raise validators.Invalid, \ > (self.message('invalid', state, type=typename), value, > state) > _from_python = _to_python --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "TurboGears Trunk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/turbogears-trunk -~----------~----~----~----~------~----~------~--~---
