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
-~----------~----~----~----~------~----~------~--~---