Thank you both, I persevered and now have a much more decoupled and
flexible system :)

I am still having some trouble, though, trying to achieve what I'm
trying to do. Ideally, I want to hand the object to the form and have
it populate the field defaults. This works for everything but
CheckBoxLists. I feel like I'm _almost_ there but I just can't
understand some of the widget + validation behaviour.

I have an Entity with skills, a list of Skill objects, and the
following forms & helper functions:

def opt_skills():
    return [(s.id, s.name) for s in Skill.select()]

class CoerceSkill(FancyValidator):
    def _from_python(self, value, state):
        return getattr(value, 'id', 0)
    def _to_python(self, value, state):
        return Skill.get_by(id=value)

class StaffFields(WidgetsList):
    team_id = SingleSelectField(label='Team',
        options=opt_teams,
        default=NULL)
    skills = CheckBoxList(label='Skills',
        options=opt_skills,
        convert=True,
        validator=ForEach(
            CoerceSkill(),
            convertToList=True)
        )

class StaffSchema(Schema):
    team_id = IndexValidator()

The problems:
1. The code works when _setting_ .skills, but won't display which ones
are already present in the list during the initial form render.
2. I put traces on CoerceSkill's to/from_python functions, and for
some reason, it's to_python is called _immediately after_ the
from_python during the initial form render.
3. If I comment out the CoerceSkill validator's 'to_python' function,
it will _display_ the correct skills but doesn't set them (the
opposite of #1).
4. If I attach the skills ForEach validator to the schema, I get
completely different behaviour (I had expected them to be identical).
It tracebacks with "Please enter an integer value"

class StaffSchema(Schema):
    team_id = IndexValidator()
    skills = ForEach(
        CoerceSkill(),
        convertToList=True,
        )

I feel like I'm close to resolving this but after several hours it's
still not clear. Am I trying to deal with a list when I should be
dealing with a single value at some point?

As always, any help or guidance is greatly appreciated.

-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