Hey everyone,

I've been using a validator to coerce results to and from python
objects, to try and avoid having to write (object.id, object.field)
tuple generators.

The following code appears to work, in that widgets fill with the
correct values, the show the previously selected values, and they
return object lists which I can assign directly back to the original
object:

class CoerceOptionsFrom(FancyValidator):
    def __init__(self, entity, field='name'):
        self.entity = entity
        self.field = field
    def _from_python(self, value, state):
        return getattr(value, self.field, 0)
    def _to_python(self, value, state):
        params = { self.field: value }
        return self.entity.filter_by(**params).scalar()

class TaskForm(WidgetsList):
    staff = CheckBoxList(
        label='Assign staff',
        options=Team.all_workers(),
        validator=ForEach(
            CoerceOptionsFrom(Staff.q.join('contact_details')),
            convertToList=True,
            ),
        convert=True,
        )

However, new workers get assigned to teams several times a day, so I
need the options to be dynamic. Trying to treat Team.all_workers as a
callback produces the correct options in the CheckBoxList, but doesn't
recognise any pre-assigned values as matches. Tracing the validator,
it's called and responds exactly the same way whether 'options' is set
to the callback or the list.

I've tried passing a fresh 'options' list to the form but again, it
displays the correct options but doesn't match any of the values as
selections.

Any ideas?

- alex23


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to